One Row of Data

What is the easiest/best way to read in one row of data from a SQL server?  Should I just read it into a datareader?
LVL 1
NeoTekAsked:
Who is Participating?
 
doobdaveConnect With a Mentor Commented:
Hi,

you need to do a drUserInfo.Read() before you can access the contents of the DataReader object, as before then it is placed at BOF (beginning of file).

HTH
David
0
 
b1xml2Commented:
yep.
fastest, no frills way
0
 
NeoTekAuthor Commented:
The following code isn't working.  I get the error:

Invalid attempt to read when no data is present.

Code:

        Try
            connUsers.Open()
            cmdUserInfo.Parameters.Add("@userID", Trim(txtUsername.Text))
            cmdUserInfo.Parameters.Add("@password", Trim(txtPassword.Text))
            drUserInfo = cmdUserInfo.ExecuteReader(CommandBehavior.CloseConnection)
        Catch ex As Exception
            lblError.Text = "Error connecting to server."
            Exit Sub
        End Try

        If drUserInfo Is Nothing Or drUserInfo.HasRows = False Then
            lblError.Text = "Invalid username or password."
            Exit Sub
        Else
            lblError.Text = drUserInfo("userID")
            'Session("userID") = drUserInfo("userID")
            'Session("userType") = drUserInfo("userType")
            'Session("name") = drUserInfo("firstName") & " " & drUserInfo("lastName")
            'Session("email") = drUserInfo("email")
            'Session("custNo") = drUserInfo("custNo")
            'Session("viewOrders") = drUserInfo("viewOrders")
            'Session("createOrders") = drUserInfo("createOrders")
            'Session("makeRequests") = drUserInfo("makeRequests")
            'Session("makeApprovals") = drUserInfo("makeApprovals")

            FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, False)
        End If
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
b1xml2Commented:
Try
    connUsers.Open()
    cmdUserInfo.Parameters.Add("@userID", Trim(txtUsername.Text))
    cmdUserInfo.Parameters.Add("@password", Trim(txtPassword.Text))
    drUserInfo = cmdUserInfo.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As Exception
    lblError.Text = "Error connecting to server."
End Try

If drUserInfo Is Nothing Or drUserInfo.HasRows = False Then
      lblError.Text = "Invalid username or password."
      drUserInfo.Close
ElseIf drUserInfo.Read Then
      lblError.Text = drUserInfo("userID")
      'Session("userID") = drUserInfo("userID")
      'Session("userType") = drUserInfo("userType")
      'Session("name") = drUserInfo("firstName") & " " & drUserInfo("lastName")
      'Session("email") = drUserInfo("email")
      'Session("custNo") = drUserInfo("custNo")
      'Session("viewOrders") = drUserInfo("viewOrders")
      'Session("createOrders") = drUserInfo("createOrders")
      'Session("makeRequests") = drUserInfo("makeRequests")
      'Session("makeApprovals") = drUserInfo("makeApprovals")
      drUserInfo.Close()
      FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, False)
End If


0
 
NeoTekAuthor Commented:
Sill no luck...

The error is with this line...

lblError.Text = drUserInfo("userID")
0
 
NeoTekAuthor Commented:
-=Raised Point Level=-
0
 
b1xml2Commented:
lblError.Text = drUserInfo.GetValue(drUserInfo.GetOrdinal("UserId")).ToString

0
 
amyhxuCommented:
What exactly is the error?
This doesn't seem to be the case here. However, just in case...
If Not IsDbNull(drUserInfo("userID")) Then
    lblError.Text = drUserInfo("userID")
End If
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.