Posted By

gndprx on 02/07/07


Tagged

SqlDataReader


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

paelgr
Priestd09


Simple SqlDataReader C# / .Net 2.0


 / Published in: C#
 

  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  

You need to login to post a comment.