Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am sending data from dynamic textboxes to Excel with a for loop. I need to insert a for loop inside of the existing one to add some text that I am splitting with a '|' in cell E only for the new added rows. Below is my existing for loop. The problem I am having is when I try to declare the String to identify the dynamic textbox I want to access it gives me an error **"Object reference not set to an instance of an object."** Everything with double quotes is a dynamic textbox from the same panel. I can only assume it is because the control is dynamic and it hasn't been created yet at that point? but what is the difference between the other dynamics I am using in the existing for loop? The solution I am looking for is how to create the identifier so I can use the array.
C#
int StartBundleRow = 10;
  for (int BndlRow = 0; BndlRow < bundleRows; BndlRow++)//add rows to spreadsheet
       {
       worksheet.Rows[StartBundleRow].Insert();
       worksheet.Rows[StartBundleRow].Font.Size = 14;
       worksheet.Rows[StartBundleRow].Font.Bold = true;
       worksheet.Rows[StartBundleRow].Interior.Color = CyberaWhite;
       worksheet.Columns["A"].Interior.Color = CyberaGrey;
       worksheet.Columns["J:XFD"].Interior.Color = CyberaGrey;
       worksheet.Cells[StartBundleRow, "C"].Interior.Color = CyberaPurple;
       worksheet.Rows[StartBundleRow].HorizontalAlignment = XlHAlign.xlHAlignLeft;
       worksheet.Cells[StartBundleRow, "D"].value =             srcBundlePanel.Controls["txtQtyBundle" + BndlRow].Text;
            worksheet.Cells[StartBundleRow, "E"].value = srcBundlePanel.Controls["txtProductNameBundle" + BndlRow].Text;

       ----HERE IS WHERE I AM WORKING----

 string DescriptionSplit = srcBundlePanel.Controls["txtProductDescBundle"].Text;
 string[] descriptionParts = DescriptionSplit.Split('|');
   for (int i = 0; i < descriptionParts.Length; i++)
       {
       worksheet.Rows[StartBundleRow].Insert();    //applies the description for the default bundle row
       worksheet.Rows[StartBundleRow].Font.Bold = false;
       worksheet.Cells[StartBundleRow++, "E"].Value = rowIndent + descriptionParts[i].Trim();

       ----TO HERE----
       }
       worksheet.Cells[StartBundleRow, "F"].value = srcBundlePanel.Controls["txtListPriceBundle" + BndlRow].Text;
       worksheet.Cells[StartBundleRow, "G"].value = srcBundlePanel.Controls["txtMaxDiscountBundle" + BndlRow].Text;
       worksheet.Cells[StartBundleRow++, "H"].value = srcBundlePanel.Controls["txtProposedPriceBundle" + BndlRow].Text;
       }
Posted
v2
Comments
BillWoodruff 13-Dec-15 23:26pm    
Put a breakpoint before the line:

string DescriptionSplit = srcBundlePanel.Controls["txtProductDescBundle"].Text;

When you hit that break-point, use Visual Studio's dynamic inspection tool to check the contents of 'srcBundlePanel.

What you should be doing is creating a Dictionary of Types String, TextBox to hold the references to the TextBoxes created at run-time; that way you can do a direct look-up based on the name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900