Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie alert - SQLDataReader question 1

Status
Not open for further replies.

sbushway

Programmer
Jan 27, 2003
58
US
Let me just preface this post with the statement that I'm fairly new to .NET.

I want to create a Master/Detail page. I've got the Master page working fine: it contains a list of records each having a clickable ID field that passes that ID value to the Detail page.

I want to display the data corresponding to the ID that was passed on my second page. I know how to do it using a Stored Procedure, but I don't know how to do it without using one.

Here's what I have so far in my Page_Load sub:

'Get the value of the URL variable that was passed
Dim intID as Integer = CInt(Request.Params("ID))

Dim strSQL as String = "select o.orderid, o.cycle, o.orderingUIC, o.shippingUIC, o.answersheets, e.quantity, e.rate from ordertblx o, examsx e where (o.orderid = e.orderid) and o.orderid =" & intID

Dim myConn as SqlConnection = New SQLConnection(ConfigurationSettings.AppSettings("ConnectionString"))
myConn.Open()

Dim myComm as SqlCommand = New SqlCommand(strSQL, myConn)
Dim myDR as SqlDataReader = myComm.ExecuteReader(CommandBehavior.CloseConnection)

Here's where my problem is:
There will only be one record returned and all I want to do is set certain labels to the fields in the record returned.

I read in a book to do something like this:
lblCycle.Text = myDR.Item("cycle")
lblOrderingUIC.Text = myDR.Item("orderingUIC")

But that didn't work - I got a message saying that "Invalid attepmpt to read when no data is present."

Any help/suggestions would be greatly appreciated.

Thanks in advance,
Suzanne
 
Are you sure you are returning data??. Check by looking at the hasrows property of your datareader

Code:
dim anyData as Boolean = mydr.hasrows()
Firstly you will need to read the 1st record.
Code:
mydr.read()
Finally...I think that each element in the datareader is an object and as such you may need to convert to string
Code:
lblCycle.Text = cstr(myDR.Item("cycle"))




Sweep
...if it works dont mess with it
 
Future note.... if you're going to read multiple records you can use


do while mydr.read
'Assignment Code goes here
loop

Scott
Programmer Analyst
<{{><
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top