| | |
Textbox DataBinding
Please support our ASP.NET advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 324
Reputation:
Solved Threads: 7
ASP.NET Syntax (Toggle Plain Text)
public partial class FrmOnline : System.Web.UI.Page { string Query; SqlCommand cmd; SqlConnection conn; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { OpenSQLConnection(); SqlCommand cmd = new SqlCommand("select * from Info1 where UID=1", conn); SqlDataReader dr; dr = cmd.ExecuteReader(); while (dr.Read()) { dr.Read(); TextBox1.Text = dr[0].ToString(); } } } private void OpenSQLConnection() { try { conn = new SqlConnection("Data Source=(local);Initial catalog=sonia;User ID=sonia;Password=sonia;"); conn.Open(); } catch (Exception ex) { } }
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.
•
•
Join Date: Oct 2009
Posts: 6
Reputation:
Solved Threads: 2
0
#2 Oct 13th, 2009
•
•
•
•
ASP.NET Syntax (Toggle Plain Text)
public partial class FrmOnline : System.Web.UI.Page { string Query; SqlCommand cmd; SqlConnection conn; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { OpenSQLConnection(); SqlCommand cmd = new SqlCommand("select * from Info1 where UID=1", conn); SqlDataReader dr; dr = cmd.ExecuteReader(); while (dr.Read()) { dr.Read(); TextBox1.Text = dr[0].ToString(); } } } private void OpenSQLConnection() { try { conn = new SqlConnection("Data Source=(local);Initial catalog=sonia;User ID=sonia;Password=sonia;"); conn.Open(); } catch (Exception ex) { } }
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.
Rewrite the code like this and try again
asp.net Syntax (Toggle Plain Text)
if (!IsPostBack) { OpenSQLConnection(); SqlCommand cmd = new SqlCommand("select * from Info1 where UID=1", conn); SqlDataReader dr; try { dr = cmd.ExecuteReader(); if(dr.HasRows) { dr.Read(); TextBox1.Text = dr[0].ToString(); } } } catch(Exception){} finally { if(dr!=null) dr.Close(); } } }
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).
![]() |
Similar Threads
- Code Snippet: Numbers-Only Textbox (C#)
- Replace on pressing spacebar in textbox (C#)
- Focus on textBox? (C#)
- Taking text from textbox to radiobutton(newbie) (C#)
- Code Snippet: Capturing numeric input in a TextBox (C#)
- [Databinding] (VB.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: Converting Html to xml
- Next Thread: label property problem in asp.net
Views: 344 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 activexcontrol advice ajax anathor application asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox child click commonfunctions compatible confirmationcodegeneration content contenttype courier css database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdown dropdownmenu edit expose feedback flash flv form formatdecimal forms formview google grid gridview homeedition hosting identity iframe iis index javascript jquery list menu migration mono mssql multistepregistration nameisnotdeclared object objects order problem ratings refer rotatepage save search security serializesmo.table session silverlight smartcard software sql sqlserver2005 suse textbox tracking typeof unauthorized update validation vb vb.net video view virtualdirectory vista visual-studio visualstudio web webarchitecture webdevelopemnt xml youareanotmemberofthedebuggerusers





