Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to select entir data of a specific column in sql server database into a form components like lables? my code look like this---here i am not geting the value for lable2.
C#
private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(@"Server=192.168.1.4;Database=DIGIQUEUE;User Id=digiq;Password=digiq123;");
                con.Open();
                string sql1 = "Select callpad_status from callpad ";
                SqlCommand cmd = new SqlCommand(sql1, con);
                SqlDataReader sqlReader = cmd.ExecuteReader();
                while (sqlReader.Read())
                {
                    label1.Text = sqlReader.GetValue(0).ToString();
                    label2.Text = sqlReader.GetValue(1).ToString();
                }
                sqlReader.Close();
            }
            catch (System.Exception ex)
            {
            }
       }
Posted
Updated 28-May-13 20:40pm
v2
Comments
Himanshu__Bisht 29-May-13 2:31am    
Are you getting any error or a undesired result. Pls Share. Thanks!!
Sushil Mate 29-May-13 2:44am    
which data column you want to add in label2? you need to put in select query. the problem lies in the select query.
renish patel 29-May-13 3:12am    
May be you get solution using this...

while(sqlReader.Read())
{
label1.Items.Add(sqlReader["callpad_status"].ToString());
}

Thanks

check this
C#
private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(@"Server=192.168.1.4;Database=DIGIQUEUE;User Id=digiq;Password=digiq123;");
                con.Open();
                string sql1 = "Select callpad_status from callpad ";
                SqlCommand cmd = new SqlCommand(sql1, con);
                SqlDataReader sqlReader = cmd.ExecuteReader();
                label1.Text="";
                while (sqlReader.Read())
                {
                    label1.Text =label1.Text + sqlReader.GetValue(0).ToString(); //you  are selecting only one column in your select query. when you will add one more column in your query then you can use label2 same as label1
                    label2.Text = sqlReader.GetValue(1).ToString();
                }
                sqlReader.Close();
            }
            catch (System.Exception ex)
            {
            }
       }
 
Share this answer
 
Hi,
Please change the select query like this ..

SQL
Select columnname1 , columnname2 from tablename


then you can get the value of lable2
 
Share this answer
 
C#
while(sqlReader.Read())
               {
                   label1.Items.Add(sqlReader["callpad_status"].ToString());
               }

 
Share this answer
 

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