Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sqlclient.hasrows not functioning properly....or bad code?

Status
Not open for further replies.

etjohnson81

Programmer
Jul 17, 2006
72
US
The code below is what I am using to determine if credentials were good.

Code:
Try
            While sqlOutput.Read

                If sqlOutput.HasRows = False Then
                    lblErrorMessage.Text = "You don't have access to this site.  Please contact your administrator."
                    lblErrorMessage.Visible = True
                End If

                If sqlOutput.HasRows = True Then
                    lblErrorMessage.Text = "Login Success"
                    lblErrorMessage.Visible = True

                    'Load user information

                End If
            End While
        Catch ex As Exception
            lblErrorMessage.Text = "An Error Occured During Login"
        End Try

When the table has records (hasrows = true)... The Login Success is shown.

When there are no records (hasrows = false)...Nothing is displayed.

I had this working previously with slightly different code that used a Count(*) in the SQL String, but I would like to run only one SQL read if possible.

Anyone know why this might not work?

Travis
 
I assume you are using a datareader here. I think you need to check the .HasRows property before you .read.

Check some examples in VS Help.
 
I get back the proper value from the datareader.hasrows, I did a test display of the output in a label field and it returns properly, but not when i output during the false portion of the if, only during the true piece

Travis
 
If there are no records, it doesn't enter the loop ergo no values on false.

Elementary program control flow, my dear Watson.

A simple step through would have shown you this.

C
 
I had this working previously with slightly different code that used a Count(*) in the SQL String, but I would like to run only one SQL read if possible.
what's wrong with 2 sql queries? I doubt there is a noticable and/or quantifiable difference between the previous and proposed query plans.

I would also make the recommendation to exit the connection asap. load the reader into a datatable, close/dispose the connection and then run your logic/gui. according to the code above you never clean up after the connection. either use a finally block, or a using statement. both would do the same to.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top