I have four different SPs and need to generate four sheets in Excel. The first three sheets are generated, but when it comes to generating the 4th sheet, the system will return index was out of range exception.
dt = crm.GetAllCrmInquiry(Convert.ToInt32(userid), txtdate.Text.ToString(), txttodate.Text.ToString());
Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];
sheet.Name = "All Inquiry";
sheet.InsertDataTable(dt, true, 1, 1);
dt = crm.GetFollupDetail();
sheet = book.Worksheets[1];
sheet.Name = "FollowUp";
sheet.InsertDataTable(dt, true, 1, 1);
dt = crm.GetOpenInquiry_With_FollupDetail();
sheet = book.Worksheets[2];
sheet.Name = "Open Inquiry with FollowUp";
sheet.InsertDataTable(dt, true, 1, 1);
dt = crm.Get_Sample_Requisition_Inquiry_Detail(txtdate.Text, txttodate.Text);
sheet = book.Worksheets[3];
sheet.Name = "Sample Requition";
sheet.InsertDataTable(dt, true, 1, 1);
book.SaveToFile("D:\\Supporting Documents\\CRM ALL Inquiry.xlsx", ExcelVersion.Version2016);

Vijay Pratap SinghRank #381 · 6.6K reputationPosted Dec 28, 2023, 5:54 AM EST
The "index was out of range" exception typically occurs when trying to access an index in a collection that doesn't exist. In your case, it could be related to the index used to access the Worksheets collection in the Workbook.
dt = crm.GetAllCrmInquiry(Convert.ToInt32(userid), txtdate.Text.ToString(), txttodate.Text.ToString()); Workbook book = new Workbook(); Worksheet sheet = book.Worksheets.Add("All Inquiry"); sheet.InsertDataTable(dt, true, 1, 1); dt = crm.GetFollupDetail(); sheet = book.Worksheets.Add("FollowUp"); sheet.InsertDataTable(dt, true, 1, 1); dt = crm.GetOpenInquiry_With_FollupDetail(); sheet = book.Worksheets.Add("Open Inquiry with FollowUp"); sheet.InsertDataTable(dt, true, 1, 1); dt = crm.Get_Sample_Requisition_Inquiry_Detail(txtdate.Text, txttodate.Text); sheet = book.Worksheets.Add("Sample Requisition"); sheet.InsertDataTable(dt, true, 1, 1); book.SaveToFile("D:\\Supporting Documents\\CRM ALL Inquiry.xlsx", ExcelVersion.Version2016);Hope this will help
Ck NitinRank #189 · 12.9K reputationPosted Dec 28, 2023, 11:25 AM EST
An "index was out of range" exception typically indicates that you are trying to access an element at an index that doesn't exist in the collection. This could be due to various reasons, such as an incorrect index value, an issue with the data, or a problem with the loop or logic used to generate the sheets. Here's a systematic approach to troubleshoot and resolve the issue:
Review the Code:
Check Index Values:
Data Validation:
Conditional Statements:
Debugging:
Error Handling:
Logging:
Array/List Initialization:
Review SPs:
Consult Documentation:
By systematically reviewing these aspects of your code, you should be able to identify and resolve the "index was out of range" exception. If you provide more specific details about the relevant code snippets or error messages, I may be able to offer more targeted assistance.
Ziauddin ChoudharyRank #1,978 · 739 reputationPosted Dec 29, 2023, 12:08 AM EST
This is very common error thrown when want to access some sheet in the file which is not available. and hence please check your template by pressing ALT+F11 to identify the Sheet Name and Position.