Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 629351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:44:11+00:00 2026-05-13T19:44:11+00:00

Im having some problems parsing a string to a textbox type, im using the

  • 0

Im having some problems parsing a string to a textbox type, im using the code in a diffrent form in the program and its working fine, but when im tying the same two lines here i get a null exception

the two lines of intrest is

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

new info

Greg put me in the direction to check waht was inside the Controls[] array and this reveals what my problem is. It only contains 90 lines of TabControl info.

this is the line

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

this line is dublicated 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 can this be isen’t the Controls array populated on Initialize?


Orginal post

and this is the full loop

        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();
        } 

in trying to debug it i did this

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

and what happens is it sets the txtBCont to Textbox on this line txtBCont = br1txtB1;
but on the txtBCont = (TextBox)Controls[txbName]; it sets it back to null again.

anyone got a clue what the error is here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:44:11+00:00Added an answer on May 13, 2026 at 7:44 pm

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

    Check the value of txtBCont using the debugger.
    Check the Name properties of all of the items in Controls using the debugger.

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

    Edit: this code (or something close it to it) should print out the Name of all the textboxes 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’m assuming you’re using a Windows Forms project. The problem is that controls aren’t in an array, they’re in a hierarchy. The TabControl has its own Controls property that contains TabPages. Each TabPage has its own Controls collection…etc.

    The Find method on the Controls property can perform a recursive search on this hierarchy. Unfortunately it can return more than one control, so you need to account for that. In the code below, I’ve made the assumption that only one control with the request name can exist on the form, and I’ve thrown an exception if it doesn’t.

            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 really sure if InvalidOperationException is the best choice here….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 419k
  • Answers 419k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, your relationships should always be bi-directional. If they are… May 15, 2026 at 10:29 am
  • Editorial Team
    Editorial Team added an answer Yes, it's possible. In your xml you simply put the… May 15, 2026 at 10:29 am
  • Editorial Team
    Editorial Team added an answer when you work with UI from background thread you should… May 15, 2026 at 10:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.