cannot get the SqlDataReader object field value

  • Thread starter Thread starter Matt
  • Start date Start date
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"))
 
-----Original Message-----
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"))




.

Invoke the Read() method on the SqlDataReader object
before accessing any values.
 
Invoke the Read() method on the SqlDataReader object
before accessing any values.

And before you do that, you should check that it has rows. Here's a
fragment (in C#) that is fairly robust:


dr = cmd.executeReader(strSQL);

if (dr.HasRows)
{
int rIdCol = dr.GetOrdinal("rId");
int langCol = dr.GetOrdinal("language");
int capCol = dr.GetOrdinal("caption");
int textCol = dr.GetOrdinal("text");

while (dr.Read())
{
ret = new TextResource
( dr.GetInt32 (rIdCol)
, dr.GetString(langCol)
, dr.GetString(capCol)
, dr.GetString(textCol)
);
dr.Close();
}
}
else
{
dr.Close();
ret = null;
}

-- Rick
 
add theses lines to code:

Do While dr.Read
Response.Write("User Name = " & dr("UserName"))
Response.Write("Password = " & dr("Password"))
Loop
 
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"))
 
Back
Top