0

Ive been having some problems with understanding the MySql reader from MySql.Data.MySqlClient

This is my Code:

while (reader.Read())
{
    documentButtons = new RadioButton[3];
    for (int i = 0; i < 3; i++)
    {
        documentButtons[i] = new RadioButton();
        documentButtons[i].Text = reader["name"].ToString() + " " + (i + 1);

        Console.WriteLine(reader["name"].ToString());

        documentButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);
        this.Controls.Add(documentButtons[i]);
    }
}
Console.ReadLine();

This makes 3 radiobuttons with the texts: "Dokument1 1", "Dokument1 2", "Document1 3" As you can probably see, i want the number right behind "Dokument" to be equal to the number behind that. (My 3 first entries in the database is "Dokument1", "Dokument2" and "Dokument3". In my console, "Dokument1", 2 and 3, all show up 3 times. How can i make it create the appropriate name on the appropriate radiobutton?

1
  • Why is the for loop there at all, if what you want is just the three radio buttons? You're building three for each database record, it looks like. Commented Nov 16, 2011 at 19:15

1 Answer 1

4

I'm not sure I fully understand your question, but if you simply want to have a single RadioButton for each row returned, the following code should work:

documentButtons = new RadioButton[3];
int i = 0;
while (i <= 3 && reader.Read()) { // make sure we don't get an IndexOutOfRangeException
  documentButtons[i] = new RadioButton();
  documentButtons[i].Text = reader["name"].ToString() + " " + (i + 1);

  Console.WriteLine(reader["name"].ToString());

  documentButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);
  this.Controls.Add(documentButtons[i]);

  ++i;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I like that you just posted nearly letter for letter the answer I was typing. :)
@reallyJim: you have to type FASTER! ;)
Ah, it was the while with the reader and the "i" i was looking for, thanks a lot :) Works like a charm thank you :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.