Of course i am a newbie in c# and not much experienced about programming, so this must be an easy thing but i just cannot find the solution.
I've been working on a project in Visual.NET C# Express. I've built the main view first, then added database connections, then started coding, so this may be holding the secret.

In the attachment you can see the tab i am working on it.
I am trying to take the names of radio buttons from a databases only row.

There is a database with just 1 row, and i have just added every row on the database with textboxes. You can see them in the red box no:2. they are working great, they get the names from database. The names hold in database under n1,n2,n3... rows.

After this, i have added a code like (r1 means like radiobutton1) r1.text=n1.text etc. But if i create this code under the form1load action, it just does not work, if i put this code under a click action or something else, the code works perfectly. But i want them to take the texts on load action. I guess the problem is order of the code lines, creating the radiobuttons and changing their names.

How can i make them to be created before my code works. Or any other idea that can get texts directly from sql to radiobuttons names.

Thanks anyway... :)

Recommended Answers

All 7 Replies

Where does the code that calls the database reside? If you're using a DataSet the designer defaults the .Fill() call in your form's load event. You need to run the code to assign the text *after* you load the data from SQL. Your code to call the sql server should happen in or before the load method as a "best practice."

private void Form1_Load(object sender, EventArgs e)
        {
              
            // TODO: This line of code loads data into the 'fiyatDataSet.kayit' table. You can move, or remove it, as needed.
            this.kayitTableAdapter.Fill(this.fiyatDataSet.kayit);
            // TODO: This line of code loads data into the 'projetDataSet.proje' table. You can move, or remove it, as needed.
            this.projeTableAdapter.Fill(this.projetDataSet.proje);
            // TODO: This line of code loads data into the 'projetDataSet.firma' table. You can move, or remove it, as needed.
            this.firmaTableAdapter1.Fill(this.projetDataSet.firma);
            // TODO: This line of code loads data into the 'kayitDataSet.firma' table. You can move, or remove it, as needed.
            this.firmaTableAdapter.Fill(this.kayitDataSet.firma);
            // TODO: This line of code loads data into the 'calkayDataSet.calisan' table. You can move, or remove it, as needed.
            this.calisanTableAdapter.Fill(this.calkayDataSet.calisan);

            r1.Text = n1.Text;
            r2.Text = n2.Text;
            r3.Text = n3.Text;
            r4.Text = n4.Text;
            r5.Text = n5.Text;
            r6.Text = n6.Text;
            r7.Text = n7.Text;
            r8.Text = n8.Text;
            r9.Text = n9.Text;
            r10.Text = n10.Text;
            

        }

This is my form1 load code. Here my naming code comes after my dataset's creation. The data i want comes from "fiyatdataset". But this is not working

I guess the problem is c# creates the textboxes after runs my naming code. If there way to get the names directly from the database? Or another way for doing this?

Where is your databinding occuring? I use a TextBox and bound it to a bindingSource which was wired up to my DataSet and got the bound value in the form's load event:

private void frmDataBind_Load(object sender, EventArgs e)
    {
      // TODO: This line of code loads data into the 'dataSet1.Customer' table. You can move, or remove it, as needed.
      this.customerTableAdapter.Fill(this.dataSet1.Customer);
      this.Text = this.textBox1.Text;
    }

It worked as expected. What you could also do is:

private void frmDataBind_Load(object sender, EventArgs e)
    {
      // TODO: This line of code loads data into the 'dataSet1.Customer' table. You can move, or remove it, as needed.
      this.customerTableAdapter.Fill(this.dataSet1.Customer);
      //this.Text = this.textBox1.Text;
      if (this.dataSet1.Customer.Rows.Count > 0)
      {
        this.Text = Convert.ToString(this.dataSet1.Customer.Rows[0]["CustNumber"]);
      }
    }

I guess the problem is c# creates the textboxes after runs my naming code. If there way to get the names directly from the database? Or another way for doing this?

Just an FYI because it's not a solution to your problem. Unless you have manually implemented the creation of your controls, they are created in the form's constructor via a call to InitializeComponents() . So, assuming that this is how your controls are created, then they have already been created because your form's Load() method gets called after the form is created, but before it shown/active.

Here is an excerpt from MS about the designer generated method:

InitializeComponent is a designer-managed area of your form code to which the designer serializes relevant initialization code for the controls and components hosted on the form. The designer uses InitializeComponent to remember control state across form loads at design time and to initialize controls for run-time execution

thanks anyway but i don't have any problems about creating textboxes and binding them. they are taking their texts from my database, there is no problem with this but i want also make radiobuttons to take their names from this textboxes or the database. I cannot to this part.
Also the things that DdoubleD said confused me again. I know about the creating order of the forms and their designs but if that is true, than there shouldn't be any problem with my project. because textboxes takes their names at creation and my form load event comes after it. i don't know why but my textboxes works perfectly, but radio buttons cannot get their texts from textboxes texts.
also there is no problem with my code, because if i assign it to another event like click or anything else, it works perfectly


but thanks anyway guys, i am going to try making direct bindings between database and my radiobuttons texts from sknake's post.

thanks sknake, the second part in your post worked perfectly, sorry for my misunderstanding :)
thanks again guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.