I have been working on an ASP page (with VB Background) that is a basic user login page. It is being run on our Intranet within my organization, however, it's not working. Can anyone provide some suggestions
I've included the code for the .aspx page and for the code behind on the "Login" button.
Front end:
%@ Page Language="vb" AutoEventWireup="false" Codebehind="login.aspx.vb" Inherits="PIF_VB.login" %>
<HTML>
<HEAD>
<title>Login</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body bgColor="#cccccc" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:textbox id="UserName" style="Z-INDEX: 101; LEFT: 200px; POSITION: absolute; TOP: 104px"
runat="server" Width="152px"></asp:textbox><asp:label id="Label1" style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 112px" runat="server"
Width="82px" Font-Bold="True">User Name:</asp:label><asp:label id="Label2" style="Z-INDEX: 103; LEFT: 128px; POSITION: absolute; TOP: 152px" runat="server"
Width="64px" Font-Bold="True">Password:</asp:label><INPUT runat="server" style="Z-INDEX: 104; LEFT: 248px; POSITION: absolute; TOP: 184px"
type="submit" value="Login" id="Submit1" name="Submit1"><INPUT style="Z-INDEX: 105; LEFT: 200px; WIDTH: 152px; POSITION: absolute; TOP: 144px; HEIGHT: 24px"
type="password" size="20" id="password">
Backend on Login Button Click:
Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
Dim cn As New System.Data.SqlClient.SqlConnection("PROVIDER=SQLOLEDB;DATA SOURCE=themisto;UID=acadia_pif;PWD=pi32fs;DATABASE =AHOSP_PIF")
'here is where you will put the code to go after the login info
Try
'build a command from the connection
Dim cmd As New SqlClient.SqlCommand
cmd.Connection = cn
cmd.Parameters.Add("@username", System.Data.SqlDbType.Text, 80)
cmd.Parameters.Add("@password", System.Data.SqlDbType.Text, 80)
cmd.Parameters("@username").Value = UserName.Text
cmd.Parameters("@password").Value = password.Text
cmd.CommandText = "Select count(*) from AHOSP_Users where user_id=@username and password=@password"
'open the connection (execute scalar may do this for you)
cn.Open()
'return the number of records that match the credentials!
Dim cnt As Integer = cmd.ExecuteScalar
If cnt > 0 Then
'you found a match
'so go to the application pages!
'lblmsg.Text = "Congrats!"
'lblmsg.Visible = True
Response.Redirect("tab.aspx")
Else
'you didn't so give them a message
lblmsg.Text = "That username and password combination is not valid. Please try again."
lblmsg.Visible = True
End If
Catch ex As Exception
Finally
'be sure to dispose of the connection
cn.Dispose()
End Try
End Sub
End Class
If anyone can save my life, I'd truly appreciate it. Thanks!
I've included the code for the .aspx page and for the code behind on the "Login" button.
Front end:
%@ Page Language="vb" AutoEventWireup="false" Codebehind="login.aspx.vb" Inherits="PIF_VB.login" %>
<HTML>
<HEAD>
<title>Login</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body bgColor="#cccccc" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:textbox id="UserName" style="Z-INDEX: 101; LEFT: 200px; POSITION: absolute; TOP: 104px"
runat="server" Width="152px"></asp:textbox><asp:label id="Label1" style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 112px" runat="server"
Width="82px" Font-Bold="True">User Name:</asp:label><asp:label id="Label2" style="Z-INDEX: 103; LEFT: 128px; POSITION: absolute; TOP: 152px" runat="server"
Width="64px" Font-Bold="True">Password:</asp:label><INPUT runat="server" style="Z-INDEX: 104; LEFT: 248px; POSITION: absolute; TOP: 184px"
type="submit" value="Login" id="Submit1" name="Submit1"><INPUT style="Z-INDEX: 105; LEFT: 200px; WIDTH: 152px; POSITION: absolute; TOP: 144px; HEIGHT: 24px"
type="password" size="20" id="password">
Backend on Login Button Click:
Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
Dim cn As New System.Data.SqlClient.SqlConnection("PROVIDER=SQLOLEDB;DATA SOURCE=themisto;UID=acadia_pif;PWD=pi32fs;DATABASE =AHOSP_PIF")
'here is where you will put the code to go after the login info
Try
'build a command from the connection
Dim cmd As New SqlClient.SqlCommand
cmd.Connection = cn
cmd.Parameters.Add("@username", System.Data.SqlDbType.Text, 80)
cmd.Parameters.Add("@password", System.Data.SqlDbType.Text, 80)
cmd.Parameters("@username").Value = UserName.Text
cmd.Parameters("@password").Value = password.Text
cmd.CommandText = "Select count(*) from AHOSP_Users where user_id=@username and password=@password"
'open the connection (execute scalar may do this for you)
cn.Open()
'return the number of records that match the credentials!
Dim cnt As Integer = cmd.ExecuteScalar
If cnt > 0 Then
'you found a match
'so go to the application pages!
'lblmsg.Text = "Congrats!"
'lblmsg.Visible = True
Response.Redirect("tab.aspx")
Else
'you didn't so give them a message
lblmsg.Text = "That username and password combination is not valid. Please try again."
lblmsg.Visible = True
End If
Catch ex As Exception
Finally
'be sure to dispose of the connection
cn.Dispose()
End Try
End Sub
End Class
If anyone can save my life, I'd truly appreciate it. Thanks!

User Authentication ASP/SQL
sgrussell
beezleinc
Imports System.SqlClient
dim cn as new sqlconnection("PROVIDER=SQLOLEDB;DATA SOURCE=themisto;UID=acadia_pif;PWD=pi32fs;DATABASE =AHOSP_PIF")
dim cmd as new sqlcommand("Select count(*) from AHOSP_Users where user_id=' " & UserName.Text & " ' and password=' " & password.Text & "'",cn)
cn.open()
Dim cnt As Integer = cmd.ExecuteScalar()
If cnt > 0 Then
'you found a match
'so go to the application pages!
'lblmsg.Text = "Congrats!"
'lblmsg.Visible = True
Response.Redirect("tab.aspx")
Else
'you didn't so give them a message
lblmsg.Text = "That username and password combination is not valid. Please try again."
lblmsg.Visible = True
End If
ysb
How would I print out the Catch Statement
I tried response.write(ex.message) and that's not working.
Mustansir - MSFT
hi
best soluation is remove the submit button and add new submit button it will works.
The same problem we wear facing 1 yrs back.
Thanks
Dhiraj
Jeke
Mohit Saini
Michael Stephenson UK
Holger Grund
sharadha
Are you using ASP.net 2.0 If so I'd recommend that you don't write a line of code for a login control. One of these two webcasts should give an overview of using the Login control
Either this one
or try this one