你调用的对象是空的

时间:2011-02-25 08:51:06

标签: vb.net iis

我不知道为什么在服务器上部署Web时出现此错误。是因为我的连接字符串的问题?请帮我。谢谢!!!

的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)

堆栈跟踪

  

[NullReferenceException:对象引用未设置为对象的实例。]      WebApplication1.Login.ImageButton2_Click(Object sender,ImageClickEventArgs e)位于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

1 个答案:

答案 0 :(得分:0)

您无需引用连接字符串中的值。

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