anasalama
asked on
login form - urgent please
hi there..
i need to build a login form in asp.net using vb.net and sql server db.
the form contains (id and password) when user enters his data the asp.net code must check if he has a record .
if the user flag=1 he will be redirected to file1.aspx
if the user flag=2 he will be redirected to file2.aspx
if he has no record he will be redirected to exit.aspx
(i need full code please if possible because basicly i am classic asp programmer and this is required by asp.net)
this is the table structure (users).
-------------------------- ---------- ---------
id varchar 50
password varchar 50
flag char 1
-------------------------- ---------- ---------
i need to build a login form in asp.net using vb.net and sql server db.
the form contains (id and password) when user enters his data the asp.net code must check if he has a record .
if the user flag=1 he will be redirected to file1.aspx
if the user flag=2 he will be redirected to file2.aspx
if he has no record he will be redirected to exit.aspx
(i need full code please if possible because basicly i am classic asp programmer and this is required by asp.net)
this is the table structure (users).
--------------------------
id varchar 50
password varchar 50
flag char 1
--------------------------
ASKER
Can you write it for 110 points ?
This all waht i have
This all waht i have
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok Torrwin .. i tried the code after making some changes ..
but i got this error :
-------------------------- ---------- ---------- ---------- ---------- ---------- -------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30506: Handles clause requires a WithEvents variable.
Source Error:
Line 13: Dim FoundPW As String
Line 14: Dim numtries As Integer = 0
Line 15: Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Line 16:
Line 17: CS = "Server=127.0.0.1;initial catalog=latham;uid=sa;pwd= ;"
Source File: C:\Inetpub\wwwroot\loginpa ge\login.a spx Line: 15
-------------------------- ---------- ---------- ---------- ---------- ---------- -------
This is the code :
-------------------------- ---------- ---------- ---------- ---------- ---------- -------
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Sql Client" %>
<html>
<head>
<title>Login Form ...</title>
<script runat="server">
Dim CS As String
Dim myConn As SqlClient.SqlConnection
Dim myCommand As SqlClient.SqlCommand
Dim myAdapter As SqlClient.SqlDataAdapter
Dim dsAdmin As DataSet
Dim FoundPW As String
Dim numtries As Integer = 0
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
CS = "Server=127.0.0.1;initial catalog=latham;uid=sa;pwd= ;"
myConn = New SqlClient.SqlConnection()
myConn.ConnectionString = CS
myConn.Open()
myCommand = New SqlClient.SqlCommand()
myCommand.Connection = myConn
myCommand.CommandText = "select * from users where ID = '" & txtUserName.Text & "'"
myAdapter = New SqlClient.SqlDataAdapter()
myAdapter.SelectCommand = myCommand
dsAdmin = New DataSet()
myAdapter.Fill(dsAdmin, "admin")
If dsAdmin.Tables(0).Rows.Cou nt = 0 Then
Response.Redirect("exit.as px")
End If
FoundPW = dsAdmin.Tables(0).Rows(0). Item("pass word")
FoundPW = FoundPW.TrimEnd
If FoundPW = txtPW.Text Then
Session("Userid") = txtUserName.Text
Session("Password") = txtPW.Text
If dsAdmin.Tables(0).Rows(0). Item("flag ") = 1
Response.Redirect("file1.a spx")
ElseIf dsAdmin.Tables(0).Rows(0). Item("flag ") = 2
Response.Redirect("file2.a spx")
Else
Response.Redirect("exit.as px")
Else
txtUserName.Text = ""
txtPW.Text = ""
lblSuccess.Text = "Invalid UserName or Password."
numtries += 1
If numtries = 3 Then
numtries = 0
Response.Redirect("exit.as px")
End If
End If
End Sub
</script>
<style type="text/css">
BODY {
scrollbar-3dlight-color:bl ack;
scrollbar-arrow-color:whit e;
scrollbar-base-color:RGB(7 5,75,150);
scrollbar-track-color:whit e;
scrollbar-darkshadow-color :white;
scrollbar-face-color:RGB(7 5,75,150);
scrollbar-highlight-color: RGB(75,75, 150);
scrollbar-shadow-color:bla ck }
</style>
</head>
<body ms_positioning="GridLayout ">
<form id="Form1" method="post" runat="server">
<asp:label id="Label5" runat="server" font-names="Tahoma" font-size="X-Large" style="LEFT: 54px; POSITION: absolute; TOP: 24px">ID :</asp:label>
<asp:textbox id="txtUserName" runat="server" font-size="X-Small" font-names="Tahoma" width="174px" height="24px" tabindex="1" style="LEFT: 136px; POSITION: absolute; TOP: 114px"></asp:textbox>
<asp:label id="Label1" runat="server" font-names="Tahoma" font-size="X-Small" style="LEFT: 54px; POSITION: absolute; TOP: 119px">Password :</asp:label>
<asp:textbox id="txtPW" runat="server" font-size="X-Small" font-names="Tahoma" width="174" height="24" tabindex="2" style="LEFT: 134px; POSITION: absolute; TOP: 155px"></asp:textbox>
<asp:button id="btnSend" onclick="btnSubmit_Click" runat="server" font-names="Tahoma" text="Send" backcolor="Navy" tabindex="5" font-size="X-Small" forecolor="White" style="LEFT: 212px; POSITION: absolute; TOP: 328px"></asp:button>
</form>
</body>
</html>
-------------------------- ---------- ---------- ---------- ---------- ---------- -------
When i removed (Handles btnSubmit.Click) i got another if-else error :
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30081: 'If' must end with a matching 'End If'.
Source Error:
Line 38: FoundPW = FoundPW.TrimEnd
Line 39:
Line 40: If FoundPW = txtPW.Text Then
Line 41: Session("Userid") = txtUserName.Text
Line 42: Session("Password") = txtPW.Text
Source File: C:\Inetpub\wwwroot\loginpa ge\login.a spx Line: 40
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---
Any idea ?
but i got this error :
--------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30506: Handles clause requires a WithEvents variable.
Source Error:
Line 13: Dim FoundPW As String
Line 14: Dim numtries As Integer = 0
Line 15: Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Line 16:
Line 17: CS = "Server=127.0.0.1;initial catalog=latham;uid=sa;pwd=
Source File: C:\Inetpub\wwwroot\loginpa
--------------------------
This is the code :
--------------------------
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Sql
<html>
<head>
<title>Login Form ...</title>
<script runat="server">
Dim CS As String
Dim myConn As SqlClient.SqlConnection
Dim myCommand As SqlClient.SqlCommand
Dim myAdapter As SqlClient.SqlDataAdapter
Dim dsAdmin As DataSet
Dim FoundPW As String
Dim numtries As Integer = 0
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
CS = "Server=127.0.0.1;initial catalog=latham;uid=sa;pwd=
myConn = New SqlClient.SqlConnection()
myConn.ConnectionString = CS
myConn.Open()
myCommand = New SqlClient.SqlCommand()
myCommand.Connection = myConn
myCommand.CommandText = "select * from users where ID = '" & txtUserName.Text & "'"
myAdapter = New SqlClient.SqlDataAdapter()
myAdapter.SelectCommand = myCommand
dsAdmin = New DataSet()
myAdapter.Fill(dsAdmin, "admin")
If dsAdmin.Tables(0).Rows.Cou
Response.Redirect("exit.as
End If
FoundPW = dsAdmin.Tables(0).Rows(0).
FoundPW = FoundPW.TrimEnd
If FoundPW = txtPW.Text Then
Session("Userid") = txtUserName.Text
Session("Password") = txtPW.Text
If dsAdmin.Tables(0).Rows(0).
Response.Redirect("file1.a
ElseIf dsAdmin.Tables(0).Rows(0).
Response.Redirect("file2.a
Else
Response.Redirect("exit.as
Else
txtUserName.Text = ""
txtPW.Text = ""
lblSuccess.Text = "Invalid UserName or Password."
numtries += 1
If numtries = 3 Then
numtries = 0
Response.Redirect("exit.as
End If
End If
End Sub
</script>
<style type="text/css">
BODY {
scrollbar-3dlight-color:bl
scrollbar-arrow-color:whit
scrollbar-base-color:RGB(7
scrollbar-track-color:whit
scrollbar-darkshadow-color
scrollbar-face-color:RGB(7
scrollbar-highlight-color:
scrollbar-shadow-color:bla
</style>
</head>
<body ms_positioning="GridLayout
<form id="Form1" method="post" runat="server">
<asp:label id="Label5" runat="server" font-names="Tahoma" font-size="X-Large" style="LEFT: 54px; POSITION: absolute; TOP: 24px">ID :</asp:label>
<asp:textbox id="txtUserName" runat="server" font-size="X-Small" font-names="Tahoma" width="174px" height="24px" tabindex="1" style="LEFT: 136px; POSITION: absolute; TOP: 114px"></asp:textbox>
<asp:label id="Label1" runat="server" font-names="Tahoma" font-size="X-Small" style="LEFT: 54px; POSITION: absolute; TOP: 119px">Password :</asp:label>
<asp:textbox id="txtPW" runat="server" font-size="X-Small" font-names="Tahoma" width="174" height="24" tabindex="2" style="LEFT: 134px; POSITION: absolute; TOP: 155px"></asp:textbox>
<asp:button id="btnSend" onclick="btnSubmit_Click" runat="server" font-names="Tahoma" text="Send" backcolor="Navy" tabindex="5" font-size="X-Small" forecolor="White" style="LEFT: 212px; POSITION: absolute; TOP: 328px"></asp:button>
</form>
</body>
</html>
--------------------------
When i removed (Handles btnSubmit.Click) i got another if-else error :
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30081: 'If' must end with a matching 'End If'.
Source Error:
Line 38: FoundPW = FoundPW.TrimEnd
Line 39:
Line 40: If FoundPW = txtPW.Text Then
Line 41: Session("Userid") = txtUserName.Text
Line 42: Session("Password") = txtPW.Text
Source File: C:\Inetpub\wwwroot\loginpa
--------------------------
Any idea ?
Count up your if statements, you probably deleted an if or end if statement and forgot to also delete its counterpart.
If FoundPW = txtPW.Text Then
Session("Userid") = txtUserName.Text
Session("Password") = txtPW.Text
If dsAdmin.Tables(0).Rows(0). Item("flag ") = 1
Response.Redirect("file1.a spx")
ElseIf dsAdmin.Tables(0).Rows(0). Item("flag ") = 2
Response.Redirect("file2.a spx")
Else
Response.Redirect("exit.as px")
Else
txtUserName.Text = ""
txtPW.Text = ""
lblSuccess.Text = "Invalid UserName or Password."
numtries += 1
If numtries = 3 Then
numtries = 0
Response.Redirect("exit.as px")
End If
End If
If FoundPW = txtPW.Text Then
Session("Userid") = txtUserName.Text
Session("Password") = txtPW.Text
If dsAdmin.Tables(0).Rows(0).
Response.Redirect("file1.a
ElseIf dsAdmin.Tables(0).Rows(0).
Response.Redirect("file2.a
Else
Response.Redirect("exit.as
Else
txtUserName.Text = ""
txtPW.Text = ""
lblSuccess.Text = "Invalid UserName or Password."
numtries += 1
If numtries = 3 Then
numtries = 0
Response.Redirect("exit.as
End If
End If
ASKER
would you try your code in your machine ?
i did not delete any 'if' or 'end if'
i did not delete any 'if' or 'end if'
ASKER
When i remove this part the code runs well but "Invalid UserName or Password" does not appear.!!
The problem is in this segment "
-------------------------- ---------- ---------- ---------- ---------- ---------- -
Else
txtUserName.Text = ""
txtPW.Text = ""
lblSuccess.Text = "Invalid UserName or Password."
numtries += 1
-------------------------- ---------- ---------- ---------- ---------- ---------- -
The problem is in this segment "
--------------------------
Else
txtUserName.Text = ""
txtPW.Text = ""
lblSuccess.Text = "Invalid UserName or Password."
numtries += 1
--------------------------
I copied this code directly from my page so I know it is in working order. Well, except I changed the server and table names.
Are you trying to run this as a script? Right click on your form in the solution explorer and choose "view code". Thats where the code should be placed.
Are you trying to run this as a script? Right click on your form in the solution explorer and choose "view code". Thats where the code should be placed.
ASKER
I don't know what are you talking about !?
of course I don't try to run the only vb script in my browser !
did you read my first reply ?
I made some changes .. now the vb code + html exist in the same file .
there is problem with your if - else statements.
would you run this file in your computer please and tell the result(just change the connection and the db(
-------------------------- ---------- ---------- ---------- ---------- ---------- -----
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Sql Client" %>
<html>
<head>
<title>Login Form ...</title>
<script runat="server">
Dim CS As String
Dim myConn As SqlClient.SqlConnection
Dim myCommand As SqlClient.SqlCommand
Dim myAdapter As SqlClient.SqlDataAdapter
Dim dsAdmin As DataSet
Dim FoundPW As String
Dim numtries As Integer = 0
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handles btnSubmit.Click
CS = "Server=127.0.0.1;initial catalog=latham;uid=sa;pwd= ;"
myConn = New SqlClient.SqlConnection()
myConn.ConnectionString = CS
myConn.Open()
myCommand = New SqlClient.SqlCommand()
myCommand.Connection = myConn
myCommand.CommandText = "select * from users where ID = '" & txtUserName.Text & "'"
myAdapter = New SqlClient.SqlDataAdapter()
myAdapter.SelectCommand = myCommand
dsAdmin = New DataSet()
myAdapter.Fill(dsAdmin, "admin")
If dsAdmin.Tables(0).Rows.Cou nt = 0 Then
Response.Redirect("exit.as px")
End If
FoundPW = dsAdmin.Tables(0).Rows(0). Item("pass word")
FoundPW = FoundPW.TrimEnd
If FoundPW = txtPW.Text Then
Session("Userid") = txtUserName.Text
Session("Password") = txtPW.Text
If dsAdmin.Tables(0).Rows(0). Item("flag ") = 1
Response.Redirect("file1.a spx")
ElseIf dsAdmin.Tables(0).Rows(0). Item("flag ") = 2
Response.Redirect("file2.a spx")
Else
Response.Redirect("exit.as px")
Else
txtUserName.Text = ""
txtPW.Text = ""
lblSuccess.Text = "Invalid UserName or Password."
numtries += 1
end if
If numtries = 3 Then
numtries = 0
Response.Redirect("exit.as px")
End If
End If
End Sub
</script>
<style type="text/css">
BODY {
scrollbar-3dlight-color:bl ack;
scrollbar-arrow-color:whit e;
scrollbar-base-color:RGB(7 5,75,150);
scrollbar-track-color:whit e;
scrollbar-darkshadow-color :white;
scrollbar-face-color:RGB(7 5,75,150);
scrollbar-highlight-color: RGB(75,75, 150);
scrollbar-shadow-color:bla ck }
</style>
</head>
<body ms_positioning="GridLayout ">
<form id="Form1" method="post" runat="server">
<asp:label id="Label5" runat="server" font-names="Tahoma" font-size="X-Large" style="LEFT: 54px; POSITION: absolute; TOP: 24px">ID :</asp:label>
<asp:textbox id="txtUserName" runat="server" font-size="X-Small" font-names="Tahoma" width="174px" height="24px" tabindex="1" style="LEFT: 136px; POSITION: absolute; TOP: 114px"></asp:textbox>
<asp:label id="Label1" runat="server" font-names="Tahoma" font-size="X-Small" style="LEFT: 54px; POSITION: absolute; TOP: 119px">Password :</asp:label>
<asp:textbox id="txtPW" runat="server" font-size="X-Small" font-names="Tahoma" width="174" height="24" tabindex="2" style="LEFT: 134px; POSITION: absolute; TOP: 155px"></asp:textbox>
<asp:label id="lblSuccess" runat="server" font-names="Tahoma" font-size="X-Small" style="LEFT: 54px; POSITION: absolute; TOP: 119px">Password :</asp:label>
<asp:button id="btnSend" onclick="btnSubmit_Click" runat="server" font-names="Tahoma" text="Send" backcolor="Navy" tabindex="5" font-size="X-Small" forecolor="White" style="LEFT: 212px; POSITION: absolute; TOP: 328px"></asp:button>
</form>
</body>
</html>
-------------------------- ---------- ---------- ---------- ---------- ---------- -----
of course I don't try to run the only vb script in my browser !
did you read my first reply ?
I made some changes .. now the vb code + html exist in the same file .
there is problem with your if - else statements.
would you run this file in your computer please and tell the result(just change the connection and the db(
--------------------------
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Sql
<html>
<head>
<title>Login Form ...</title>
<script runat="server">
Dim CS As String
Dim myConn As SqlClient.SqlConnection
Dim myCommand As SqlClient.SqlCommand
Dim myAdapter As SqlClient.SqlDataAdapter
Dim dsAdmin As DataSet
Dim FoundPW As String
Dim numtries As Integer = 0
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handles btnSubmit.Click
CS = "Server=127.0.0.1;initial catalog=latham;uid=sa;pwd=
myConn = New SqlClient.SqlConnection()
myConn.ConnectionString = CS
myConn.Open()
myCommand = New SqlClient.SqlCommand()
myCommand.Connection = myConn
myCommand.CommandText = "select * from users where ID = '" & txtUserName.Text & "'"
myAdapter = New SqlClient.SqlDataAdapter()
myAdapter.SelectCommand = myCommand
dsAdmin = New DataSet()
myAdapter.Fill(dsAdmin, "admin")
If dsAdmin.Tables(0).Rows.Cou
Response.Redirect("exit.as
End If
FoundPW = dsAdmin.Tables(0).Rows(0).
FoundPW = FoundPW.TrimEnd
If FoundPW = txtPW.Text Then
Session("Userid") = txtUserName.Text
Session("Password") = txtPW.Text
If dsAdmin.Tables(0).Rows(0).
Response.Redirect("file1.a
ElseIf dsAdmin.Tables(0).Rows(0).
Response.Redirect("file2.a
Else
Response.Redirect("exit.as
Else
txtUserName.Text = ""
txtPW.Text = ""
lblSuccess.Text = "Invalid UserName or Password."
numtries += 1
end if
If numtries = 3 Then
numtries = 0
Response.Redirect("exit.as
End If
End If
End Sub
</script>
<style type="text/css">
BODY {
scrollbar-3dlight-color:bl
scrollbar-arrow-color:whit
scrollbar-base-color:RGB(7
scrollbar-track-color:whit
scrollbar-darkshadow-color
scrollbar-face-color:RGB(7
scrollbar-highlight-color:
scrollbar-shadow-color:bla
</style>
</head>
<body ms_positioning="GridLayout
<form id="Form1" method="post" runat="server">
<asp:label id="Label5" runat="server" font-names="Tahoma" font-size="X-Large" style="LEFT: 54px; POSITION: absolute; TOP: 24px">ID :</asp:label>
<asp:textbox id="txtUserName" runat="server" font-size="X-Small" font-names="Tahoma" width="174px" height="24px" tabindex="1" style="LEFT: 136px; POSITION: absolute; TOP: 114px"></asp:textbox>
<asp:label id="Label1" runat="server" font-names="Tahoma" font-size="X-Small" style="LEFT: 54px; POSITION: absolute; TOP: 119px">Password :</asp:label>
<asp:textbox id="txtPW" runat="server" font-size="X-Small" font-names="Tahoma" width="174" height="24" tabindex="2" style="LEFT: 134px; POSITION: absolute; TOP: 155px"></asp:textbox>
<asp:label id="lblSuccess" runat="server" font-names="Tahoma" font-size="X-Small" style="LEFT: 54px; POSITION: absolute; TOP: 119px">Password :</asp:label>
<asp:button id="btnSend" onclick="btnSubmit_Click" runat="server" font-names="Tahoma" text="Send" backcolor="Navy" tabindex="5" font-size="X-Small" forecolor="White" style="LEFT: 212px; POSITION: absolute; TOP: 328px"></asp:button>
</form>
</body>
</html>
--------------------------
ASKER
Thank you very much ...
It works now .
i wrote :
-------------------------- ---------- ---------- ---------- -
FoundID = dsAdmin.Tables(0).Rows(0). Item("ID")
FoundID = FoundID.TrimEnd
-------------------------- ---------- ---------- ---------- -
and
-------------------------- ---------- ---------- ---------- -
If FoundPW <> txtPW.Text or FoundID <> txtUserName.Text Then
lblSuccess.Text = "<br><br><br><br><br><br>I nvalid UserName or Password."
end if
-------------------------- ---------- ---------- ----------
It works now .
i wrote :
--------------------------
FoundID = dsAdmin.Tables(0).Rows(0).
FoundID = FoundID.TrimEnd
--------------------------
and
--------------------------
If FoundPW <> txtPW.Text or FoundID <> txtUserName.Text Then
lblSuccess.Text = "<br><br><br><br><br><br>I
end if
--------------------------
There's the theory behind it, I'm not going to write the entire code for you for only 50 points...