Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to get data from database tables that have several rows and columns, in SQL server to text boxes in Visual Studio form using C#. When I write the code:

C#
        con.Open();
SqlCommand cmd1 = new SqlCommand("Select * from Table1", con);
        SqlDataReader dr1 = cmd1.ExecuteReader();

        while (dr1.Read())
        {
           textBox1.Text = dr1[0].ToString();
           textBox2.Text = dr1[2].ToString();
        }
        con.Close();

I can get the data from the last row of first column and second column into textBox1 and textBox2. If I need to get a value that is in the 3rd row and 5th column, how can I write the code?

What I have tried:

I am trying something like:
textBox1.Text = dr1[4].Rows[2].ToString();
I get build error - error CS1061: 'object' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Can anyone please help me to write correctly? Thanks.
Posted
Updated 3-Jun-16 6:11am

The reader positions at the next row when you dr1.read() so the first dr1.read presents you with the first row, you cannot access subsequent rows without moving the read on to those rows.

It might be better to get you select to return just the information you need, how do you know it is the third row?

To make a simple example to meet your question the following will do it:

C#
dr1.Read();
           textBox1.Text = dr1[0].ToString();
           textBox2.Text = dr1[2].ToString();
dr1.read();
dr1.read();
           textBox3.Text = dr1[4].ToString();


The flaw in the example is obvious, you must know that you returned at least three rows or test each read for success.
 
Share this answer
 
v2
Comments
Savi kolla 3-Jun-16 13:35pm    
My problem is solved. Thanks so much !!!
Karthik_Mahalingam 5-Jun-16 2:42am    
Hi Savi
if your question is resolved, Cilck  Accept answer  to close this post.
You can also use the SqlDataAdapter class and fill a DataTable with the returned values.
For more information see MSDN SqlDataAdapter Class (System.Data.SqlClient)[^]

The code in your case would be something like this:
C#
con.Open();
SqlCommand cmd1 = new SqlCommand("Select * from Table1", con);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd1;
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
con.Close();

if ((dataTable.Rows.Count > 0) && (dataTable.Columns.Count > 2))
{
    textBox1.Text = dataTable.Rows[0][0].ToString();
    textBox2.Text = dataTable.Rows[0][2].ToString();
}
if ((dataTable.Rows.Count > 2) && (dataTable.Columns.Count > 4))
{
    textBox3.Text = dataTable.Rows[2][4].ToString();
}


A better way would be to used named columns in the query.
C#
con.Open();
SqlCommand cmd1 = new SqlCommand("Select Column1, Column3, Columns5 from Table1", con);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd1;
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
con.Close();

if (dataTable.Rows.Count > 0)
{
    textBox1.Text = dataTable.Rows[0]["Column1"].ToString();
    textBox2.Text = dataTable.Rows[0]["Column3"].ToString();
}
if (dataTable.Rows.Count > 2)
{
    textBox3.Text = dataTable.Rows[2]["Column5"].ToString();
}

The column names are of course only examples, it should be names like Id, Name, CurrentDate or similar.
 
Share this answer
 
Comments
Savi kolla 3-Jun-16 13:35pm    
Thanks so much !!!
George Jonsson 3-Jun-16 22:47pm    
You are welcome.
Karthik_Mahalingam 5-Jun-16 2:43am    
Hi Savi
if your question is resolved, Cilck  Accept answer  to close this post. else someone will post the answer unnecessarily

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900