We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

gndprx on 02/07/07


Tagged

SqlDataReader


Versions (?)


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.