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 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
The problem appears to be that the
Connectionproperty onobjSqlCommandisNothingand hence you get aNullReferenceExceptionwhen you access it. This means in all likely hood thatobjSqlConnectionis alsoNothingand that is likely the root cause of your problem.