473,382 Members | 1,258 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Comparing datareader result to a string

JKM
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 The
Dim conPubs As SqlConnectio
Dim cmdSelectRecord As SqlComman
Dim dtrUser As SqlDataReade

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

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

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

dtrUser.Close(
conPubs.Close(
End I
End Sub
Nov 20 '05 #1
1 1822
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
bi**@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
"JKM" <an*******@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
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;Data base=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

Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
6
by: Grant | last post by:
I am connecting to an access database using a datareader in C#. I get results when I run a certain query from Access but when I run it from Code it does not retrieve any results. I have put a stop...
2
by: Jake S | last post by:
Hi all, Is it possibele to set the datasource of a dropdown list to a datareader? When I try to the only column I receive is a column populated by the datasource name repeated the amount of...
2
by: Martin Raychev | last post by:
Hi all, I have the following problem: I have a private method that returns a SqlDataReader. For this to work I have not to close the DB connection in the above method. I do this only to
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
3
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
2
by: Mattyw | last post by:
Hi I have a sqlcommand that returns all the rows in a column and then pass that to a datareader. I am new to VS.Net and so far I can only return the first row in the first column using ...
15
by: Rob Nicholson | last post by:
I'm starting to worry a bit now. We're getting the above error when two users hit the same database/page on an ASP.NET application using ADO.NET, talking to a SQL 7 server. The error is perfectly...
2
by: orencs | last post by:
Hi, I am using Datareader and stored procedure in C# ADO.NET. When I am running the stored procedure in the SQL Query Analyzer and recieve two rows (as I hace expected) col1 col2 0 1 0 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.