get record field value in ASP.NET

M

Matt

In ASP 3.0, rs("field_name") is able to get the value of field_name in a
recordset.

In ASP.NET using VB.NET, I think SqlDataReader is the substitute of
recordset? I don't quit understand how to use it.

Here's my code attempts, but it yields run-time error "Invalid attempt to
read when no data is present"on dr(i).ToString

Dim cn As New SqlConnection(cs)
Dim sql As String = "select * from [Admin] where UserName = '" &
TextBox1.Text & "'" & "AND Password = '" & TextBox2.Text & "'" & ";"
Dim cmd As New SqlCommand(sql, cn)
cmd.Connection.Open()
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
For i = 0 To dr.FieldCount - 1
strLine = strLine & dr.GetName(i).ToString & Chr(9) & dr(i).ToString
Next


Please advise!
Thanks!
 
A

arulvendan

-----Original Message-----
In ASP 3.0, rs("field_name") is able to get the value of field_name in a
recordset.

In ASP.NET using VB.NET, I think SqlDataReader is the substitute of
recordset? I don't quit understand how to use it.

Here's my code attempts, but it yields run-time error "Invalid attempt to
read when no data is present"on dr(i).ToString

Dim cn As New SqlConnection(cs)
Dim sql As String = "select * from [Admin] where UserName = '" &
TextBox1.Text & "'" & "AND Password = '" & TextBox2.Text & "'" & ";"
Dim cmd As New SqlCommand(sql, cn)
cmd.Connection.Open()
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
For i = 0 To dr.FieldCount - 1
strLine = strLine & dr.GetName(i).ToString & Chr(9) & dr(i).ToString
Next


Please advise!
Thanks!


.

When you say dr(0) , it means that you are trying to
access the first column of the current row.In your
case,since the SqlDataReader has just been created you
need to invoke the Read() method to move the record
pointer to point to the first row and then the subsequent
rows.The Read() method advances the cursor to the next
row.It returns -1 at the end.

Hope this would help..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top