Friday, March 16, 2012

Object reference not set to an instance of an object.

Ihave a number of checkbox selections available, however I need a way tocheck and load null values...as it is highly unlikely a user will everselect all posible checkboxes for this query. So whether they are setor not, I create sessions variables then go to dg1.aspx to show them ina datagrid.
search page:

Session("furnished") = furnished.Checked
Session("pets") = pets.Checked
Session("parking") = parking.Checked
Session("aircond") = aircond.Checked
Session("amenpref") = amenpref.Checked
Session("type") = type.Checked
Session("rooms") = rooms.Checked
Session("price") = price.Checked
Session("pref") = pref.Checked
Session("term") = term.Checked
Whenthe form is submitted to the datagrid page I get the object referenceerror. How can I prevent this? Here's the datagrid page:
Function CreateDataSource() As ICollection


'process session variables input

Dim type, typesel, rooms, roomsel, price, pricesel, pref,prefsel, term, termsel, furnished, furnishedsel, pets, petsel, parking,parkingsel, aircond, aircondsel As string

type = Request.Params("type")

if type = 1 then
typesel = System.DBNull.value
else if type = 2then
typsel = "suitesapts"
else if type = 3then
typesel = "duplex"
else if type = 4then
typesel = "liveworkspace"
else if type = 5then
typesel = "officestore"
else if type = 6 then
typesel = "parkingstorage"
else if type = 7then
typesel = "commercial"
end if

'rooms
rooms = Request.Params("rooms")

if rooms = 1 then
roomsel = System.DBNull.value
else if rooms = 2 then
roomsel = "bachelorstudio"
else if rooms = 3 then
roomsel = "loft"
else if rooms = 4 then
roomsel = "1bed"
else if rooms = 5 then
roomsel = "2bed"
else if rooms = 6 then
roomsel = "3ormore"
end if

'term
term = Request.Params("term")

if term = 1 then
termsel = System.DBNull.value
else if term = 2then
termsel = "shortterm"
else if term = 3 then
termsel = "shared"
else if term = 4 then
termsel = "lease"
end if

'preferences



pets = Request.Param("pets")
if pets = 0 then
petsel = System.DBNull.value
end if

parking = Request.Param("parking")
if parking = 0 then
parkingsel = System.DBNull.value
end if

aircond = Request.Param("aircond")
if aircond = 0 then
aircondsel = System.DBNull.value
end if

price = Request.Params("price")
if price = 1 then
pricesel = System.DBNull.value
end if

amenpref = Request.params("amenpref")
if amenpref = 1 then
amenprefsel = System.DBNull.value
end if

' Create a new connection object
Dim cn As New SqlConnection( _
"Server=localhost;Trusted_Connection=true;Database=findandview")
' Create a new SqlDataAdapter and pass the SQL Statement
' and connection object
Dim da As SqlDataAdapter = New SqlDataAdapter _
("SELECT id, description, dateavailable, area, price frommaintable where type ='" & typesel & "' and where rooms ='"& roomsel &"' and where term='" & termsel & "' andwhere pets='" & petsel & "' and where parking = '" &parkingsel & "' and where aircond='" & aircondsel & "' andwhere price ='" & price & "' and where amenpref ='" &amenprefsel & "', cn)
' where id='" & itemid & "'"
' Create a DataSet to hold the SqlDataAdapter data
Dim ds As Datatable = New Datatable("maintable")
' Call the Fill method to load the DataSet
da.Fill(ds)
' Set the DataSource property of the grid
' to bind the data from the DataSet
'DataGrid1.DataSource = ds

Return new dataview(ds)


End Function


If any of your Request.Params and Session values are none exisisting, you will get the "Object reference not set to an instance of an object" error. Always check this:
If Not Request.Params("etc") Is NothingThen
'You can use the Object
End If

0 comments:

Post a Comment