Login program failure

C

Cemal Karademir

Hi,

I'm workin on a small Login page/program with asp.net. It uses an MS-Access
database that looks like this:
USERID | NAME | PASSWORD
_________________________________
1 | John | hello
2 | Paul | hello2

I don't understand why it doesn't work. I think i'm doing something wrong
with the client/server side code.

I use WebMatrix for this. The code looks like this:

dim Conn as OleDbConnection
dim ConnString as String="Provider=Microsoft.Jet.OLEDB.4.0; Ole DB
Services=-4; Data Source=C:\MyDb.mdb"
dim SQL as String="Select NAME, USERID from USERS"
dim dtrUsers as OleDbDataReader
dim cmdSelectUser as OleDbCommand
dim sPassWord as string

Sub Page_Load
if not IsPostBack then
Conn = New OleDbConnection (ConnString)
SQL = "Select NAME, USERID from USERS"
cmdSelectUser = New OleDbCommand (SQL, Conn)
Conn.Open()
dtrUsers = cmdSelectUser.ExecuteReader()
drpUsers.DataSource = dtrUsers
drpUsers.DataTextField = "NAME"
drpUsers.DataValueField = "USERID"
drpUsers.DataBind()
drpUsers.Items.Insert(0, New ListItem("`Select
Name...", -1))
dtrUsers.Close()
Conn.Close()
end if
End Sub

Sub drpUsers_SelectedIndexChanged(s As Object, e As EventArgs)
dim iUser as integer = drpUsers.SelectedItem.Value
if iUser <> -1 then
SQL="Select PASSWORD from USERS where USERID = " & iUser
Conn = New OleDbConnection (ConnString)
Conn.Open()
cmdSelectUser = New OleDbCommand (SQL, Conn)
dtrUsers = cmdSelectUser.ExecuteReader()
do while dtrUsers.Read()
sPassword= dtrUsers("PASSWORD")
Loop
dtrUsers.Close()
Conn.Close()
end if
End Sub

Sub btnLogIn_Click(sender As Object, e As EventArgs)
If sPassword = txtPassWord.text then
lblStatus.Text = "OK"
else
lblStatus.Text = "NOT OK"
end if
End Sub

The interface looks like this:

<form runat="server">
<asp:DropDownList id="drpUsers" runat="server"
OnSelectedIndexChanged="drpUsers_SelectedIndexChanged"
Width="141px"></asp:DropDownList>
<br />
<asp:TextBox id="txtPassWord" runat="server"
AutoPostBack="True"></asp:TextBox>
<br />
<asp:Button id="btnLogIn" onclick="btnLogIn_Click" runat="server"
Text="Verder"></asp:Button>
<br />
<asp:Label id="lblStatus" runat="server">lblStatus</asp:Label>
</form>
 
K

Karl

Your sPassword variable will be blank. When the page loads, the
dropdownlist is populated, the user selects his or her username, the page
posts back to itself and the password is retrieved, but you don't store it
anywhere. When the user hits the LogIn button, the page posts back to
itself again, the drpUsers_SelectedIndexChanged method WON'T fire so the
password won't get reset.

Lose the entire drpUsers_SelectedIndexChanged function and don't have the
form postback when a username is selected. Instead, let users select their
username and password, then in btnLogIn_Click do something like:

dim iUser as integer = drpUsers.SelectedItem.Value
if iUser <> -1 then
SQL="Select * from USERS where USERID = " & iUser & " AND Password = '"
& txtPassWord.text & "'"
Conn = New OleDbConnection (ConnString)
Conn.Open()
cmdSelectUser = New OleDbCommand (SQL, Conn)
dtrUsers = cmdSelectUser.ExecuteReader()
if dtrUsers.Read then
'THE userId and password match, valid user
else
'invalid user
end if
dtrUsers.Close()
Conn.Close()
end
 
C

Cemal Karademir

Hi Karl,

Works great, thanx a lot, except why do i have to click twice the Login
Button?

thanx Cemal
 
K

Karl

You are saying nothing happens when you click the button the first time? or
does it postback and give an invalid user but work the 2nd time by simply
clicking again?
 
C

Cemal Karademir

The first time I click nothing happens. The second time it works. I don't
understand why that is. I can pass the code if you want.
 
K

Karl

Code would help..otherwise, if you are able to debug, you should be able to
find the problem fairly easily.

Karl
 
C

Cemal Karademir

Here's the code.

When I click btnClose (there is no code behind it) and then btn LogIn it
works. Or I have to click btnLogIn twice. Maybe because the set focus loses?

Thanx, Cemal

<%@ Page Language="VB" Debug="TRUE" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

dim Conn as OleDbConnection
dim ConnString as String="Provider=Microsoft.Jet.OLEDB.4.0; Ole DB
Services=-4; Data Source=C:\MyDb.mdb"
dim SQL as String="Select NAME, USERID from USERS"
dim dtrUsers as OleDbDataReader
dim cmdSelectUser as OleDbCommand

Sub Page_Load
if Not IsPostBack then
Conn = New OleDbConnection (ConnString)
SQL = "Select NAME, USERID from USERS"
cmdSelectUser = New OleDbCommand (SQL, Conn)
Conn.Open()
dtrUsers = cmdSelectUser.ExecuteReader()
drpUsers.DataSource = dtrUsers
drpUsers.DataTextField = "NAME"
drpUsers.DataValueField = "USERID"
drpUsers.DataBind()
drpUsers.Items.Insert(0, New ListItem("Select Name...", -1))
dtrUsers.Close()
Conn.Close()
end if
End Sub

Sub btnLogIn_Click(sender As Object, e As EventArgs)
' I have to click this button twice???
dim iUser as integer = drpUsers.SelectedItem.Value
if iUser <> -1 then
SQL="Select * from USERS where USERID = " & iUser & " AND Password =
'" & txtPassWord.text & "'"
Conn = New OleDbConnection (ConnString)
Conn.Open()
cmdSelectUser = New OleDbCommand (SQL, Conn)
dtrUsers = cmdSelectUser.ExecuteReader()
if dtrUsers.Read then
Response.Redirect("NextPage.aspx")
end if
dtrUsers.Close()
Conn.Close()
end if
End Sub

Sub btnClose_Click(sender As Object, e As EventArgs)
' There is no code !!!
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DropDownList id="drpUsers" runat="server"
Width="141px"></asp:DropDownList>
<br />
<asp:TextBox id="txtPassWord" runat="server"
AutoPostBack="True"></asp:TextBox>
<br />
<asp:Button id="btnLogIn" onclick="btnLogIn_Click" runat="server"
Text="Verder"></asp:Button>
<br />
<asp:Button id="btnClose" onclick="btnClose_Click" runat="server"
Text="Afsluiten"></asp:Button>
<br />
<asp:Label id="lblUser" runat="server">lblUser</asp:Label>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top