title: Object reference not set to an instance of an object
body: <p>I don't know why I am getting this error when I deploy my web on server. Is it because the problem with my connection string? Please help me. Thank you!!!</p>
  
  <p>Web.config</p>
  
  <pre><code> &lt;add name="Database1" connectionString="Data Source='170.21.191.85';Initial Catalog='Database1';User ID='sa';Password='123'"/&gt;
  </code></pre>
  
  <p>Login.aspx.vb</p>
  
  <pre><code>Dim connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("Database1").ConnectionString()
      Dim mycon As New SqlConnection(connection)
  </code></pre>
  
  <p>Stack Trace</p>
  
  <blockquote>
    <p>[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.14\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</p>
  </blockquote>
  
  <p>ImageButton2_Click</p>
  
  <pre><code> 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("Database1").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
  </code></pre>