Simple SqlDataReader C# / .Net 2.0


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. private void bindSomething()
  2. {
  3. string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_From_WebConfig"].ConnectionString;
  4. SqlConnection conn = new SqlConnection(connectionString);
  5. string selectSQL = "Select * from tablename";
  6. SqlCommand getStuff = new SqlCommand(selectSQL, conn);
  7.  
  8. try
  9. {
  10. conn.Open();
  11. SqlDataReader reader = getStuff.ExecuteReader();
  12. while (reader.Read() == true)
  13. {
  14. someLabel.Text = Convert.ToString(reader["column1"]);
  15. someLabel2.Text = Convert.ToString(reader["column2"]);
  16. }
  17. reader.Close();
  18. }
  19. catch
  20. {
  21. Response.Write("Error retrieving data");
  22. }
  23. finally
  24. {
  25. conn.Close();
  26. }
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.