| | |
Textbox DataBinding
Please support our ASP.NET advertiser: Intel Parallel Studio Home
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
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 activexcontrol advice ajax alltypeofvideos anathor application asp asp.net bc30451 bottomasp.net box browser button c# checkbox click commonfunctions confirmationcodegeneration css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dynamically edit expose feedback fileuploader fill flash form formatdecimal forms formview google grid gridview gudi iframe iis javascript list listbox login microsoft mono mouse mssql multistepregistration news numerical objects opera panelmasterpagebuttoncontrols parent project radio redirect registration relationaldatabases reportemail richtextbox rotatepage save schoolproject search security select silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking unauthorized validation vb.net video videos view virtualdirectory vista visualstudio web webapplications webdevelopemnt webprogramming webservice xsl youareanotmemberofthedebuggerusers





