Object reference not set to an instance of an object.

poifull

New member
Joined
Feb 28, 2011
Messages
1
Programming Experience
Beginner
Hi, I have the above error when i deploy my web on the server. Below is the codes and stack trace. Help needed. Thank you so much!

Stack Trace

[NullReferenceException: Object reference not set to an instance of an object.]
WebApplication1.Login.ImageButton2_Click(Object sender, ImageClickEventArgs e) in C:\Users\L30810\Desktop\fyp final.16\WebApplication1\Login.aspx.vb:16
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +86
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746

Login.aspx.vb

VB.NET:
Protected Sub ImageButton2_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
        Dim connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("database").ConnectionString()
        Dim mycon As New SqlConnection(connection)

        mycon.Open()

        Dim queryString As String = "SELECT Role,StaffName FROM [role] WHERE LoginID ='" + TextBox1.Text + "' and Password='" + TextBox2.Text + "'"
        Dim cmd As SqlCommand = New SqlCommand(queryString, mycon)

        Dim reader As SqlDataReader = cmd.ExecuteReader()

        Dim user1 As String = ""
        While reader.Read()
            Dim role As String = ""
            role = reader.GetString(0)
            user1 = reader.GetString(1)
            Session("role") = role
        End While

        If (Session("role") = "SA") Then
            Response.Expires = 0
            Response.ExpiresAbsolute = Now()
            Response.CacheControl = "no-cache"
            Session("User") = user1
            Response.Redirect("MainPageSA.aspx")

        ElseIf (Session("role") = "MGR") Then
            Session("User") = user1
            Response.Expires = 0
            Response.ExpiresAbsolute = Now()
            Response.CacheControl = "no-cache"
            Response.Redirect("MainPageMGR.aspx")

        ElseIf (Session("role") = "Assessor") Then
            Session("User") = user1
            Response.Expires = 0
            Response.ExpiresAbsolute = Now()
            Response.CacheControl = "no-cache"

            Response.Redirect("MainPageAssessor.aspx")

        ElseIf (Session("role") = "MC") Then
            Session("User") = user1
            Response.Expires = 0
            Response.ExpiresAbsolute = Now()
            Response.CacheControl = "no-cache"
            Response.Redirect("MainPageMC.aspx")
        Else
            MsgBox("Invalid Username/Password", MsgBoxStyle.OkOnly, "Clinical Peformance Appraisal")

        End If
        reader.Close()
        mycon.Close()
    End Sub
 
First of all, I'm going to have to pull you up on your naming convention. You have a String variable named "connection" and then a SqlConnection variable named "mycon". Given that the second variable is the actual connection object, shouldn't it be named "connection"? The first variable is a a connection string, not a connection, so what's wrong with naming it "connectionString"?

Also, I cannot recommend strongly enough that you do not use string concatenation to insert values into SQL code. You should always use parameters. For the why and how, Follow the Blog link in my signature and check out my post on ADO.NET Parameters.

Finally, why are you using a loop to read the data? If there are multiple records then your code is bad because it will only get the last one. If there is only one record then the loop is pointless.

As for the issue, are you saying that this error occurs in the deployed application but not during testing? If so then you should put some tracing/logging in your code. That way you can determine exactly where the code is getting to before failing and then you can test each reference on the line that fails to see which one is Nothing.
 
Hi Poifull,

Did you add the Session user? I see your code Session("role") and Session("user")? Maybe thats the problem you should Session.Add("user", object) first. But if you add that Session objects in your another page of program that is not the cause of your problem. Use breakpoint to pinpoint where the Object reference not set to an instance of an object error occured. Hope it helps
 
Another source of that type of error is a missing resource, for example a button image.

The error message appears to be referring to a local C: path that, I'm guessing, is your apps dev folder... have you copied all the relevant resources when deploying?
 
Back
Top