Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo
Comparing datareader result to a string (Tuesday, September 14, 2004)
 

Found the following interesting discussion in the Newsgroups:

Comparing datareader result to a string
by:Anonymous

I'm a newbie to VB.NET and ASP.NET (as you'll see by my code). However, I'm not able to get a simple process to work. I'm trying to retrieve a record and then check to see if the 'accountstat' column of the DB is set to 'inactive'. I can't, however, get it to compare correctly. Here's the code:

Sub Sign_In(s As Object, e As EventArgs)
If IsValid Then
Dim conPubs As SqlConnection
Dim cmdSelectRecord As SqlCommand
Dim dtrUser As SqlDataReader

conPubs = New SqlConnection("Server=moses;UID=sa;PWD=webdev;Database=testDB")
conPubs.Open()

cmdSelectRecord = new SqlCommand("Select * from siteusers where email='" & txtUsername.text & "' and password='"& txtPassword.Text &"'", conPubs)
dtrUser = cmdSelectRecord.ExecuteReader()

If dtrUser.Read Then
If dtrUser("accountstat") = "inactive" Then
txtInvalid.text = "Your account has not been activated. Follow the instructions sent to you via email to activate your account."
Else
' Session("uid") = dtrUser("uid")
' Response.Redirect(Session("Referer"))
End If
Else
txtInvalid.text = "Invalid email address and/or password"
session("forgotEmail")=txtUsername.text
End If

dtrUser.Close()
conPubs.Close()
End If
End Sub


 Reply:
by:William Ryan eMVP

 Read is pushing the pointer up one. If you are just checking for one value,
use an Output parameter or executescalar. you can use it like SELECT Email
from whatever
...then just use Dim s as String = cmd.ExecuteScalar . If the query returned
bill@somewhere.net then that's what the string would be. Assuming that you
really want a datareader though, get rid of that .Read statement, b/c at
most, you'll only be dealing with one row. If you are using the 1.1
framework, replace it with.

If dtrUser.HasRows Then
While dr.Read
If CType(dtrUser("accountstat"), String) = "inactive" Then
txtInvalid.text = "Your account has not been activated. Follow the
instructions sent to you via email to activate your account."
End While
End If

Now,first off, get rid of the name based lookup . It's slow and wasteful.
Either use the getordinal method

Dim i as Integer = dtrUser.GetOrdinal("accountstat") and then reference this
with
dtrUser(i)
or just use the index. The index will be the position the field is in the
query so the first field will always be 0.

Now, TURN ON OPTION STRICT! This code shouldn't even compile and C#
wouldn't let this run. Option Strict Off = Option Slow ON and Option Bugs
On

this should actually read dtrUser(0).GetString("accountstat") ' or the index
you want to reference instead. Now the logic is still in question but this
is the crux of the problem. I'd also use some breakpoints and assertions
just to see what's happening... but before you do anything else, Turn On
Option Strict... trust me on this.

If you have any specific problems, let me know, we'll get you through it but
this should fix it.

Cheers,

Bill



Posted by Xander Zelders



 
Previous Posts
    - Dataviews and constrained datasources?
    - Windows service not releasing object
    - How to Shut Down Server
    - How to wrap SendMessage
    - Passing BackColor Values Between .NET and VB6 appl...
    - Scroll Richtextbox automatically
    - FileOpen file permission problem
    - TreeView
    - How to Broadcast a message
    - Upgrading or using the Online Resources Tap

Archives
    - 08/01/2004 - 08/08/2004
    - 08/08/2004 - 08/15/2004
    - 08/15/2004 - 08/22/2004
    - 08/22/2004 - 08/29/2004
    - 08/29/2004 - 09/05/2004
    - 09/05/2004 - 09/12/2004
    - 09/12/2004 - 09/19/2004
    - 09/19/2004 - 09/26/2004
    - 09/26/2004 - 10/03/2004
    - 10/03/2004 - 10/10/2004
    - 01/02/2005 - 01/09/2005
    - 01/09/2005 - 01/16/2005
    - 01/30/2005 - 02/06/2005
    - 01/01/2006 - 01/08/2006


Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland