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 SinghPosted Dec 28, 2023, 10:54 AM
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.
Hope this will help
Ziauddin ChoudharyPosted Dec 29, 2023, 5:08 AM
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.
Ck NitinPosted Dec 28, 2023, 4:25 PM
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.