Friday, March 16, 2012

Login Page problem

I have created a simple login page using loginview
login.aspx
1<asp:LoginView ID="LoginView1" runat="server">2 <AnonymousTemplate>3 <asp:Login ID="loginsys" runat="server" UserNameLabelText="Username:" OnAuthenticate="loginsys_Authenticate" />4 <asp:Label ID="lblErrMsg" runat="server" ForeColor="Red" />5 </AnonymousTemplate>6 <LoggedInTemplate>7 Hello,<asp:LoginName ID="LoginName1" runat="server" />8 <br />9 <asp:LoginStatus ID="LoginStatus1" runat="server" LoginText="" LogoutText="Logout" />10 </LoggedInTemplate>11 </asp:LoginView>

login.aspx.vb

1Dim loginsysAs WebControls.Login =Me.LoginView1.FindControl("loginsys")2Dim lblErrMsgAs Label =Me.LoginView1.FindControl("lblErrMsg")34Protected Sub loginsys_Authenticate(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.AuthenticateEventArgs)5Try67 If ChkLogin(loginsys.UserName, loginsys.Password)Then8 FormsAuthentication.RedirectFromLoginPage(loginsys.UserName, loginsys.RememberMeSet)9Else10 Throw New Exception("Login Failed")11End If12 Catch exAs Exception13 lblErrMsg.Text = ex.Message14End Try151617 End Sub1819 Public Function ChkLogin(ByVal UserNameAs String,ByVal UserPasswordAs String)As Boolean20 Dim connAs New SqlConnection21Dim cmmdAs New SqlCommand2223Dim pmtUserNameAs New SqlParameter24Dim pmtUserPasswordAs New SqlParameter25Dim strSQLAs String26 Dim drAs SqlDataReader2728Try2930 conn =New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)3132 conn.Open()33 strSQL ="SELECT * FROM tbl_user WHERE login = @dotnet.itags.org.login and pwd = @dotnet.itags.org.pwd"34 cmmd.Connection = conn35 cmmd.CommandText = strSQL3637 pmtUserName.ParameterName ="@dotnet.itags.org.login"38 pmtUserName.Value = UserName39 pmtUserPassword.ParameterName ="@dotnet.itags.org.pwd"40 pmtUserPassword.Value = UserPassword4142 cmmd.Parameters.Add(pmtUserName)43 cmmd.Parameters.Add(pmtUserPassword)4445 dr = cmmd.ExecuteReader()4647If dr.ReadThen48 ChkLogin =True49 Else5051 Throw New Exception("Login Fails!!")52End If5354 Catch exAs Exception55 ChkLogin =False56 lblErrMsg.Text = ex.Message57Finally5859 pmtUserName =Nothing60 pmtUserPassword =Nothing6162 dr =Nothing63 cmmd =Nothing64 conn =Nothing65 End Try6667 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

Set some breakpoints and step through the code to see where, specifically, it's blowing up...

Hello,

Please put these codes into Try/Catch block.

19 Try

20 Dim connAs New SqlConnection
21 Dim cmmdAs New SqlCommand
22
23 Dim pmtUserNameAs New SqlParameter
24 Dim pmtUserPasswordAs New SqlParameter
25 Dim strSQLAs String
26 Dim drAs SqlDataReader

Note : When you create a object, you should initialize it immediately, because those codes has out of try/catch block, the complier could recoganize them.

0 comments:

Post a Comment