?
Solved

Data Reader No Record Found Logic

Posted on 2006-03-30
4
Medium Priority
?
303 Views
Last Modified: 2012-05-05
Hi,

I’m using ASP.NET 2005 with VB.NET and SQL Server 2005.  The following code logic isn’t working when a record is not found:

While Rdr.Read
            If Rdr(2) <> Me.txtUserID.Text Then          ‘Record Not Found
                Me.lblStatus.Text = "Invalid User"
            Else
                Me.lblStatus.Text = "Welcome " & Rdr(2)
            End If
 End While

How can I fix this code or is there a better way to check if a record exists using the SQL Data Reader?

******Additional Information**********

Users Table:
UserID      UserLastName      UserFirstName      UsesrLogIn         UserPassword
1      Goodheart                      Denise                      Denise         DeniseG

Stored Procedure:
Create PROCEDURE [dbo].[spSelLogIn]
@UserLogIn as varchar(50),
@UserPassword as varchar(50)
As
Select UserID,UserLogIn, UserPassword from Users
WHERE @UserLogIn=UserLogIn and @UserPassword=UserPassword

Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnter.Click
        Dim sSQL As String
        Dim sCon As String
        Dim sLoginID As String
        Dim sPassword As String

        sLoginID = "denise"
        sPassword = "deniseg"
        sCon = "Data Source=Gizmo;Initial Catalog=SandBox;User Id=" & sLoginID & ";Password=" & sPassword & ";"

        Dim Con As SqlConnection = New SqlConnection(sCon)

        'Create sSQL String
        sLoginID = Me.txtUserID.Text
        sPassword = Me.txtPassword.Text
        sSQL = "EXEC dbo.spSelLogIn '" & sLoginID & "','" & sPassword & "'"

        Dim Cmd As SqlCommand = New SqlCommand(sSQL, Con)
        Cmd.Connection.Open()
        Dim Rdr As SqlDataReader = Cmd.ExecuteReader()

        While Rdr.Read
            If Rdr(2) <> Me.txtUserID.Text Then
                Me.lblStatus.Text = "Invalid User"
            Else
                Me.lblStatus.Text = "Welcome " & Rdr(2)
            End If
        End While

        Cmd.Connection.Close()
End Sub

Thanks,
Denise
0
Comment
Question by:DeniseGoodheart
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
appari earned 1920 total points
ID: 16330906
try

if rdr.hasrows then
While Rdr.Read
            If Rdr(2) <> Me.txtUserID.Text Then
                Me.lblStatus.Text = "Invalid User"
            Else
                Me.lblStatus.Text = "Welcome " & Rdr(2)
            End If
End While
else
       Me.lblStatus.Text = "Invalid User"

end if
0
 
LVL 7

Assisted Solution

by:irps20001
irps20001 earned 80 total points
ID: 16331110
Hi
In my observation, You are comparing Rdr(2) with UserId.
Rdr(2) is representing PASSWORD from users table and you are comparing PASSWORD from database with the UserID that has entered from the Form.
I guess you have to change your code from Rdr(2) to Rdr(1) where Rdr(1) represents UserId/LoginID

Apart from that , your code is working Fine

HTH
Happy Programming
Rana
0
 

Author Comment

by:DeniseGoodheart
ID: 16334012
Hi Rana,

You are absolutely right about my incorrect test data.  My test data is actually Denise as the UserID and Denise as the UserPassword, and I accidently put DeniseG as the UserPassword for my question.  Anyway, your suggestion did not fix the problem, but raised a good point about using realistic test data.  My test data worked because all my values are “Denise.”

Thanks,
Denise
0
 

Author Comment

by:DeniseGoodheart
ID: 16334023
Hi appari,

Your code worked great!

Many Thanks,
Denise
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question