Getting an exception exception exception in a loop. Debugging Attempt

I have some problems analyzing a string like textbox, im using code in various forms in the program and its operation is fine, but when they bind the same two lines, here I get a null exception

two lines of intervets

string txbName = "br" + bruker + "txt" + 'B' + o; txtBCont = (TextBox)Controls[txbName]; 

new information

Greg set me aside to verify that waht is inside the Controls [] array, and this shows what my problem is. It contains only 90 lines of TabControl information.

this is a string

 System.Windows.Forms.TabControl, TabPages.Count: 2, TabPages[0]: TabPage: {ShowWeek} 

this line is duplicated 90 times when I run this code inside my catch block

  catch( System.Exception excep) { System.IO.StreamWriter SW; SW = File.AppendText("C:\\MyDumpFile.txt"); foreach (Control ctrl in Controls) { SW.WriteLine(ctrl); } SW.Close(); } 

how could it not be in a Controls array filled with Initialize?


Post post

and this is the full cycle

  int dayOfset; int bruker; TextBox txtBCont; for (int i = 0; i < 18; i++) { mysqlCon.Open(); dayOfset = -4; bruker = i + 1; for (int o = 1; o < 6; o++) { MySqlCommand cmd = new MySqlCommand("SELECT (NyeSaker + GamleSaker - (select GamleSaker FROM saker Where Dato = '" + dateTimePicker1.Value.AddDays(dayOfset + 1).ToString("yyyy-MM-dd") + "' AND Bruker_ID = '" + bruker + "' ) ) FROM saker Where Bruker_ID = '" + bruker + "' AND Dato = '" + dateTimePicker1.Value.AddDays(dayOfset).ToString("yyyy-MM-dd") + "'", mysqlCon); string txbName = "br" + bruker + "txt" + 'B' + o; txtBCont = (TextBox)Controls[txbName]; //1 past og dp kontrol// try { txtBCont.Text = cmd.ExecuteScalar().ToString(); } catch( System.Exception excep) { //txtBCont1.Text = "0"; MessageBox.Show(excep.Message); } dayOfset++; } mysqlCon.Close(); } 

trying to debug it i did it

 string txbName = "br" + bruker + "txt" + 'B' + o; txtBCont = br1txtB1; txtBCont = (TextBox)Controls[txbName]; 

and what happens, it sets txtBCont to the text field on this line txtBCont = br1txtB1; but on elements txtBCont = (TextBox) [txbName]; it returns null again.

Has anyone realized that this is a mistake?

+4
source share
5 answers

Set a breakpoint on the line MessageBox.Show(excep.Message); and run the code.

Check the txtBCont value using the debugger.
Check the name properties of all elements in Controls using the debugger.

Is your assumption that there are controls where bruker> = 0 and <= 17 true?
Your guess is that there are controls where o> = 1 and <= 6 true?

Edit: this code (or something next to it) should print the name of all the text fields so you can double check.

 foreach( Control ctrl in Controls ) { TextBox textBox = ctrl as TextBox; if( textBox != null ) Console.WriteLine( textBox.Name ); } 

Edit 2:

I assume you are using a Windows Forms project. The problem is that the controls are not in the array, they are in the hierarchy. TabControl has its own Controls property containing TabPages. Each tab has its own collection of Controls ... etc.

The Find method in the Controls property can perform a recursive search in this hierarchy. Unfortunately, it can return more than one control, so you need to consider this. In the code below, I made the assumption that there can only be one control with the name of the request in the form, and I threw an exception if it is not.

  Control[] matchingControls = Controls.Find(txbName , true); // true means recursive search if (matchingControls.Length == 0 || !(matchingControls[0] is TextBox) ) throw new InvalidOperationException("No TextBoxes with name " + txbName + " found in the form."); if (matchingControls.Length > 1) throw new InvalidOperationException("Multiple controls with name " + txbName + " found in the form. Please use unique names"); txtBCont = (TextBox)matchingControls[0]; 

Note. I'm not sure that InvalidOperationException is the best choice here.

+1
source

When you try to pull something from a hash or dictionary, you will get a zero return if it does not exist. In this case, the control by name you are looking for does not exist. I noticed that your second loop index starts at 1:

 for (int o = 1; o < 6; o++) 

Is it possible that you are disabled by 1 and the control does not exist?

+2
source

Is it possible that the string created in txbName does not exist in the Controls array? The [txtbName] controls may not exist and therefore return null.

+1
source

  Try

string txbName = string.Format ("br {0} txt '{1}' {2}", bruker, "B", o);

+1
source

I would not open and close your connection every time in the cycle, open it before the cycle and close it after.

+1
source

Source: https://habr.com/ru/post/1302261/


All Articles