0

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!!!

Web.config

 <add name="Database1" connectionString="Data Source='170.21.191.85';Initial Catalog='Database1';User ID='sa';Password='123'"/>

Login.aspx.vb

Dim connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("Database1").ConnectionString()
    Dim mycon As New SqlConnection(connection)

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.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

ImageButton2_Click

 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
user596379
  • 213
  • 1
  • 4
  • 11
  • Dim connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("Database1").ConnectionString() – user596379 Feb 25 '11 at 09:26
  • Are you _sure_ the application config has a connection string named "Database1"? – Oded Feb 25 '11 at 09:28
  • And that is in fact in the `` configuration section? – Oded Feb 25 '11 at 09:29
  • I have error putting it in configSections tag. The element 'configSections' has invalid child element 'connectionStrings'. List of possible elements expected:'section,sectionGroup'. – user596379 Feb 25 '11 at 09:32
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – stakx - no longer contributing Jul 23 '15 at 08:01

1 Answers1

0

You don't need to quote the values in the connection string.

<add name="Database1" connectionString="Data Source=170.21.191.85;Initial Catalog=Database1;User ID=sa;Password=final"/>
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • @user596379 - Code? Where did I say code? I said _quote_. You don't need to use `'` in the connection string. You should use `User ID=sa;`, not `User ID='sa';`. – Oded Feb 25 '11 at 09:10
  • oops sorry..my mistake. i shall try it out. – user596379 Feb 25 '11 at 09:11
  • @user596379 - And what line does it happen on? And what is the stack trace? Can you edit your question and add these details to it? – Oded Feb 25 '11 at 09:16
  • @user596379 - Did your remove the single quotes from _all_ values of the connection string, as in my example? And can you post the contents of `ImageButton2_Click`? – Oded Feb 25 '11 at 09:19