[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

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?
0
NeoTek
Asked:
NeoTek
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
doobdaveCommented:
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now