cannot get the SqlDataReader object field value

M

Matt

I just want to get the value of field UserName and Password in SqlDataReader
object dr. But it yeields run-time error "Invalid attempt to read when no
data is present" on line dr("UserName") and dr("Password")

any ideas??

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()
Response.Write("User Name = " & dr("UserName"))
Response.Write("Password = " & dr("Password"))
 
I

IbrahimMalluf

add theses lines to code:

Do While dr.Read
Response.Write("User Name = " & dr("UserName"))
Response.Write("Password = " & dr("Password"))
Loop
 
W

William \(Bill\) Vaughn

if dr.HasRows ' in Version 1.1 of the Framework
Do While dr.Read
Response.Write("User Name = " & dr("UserName"))
Response.Write("Password = " & dr("Password"))
Loop
Else
Response.Write ("No user info on file...")
End if


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

IbrahimMalluf said:
add theses lines to code:

Matt said:
I just want to get the value of field UserName and Password in SqlDataReader
object dr. But it yeields run-time error "Invalid attempt to read when no
data is present" on line dr("UserName") and dr("Password")

any ideas??

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()
Response.Write("User Name = " & dr("UserName"))
Response.Write("Password = " & dr("Password"))
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top