Textbox DataBinding

Thread Solved

Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Textbox DataBinding

 
0
  #1
Oct 13th, 2009
  1. public partial class FrmOnline : System.Web.UI.Page
  2. {
  3. string Query;
  4. SqlCommand cmd;
  5. SqlConnection conn;
  6.  
  7. protected void Page_Load(object sender, EventArgs e)
  8. {
  9. if (!IsPostBack)
  10. {
  11. OpenSQLConnection();
  12. SqlCommand cmd = new SqlCommand("select * from Info1 where UID=1", conn);
  13. SqlDataReader dr;
  14. dr = cmd.ExecuteReader();
  15. while (dr.Read())
  16. {
  17. dr.Read();
  18. TextBox1.Text = dr[0].ToString();
  19. }
  20.  
  21. }
  22. }
  23.  
  24.  
  25. private void OpenSQLConnection()
  26. {
  27. try
  28. {
  29.  
  30. conn = new SqlConnection("Data Source=(local);Initial catalog=sonia;User ID=sonia;Password=sonia;");
  31. conn.Open();
  32.  
  33. }
  34. catch (Exception ex)
  35. {
  36.  
  37. }
  38.  
  39. }

I m getting error in line TextBox1.Text = dr[0].ToString();
Invalid attempt to read when no data is present.

But the record is there in DB,of UID 1.then y the error is coming.
Attached Thumbnails
DB.JPG  
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 6
Reputation: shenulal is an unknown quantity at this point 
Solved Threads: 2
shenulal shenulal is offline Offline
Newbie Poster
 
0
  #2
Oct 13th, 2009
Originally Posted by sonia sardana View Post
  1. public partial class FrmOnline : System.Web.UI.Page
  2. {
  3. string Query;
  4. SqlCommand cmd;
  5. SqlConnection conn;
  6.  
  7. protected void Page_Load(object sender, EventArgs e)
  8. {
  9. if (!IsPostBack)
  10. {
  11. OpenSQLConnection();
  12. SqlCommand cmd = new SqlCommand("select * from Info1 where UID=1", conn);
  13. SqlDataReader dr;
  14. dr = cmd.ExecuteReader();
  15. while (dr.Read())
  16. {
  17. dr.Read();
  18. TextBox1.Text = dr[0].ToString();
  19. }
  20.  
  21. }
  22. }
  23.  
  24.  
  25. private void OpenSQLConnection()
  26. {
  27. try
  28. {
  29.  
  30. conn = new SqlConnection("Data Source=(local);Initial catalog=sonia;User ID=sonia;Password=sonia;");
  31. conn.Open();
  32.  
  33. }
  34. catch (Exception ex)
  35. {
  36.  
  37. }
  38.  
  39. }

I m getting error in line TextBox1.Text = dr[0].ToString();
Invalid attempt to read when no data is present.

But the record is there in DB,of UID 1.then y the error is coming.
hai,
Rewrite the code like this and try again

  1. if (!IsPostBack)
  2. {
  3. OpenSQLConnection();
  4. SqlCommand cmd = new SqlCommand("select * from Info1 where UID=1", conn);
  5.  
  6. SqlDataReader dr;
  7. try
  8. {
  9. dr = cmd.ExecuteReader();
  10. if(dr.HasRows)
  11. {
  12. dr.Read();
  13. TextBox1.Text = dr[0].ToString();
  14. }
  15. }
  16. }
  17. catch(Exception){}
  18. finally
  19. {
  20. if(dr!=null) dr.Close();
  21. }
  22. }
  23. }

update me with the result.

Thanks,
Shenu
Last edited by peter_budo; Oct 13th, 2009 at 2:22 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks).
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 344 | Replies: 1
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC