-
February 6th, 2007, 12:25 PM
#1
Login Page Problem
I have created a simple login page using loginview
login.aspx
Code:
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<asp:Login ID="loginsys" runat="server" UserNameLabelText="Username:" OnAuthenticate="loginsys_Authenticate" />
<asp:Label ID="lblErrMsg" runat="server" ForeColor="Red" />
</AnonymousTemplate>
<LoggedInTemplate>
Hello,<asp:LoginName ID="LoginName1" runat="server" />
<br />
<asp:LoginStatus ID="LoginStatus1" runat="server" LoginText="" LogoutText="Logout" />
</LoggedInTemplate>
</asp:LoginView>
login.aspx.vb
Code:
Dim loginsys As WebControls.Login = Me.LoginView1.FindControl("loginsys")
Dim lblErrMsg As Label = Me.LoginView1.FindControl("lblErrMsg")
Protected Sub loginsys_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)
Try
If ChkLogin(loginsys.UserName, loginsys.Password) Then
FormsAuthentication.RedirectFromLoginPage(loginsys.UserName, loginsys.RememberMeSet)
Else
Throw New Exception("Login Failed")
End If
Catch ex As Exception
lblErrMsg.Text = ex.Message
End Try
End Sub
Public Function ChkLogin(ByVal UserName As String, ByVal UserPassword As String) As Boolean
Dim conn As New SqlConnection
Dim cmmd As New SqlCommand
Dim pmtUserName As New SqlParameter
Dim pmtUserPassword As New SqlParameter
Dim strSQL As String
Dim dr As SqlDataReader
Try
conn = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
conn.Open()
strSQL = "SELECT * FROM tbl_user WHERE login = @login and pwd = @pwd"
cmmd.Connection = conn
cmmd.CommandText = strSQL
pmtUserName.ParameterName = "@login"
pmtUserName.Value = UserName
pmtUserPassword.ParameterName = "@pwd"
pmtUserPassword.Value = UserPassword
cmmd.Parameters.Add(pmtUserName)
cmmd.Parameters.Add(pmtUserPassword)
dr = cmmd.ExecuteReader()
If dr.Read Then
ChkLogin = True
Else
Throw New Exception("Login Fails!!")
End If
Catch ex As Exception
ChkLogin = False
lblErrMsg.Text = ex.Message
Finally
pmtUserName = Nothing
pmtUserPassword = Nothing
dr = Nothing
cmmd = Nothing
conn = Nothing
End Try
End Function
However, i got the error message once start up the page
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
I have no ideas what kind of error it is since i don't find any problems in the code.
Could anybody advice, please?
Thank you
-
February 6th, 2007, 01:26 PM
#2
Re: Login Page Problem
Do you have output of the stack trace?
Some cause happiness wherever they go; others, whenever they go.
-
February 7th, 2007, 01:16 AM
#3
Re: Login Page Problem
Here is the stack trace
[NullReferenceException: Object reference not set to an instance of an object.]
login_login2..ctor() +30
ASP.login_login2_aspx..ctor() +10
__ASP.FastObjectFactory_app_web_62atenog.Create_ASP_login_login2_aspx() +20
System.Web.Compilation.BuildResultCompiledType.CreateInstance() +58
System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +113
System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +32
System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +62
System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +292
System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +139
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +146
Hope it helps!
-
February 8th, 2007, 08:23 AM
#4
Re: Login Page Problem
The only thing that comes to mind there is that you can't use a LoginView's controls directly - so lblErrMsg in the catch in loginsys_Authenticate is probably Nothing. Apologies, I'd normally use C# so my VB might be a bit sketchy...
Code:
Dim lbl As Label
lbl = LoginView1.FindControl("lblErrMsg")
If Not lbl Is Nothing Then
lbl.Text = ex.Message
End If
HTH,
T
-
February 9th, 2007, 02:21 AM
#5
Re: Login Page Problem
I tried but defining like this will have an error of " Declaration expected"
Thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|