为什么我得到一个对象引用未设置错误?

时间:2011-05-19 13:25:40

标签: asp.net vb.net

我似乎无法弄清楚为什么以下代码会给我以下错误:

Object reference not set to an instance of an object.
Line 47:             objSQLCommand.Connection.Open()

这是问题代码:

Function getTabContent() As String
    Dim strUserInitials() As String = Request.ServerVariables("LOGON_USER").Split(CChar("\"))
    strUser = LCase(Trim(strUserInitials(strUserInitials.GetUpperBound(0)))).ToString()

    objStringBuilder = New StringBuilder()

    For intColumn As Integer = 1 To 3
        objSQLCommand = New SqlCommand("select c.*, w.* " & _
        "from intranet.dbo.tabs t " & _
        "inner join intranet.dbo.columns c on t.id = c.tabs_id " & _
        "inner join intranet.dbo.widgets w on c.widgets_id = w.widget_id " & _
        "where t.is_default = 0 " & _
        "and t.cms_initials = @user " & _
        "and t.id = @tab " & _
        "and c.sort_column = @column " & _
        "and w.inactive = 0 " & _
        "order by c.tabs_id, c.sort_column, c.sort_row", objSQLConnection)

        objSQLCommand.Parameters.Add("@user", SqlDbType.VarChar, 3).Value = strUser
        objSQLCommand.Parameters.Add("@tab", SqlDbType.Int, 4).Value = Request.QueryString("tab")
        objSQLCommand.Parameters.Add("@column", SqlDbType.Int, 4).Value = intColumn

        objStringBuilder.Append("<div class=""column"" id=""column_" & intColumn & """>")

        objSQLCommand.Connection.Open()
        objSQLDataReader = objSQLCommand.ExecuteReader()

        While objSQLDataReader.Read()
            objStringBuilder.Append("<div class=""portlet"" id=""portlet_" & objSQLDataReader("widget_id") & """>")
            objStringBuilder.Append("<div class=""portlet-header"">" & objSQLDataReader("widget_name") & "</div>")
            objStringBuilder.Append("<div class=""portlet-content"">" & objSQLDataReader("widget_description") & "</div>")
            objStringBuilder.Append("</div>")
        End While

        objSQLDataReader.Close()
        objSQLCommand.Connection.Close()

        objStringBuilder.Append("</div>")
    Next intColumn

    Return objStringBuilder.ToString
End Function

3 个答案:

答案 0 :(得分:2)

问题似乎是Connection上的objSqlCommand属性为Nothing,因此当您访问它时会得到NullReferenceException。这意味着objSqlConnection Nothing也可能是{{1}},这可能是您问题的根本原因。

答案 1 :(得分:0)

Connection是SqlConnection类型的对象。您需要先创建此对象的实例,然后才能与其进行交互。像这样:

Dim connection As New SqlConnection("your_connection_string")
objSQLCommand.Connection = connection
objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()

答案 2 :(得分:0)

而不是这样做

  objSQLCommand.Connection.Open()
        objSQLDataReader = objSQLCommand.ExecuteReader()

        While objSQLDataReader.Read()
            objStringBuilder.Append("<div class=""portlet"" id=""portlet_" & objSQLDataReader("widget_id") & """>")
            objStringBuilder.Append("<div class=""portlet-header"">" & objSQLDataReader("widget_name") & "</div>")
            objStringBuilder.Append("<div class=""portlet-content"">" & objSQLDataReader("widget_description") & "</div>")
            objStringBuilder.Append("</div>")
        End While

        objSQLDataReader.Close()
        objSQLCommand.Connection.Close()

只是做

 objSQLConnection.Open()
        objSQLDataReader = objSQLCommand.ExecuteReader()

        While objSQLDataReader.Read()
            objStringBuilder.Append("<div class=""portlet"" id=""portlet_" & objSQLDataReader("widget_id") & """>")
            objStringBuilder.Append("<div class=""portlet-header"">" & objSQLDataReader("widget_name") & "</div>")
            objStringBuilder.Append("<div class=""portlet-content"">" & objSQLDataReader("widget_description") & "</div>")
            objStringBuilder.Append("</div>")
        End While


        objSQLConnection.Close()