Somone please help NullRefrence Error
-
Hi, I get a NullRefrence Exception with the following code. I've spent a week working on this and I still can't figure it out. The error only comes If I enter the wrong username or password.
Public Class Login Inherits System.Web.UI.Page Protected WithEvents btnLogin As System.Web.UI.WebControls.Button Protected WithEvents WrongPass as System.Web.UI.WebControls.Label Protected WithEvents tbPass as System.Web.UI.WebControls.textbox Protected WithEvents tbUsername as System.Web.UI.WebControls.textbox Private Sub btnLogin_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnLogin.Click if ValidateUser(tbUsername.Text,TbPass.Text) then System.Web.Security.FormsAuthentication.RedirectFromLoginPage(tbUserName.Text, true) else WrongPass.Text = "You have entered the wrong username and or password" end if End Sub Private Function ValidateUser(ByVal uid As String, ByVal passwd As String) As Boolean try dim strConn as string = System.Configuration.ConfigurationSettings.AppSettings("ConnString") dim Conn as new System.Data.OleDb.OleDbConnection(strConn) dim cmd as System.Data.OleDb.OleDbCommand = new System.Data.OleDb.OleDbCommand("SELECT * from tblUsers where Username = '" & uid & "'", Conn) dim dr as System.Data.OleDb.OleDbDataReader Conn.Open() dr = cmd.ExecuteReader() While dr.Read() If String.Compare(dr("Password").ToString(),passwd,False)=0 Then Conn.Close() Return True else Conn.Close() Return False End If End While catch ex as System.Exception end try End Function End Class
The stack is: [NullReferenceException: Object reference not set to an instance of an object.] Login.btnLogin_Click(Object sender, EventArgs e) +118 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33 System.Web.UI.Page.ProcessRequestMain() +1252 Thank you for your help.