How to display a column value from database to label
-
HI In my windows application i am creating a registration form.. after registration i want to display the register number in Label Cntrl from database(MSSQL2005)... i tried this but not working SqlConnection con = new SqlConnection("server=AAB;database=ab;uid=sa;pwd=pass"); con.Open(); SqlCommand cmd = new SqlCommand("insert into myTable values ('"+textBox1.Text+"') ", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { label1.Text= dr["id"].ToString(); } dr.Close(); Thankx in advance
-
HI In my windows application i am creating a registration form.. after registration i want to display the register number in Label Cntrl from database(MSSQL2005)... i tried this but not working SqlConnection con = new SqlConnection("server=AAB;database=ab;uid=sa;pwd=pass"); con.Open(); SqlCommand cmd = new SqlCommand("insert into myTable values ('"+textBox1.Text+"') ", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { label1.Text= dr["id"].ToString(); } dr.Close(); Thankx in advance
Problem I traced is 1) The SQL Command is "INSERT" instead of select command. Insert Command insert data into tables and return the number of records inserted. 2) DataReader returns object. You have to convert the same into the data type you want eg. code { object obj = dr.read(); Convert.ToString(obj); } SO YOUR REQUIRED CODE SHALL BE LIKE FOLLOWS ---- SqlConnection con = new SqlConnection("server=AAB;database=ab;uid=sa;pwd=pass"); con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM myTable", con); SqlDataReader dr = cmd.ExecuteReader(); while (!dr.IsDbNull()) { Object obj = dr.Read(); label1.Text= obj.ToSring(); } dr.Close(); --- try and reply.
modified on Saturday, May 30, 2009 4:09 AM
-
Problem I traced is 1) The SQL Command is "INSERT" instead of select command. Insert Command insert data into tables and return the number of records inserted. 2) DataReader returns object. You have to convert the same into the data type you want eg. code { object obj = dr.read(); Convert.ToString(obj); } SO YOUR REQUIRED CODE SHALL BE LIKE FOLLOWS ---- SqlConnection con = new SqlConnection("server=AAB;database=ab;uid=sa;pwd=pass"); con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM myTable", con); SqlDataReader dr = cmd.ExecuteReader(); while (!dr.IsDbNull()) { Object obj = dr.Read(); label1.Text= obj.ToSring(); } dr.Close(); --- try and reply.
modified on Saturday, May 30, 2009 4:09 AM