retrieving a single record and binding it to a tex

nikemen

New Member
How do I retrive a single record and bind it to a text box. Whenever I am trying to use SqlDataReader, it gives me an error that "Invalid attempt to read when no data is present" The code that I am using is<BR> Dim dr As SqlDataReader = med.GetFewMedium(theID)<BR> Dim snCount = 0 'Do db call<BR> Do While dr.Read()<BR> snCount = snCount + 1<BR> Loop<BR> If snCount <> 0 Then<BR> dr.Read()<BR> rate_fig.Text = Trim(CType(dr("medium_id"), String))<BR> dr.Close()<BR> End If<BR><BR>But I I am using the same command for a drop down list, it works fine. The code for the drop down is<BR><BR>meds_id.DataSource = med.GetFewMedium(theID)<BR>meds_id.DataBind()<BR>meds_id.Items.Insert(0, new ListItem("Select a Medium"))<BR><BR>Where am I going wrong<BR>The "GetFewMedium" is the stored procedure I am calling<BR><BR>Thanx<BR>in your Do While dr.Read() loop you loop through the entire datareader so now you're at the end. then later you do a dr.Read() in an If statement but since you're already at the end of the datareader your getting an error saying there aren't any records. try something like this...<BR><BR>If dr.Read() Then<BR> rate_fig.Text = Trim(CType(dr("medium_id"),String))<BR>End If<BR><BR>the Read() method reads in the first record in the datareader and returns a boolean value, true if there is a record, false if there's not.<BR><BR>note: this is untested.
 
Back
Top