Why am I getting an object reference not set error?
I can't seem to figure out why the below code is giving me the following error:
Object reference not set to an instance of an object.
Line 47: objSQLCommand.Connection.Open()
This is the problem code:
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 obj开发者_如何学PythonSQLDataReader.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
The problem appears to be that the Connection
property on objSqlCommand
is Nothing
and hence you get a NullReferenceException
when you access it. This means in all likely hood that objSqlConnection
is also Nothing
and that is likely the root cause of your problem.
Connection is an object of type SqlConnection. You need to create an instance of this object before you can interact with it. Something like this:
Dim connection As New SqlConnection("your_connection_string")
objSQLCommand.Connection = connection
objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()
Instead of doing this
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()
just do
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()
精彩评论