Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am looking to access rows one after the other from sql database. I am unable to traverse through to the second row of the table. Below is the code sample. Please help.

C#
if (dr.HasRows)
                {
                    while (dr.Read())
                    //int i=0, j=0;
                    {

                Src_EpId = dr[0].ToString();
                //textBox13.Text = Src_EpId+"\n";

                Device_ID = dr[1].ToString();
                // textBox1.Text = Device_ID;
                Control_ID = dr[2].ToString();
                Control_Type = dr[3].ToString();
                Current_Value = dr[4].ToString();
                Message_State = dr[5].ToString();
                Change_Value = dr[6].ToString();
                Location = dr[7].ToString();
                Image_Url = dr[8].ToString(); // total columns = 9. rows = 5. How to access row2 column1, row2 column2 etc. individually?
Posted
Updated 28-Mar-12 0:11am
v2
Comments
Herman<T>.Instance 28-Mar-12 6:11am    
what erro do you get?
sachin10d 28-Mar-12 7:29am    
This should work fine
can you please explain more on this
sachin10d 28-Mar-12 7:31am    
while loop continues till there are records in datareader so no need to worry about the row2 column1 your code will access all the records.

1 solution

Hi,

You can fill data to datatables first.After that you can access rows and columns whichever you want.

C#
DataTable dt = new DataTable();
dr.Load(dt);
foreach(DataRow dr in dt.Rows)
{
 dr["columnName"];
 // and other columns you can get
}
 
Share this answer
 
v2
Comments
[no name] 28-Mar-12 10:32am    
good one

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