1

I have a simple C# windows form in which I have 6 text-boxes. Now I have a SQL query which returns 3 rows (it will always return 3 rows - query gets data from junction table and another table) and each row has 2 columns (Name and Email). What I want to do is populate these 6 text-boxes with the result from sql query like Name 1 & Email 1 should get the value of row 1, Name 2 & Email 2 should get the value of row 2 etc etc.

I have searched here and there but I haven't been able to find a solution so please help me out. I know I need to apply some loop here but I am not able to find the logic here.

Images for reference :

SQL Query Result :

enter image description here

C# Win Form :

enter image description here

Code (which is obviously not the way to do it) :

        SqlDataReader rdr = null;
        SqlConnection con = null;
        SqlCommand cmd = null;
        String Name;
        String Email;
        string ConnectionString = "// my connection string...";
        con = new SqlConnection(ConnectionString);
        con.Open();
        string CommandText = "//sql query goes here";
         cmd = new SqlCommand(CommandText);
         cmd.Connection = con;
        rdr = cmd.ExecuteReader();
        while(rdr.Read())
          {
                  Name = rdr["Name"].ToString(); 
                 Email = rdr["Email"].ToString();
          }

        con.Close();
4
  • 1
    You should really be using a repeater control for list like data. Commented Mar 26, 2014 at 12:49
  • 2
    Please show us your code on how you retrieve the data Commented Mar 26, 2014 at 12:49
  • if its only 3 sets, you can just as well hard code the control names. Abstraction (I.E., a control loop) isn't really buying you much yet. Commented Mar 26, 2014 at 12:55
  • 1
    @Aron Isn't repeater control in asp only ? Commented Mar 26, 2014 at 13:06

2 Answers 2

1

Create two arrays of your textboxes

 TextBox[] names = new TextBox[] {txtName1, txtName2, txtName3};     
 TextBox[] mails = new TextBox[] {txtMail1, txtMail2, txtMail3};

Now after retrieving the reader for your table

int idx = 0;
while(reader.Read() && idx < names.Length)
{
    names[idx].Text = reader.GetString(0);
    mails[idx].Text = reader.GetString(1);
    idx++;
}

Of course this solution is strictly limited by your statement: it will always return 3 rows

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Sir, you have just solved my problem. I can't believe I didn't think of arrays - how stupid of me....
what if I wanted to do same in DataGridView? I have added three columns in DataGridView and want to display Name1, Name2 and Name3.
Bind the DataGridView to a DataTable. It is a lot easier
1

You may put result of query into DataTable. And later you can set textboxes to show data on each row of DataTable.

Comments

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.