Friday, March 16, 2012

Login program failure

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>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
"Cemal Karademir" <c.karademir@.zonnet.nl> wrote in message
news:OncOaWGgEHA.904@.TK2MSFTNGP09.phx.gbl...
> 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>
>
Hi Karl,
Works great, thanx a lot, except why do i have to click twice the Login
Button?
thanx Cemal
"Karl" <none> schreef in bericht
news:Oxgi6mGgEHA.3988@.tk2msftngp13.phx.gbl...
> 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
>
> "Cemal Karademir" <c.karademir@.zonnet.nl> wrote in message
> news:OncOaWGgEHA.904@.TK2MSFTNGP09.phx.gbl...
> MS-Access
wrong
runat="server"
>
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?
"Cemal Karademir" <c.karademir@.zonnet.nl> wrote in message
news:uqPLZLHgEHA.632@.TK2MSFTNGP12.phx.gbl...
> Hi Karl,
> Works great, thanx a lot, except why do i have to click twice the Login
> Button?
> thanx Cemal
> "Karl" <none> schreef in bericht
> news:Oxgi6mGgEHA.3988@.tk2msftngp13.phx.gbl...
page
it
the
> their
> '"
> wrong
> runat="server"
>
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.
"Karl" <none> schreef in bericht
news:O7jqhdHgEHA.3964@.TK2MSFTNGP12.phx.gbl...
> 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?
> "Cemal Karademir" <c.karademir@.zonnet.nl> wrote in message
> news:uqPLZLHgEHA.632@.TK2MSFTNGP12.phx.gbl...
> page
store
> it
the
> the
=
DB
runat="server">lblStatus</asp:Label>
>
When I click an other button (without any code behind it), it also works.
"Karl" <none> schreef in bericht
news:O7jqhdHgEHA.3964@.TK2MSFTNGP12.phx.gbl...
> 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?
> "Cemal Karademir" <c.karademir@.zonnet.nl> wrote in message
> news:uqPLZLHgEHA.632@.TK2MSFTNGP12.phx.gbl...
> page
store
> it
the
> the
=
DB
runat="server">lblStatus</asp:Label>
>
Code would help..otherwise, if you are able to debug, you should be able to
find the problem fairly easily.
Karl
"Cemal Karademir" <c.karademir@.zonnet.nl> wrote in message
news:Obw0wiHgEHA.636@.TK2MSFTNGP12.phx.gbl...
> 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.
> "Karl" <none> schreef in bericht
> news:O7jqhdHgEHA.3964@.TK2MSFTNGP12.phx.gbl...
> or
simply
Login
> store
to
> the
have
select
Password
> =
something
Ole
> DB
iUser
> runat="server">lblStatus</asp:Label>
>
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>
"Karl" <none> schreef in bericht
news:Oloi28HgEHA.3416@.TK2MSFTNGP09.phx.gbl...
> Code would help..otherwise, if you are able to debug, you should be able
to
> find the problem fairly easily.
> Karl
> "Cemal Karademir" <c.karademir@.zonnet.nl> wrote in message
> news:Obw0wiHgEHA.636@.TK2MSFTNGP12.phx.gbl...
don't
time?
> simply
> Login
the
back
> to
so
> have
> select
> Password
an
> something
> Ole
EventArgs)
> iUser
>
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>
"Karl" <none> schreef in bericht
news:Oloi28HgEHA.3416@.TK2MSFTNGP09.phx.gbl...
> Code would help..otherwise, if you are able to debug, you should be able
to
> find the problem fairly easily.
> Karl
> "Cemal Karademir" <c.karademir@.zonnet.nl> wrote in message
> news:Obw0wiHgEHA.636@.TK2MSFTNGP12.phx.gbl...
don't
time?
> simply
> Login
the
back
> to
so
> have
> select
> Password
an
> something
> Ole
EventArgs)
> iUser
>

0 comments:

Post a Comment