Monday, March 26, 2012

Object reference not set to an instance of an object

Hi folks,
I've been getting this error for the longest time and I can't seem tofix it. Could someone have a look at the code below and tell mewhere I'm going wrong?
Thanks...
******************** Code Below ************************
sub reservationControl(Sender as Object, e as EventArgs)
dim check1 as Integer
dim check2 as Integer
if (tbDepartTwoBerth.Text <> 0 ORtbDepartFourBerth.Text <> 0 OR tbReturnTwoBerth.Text <> 0OR tbReturnFourBerth.Text <> 0) then
check1 =checkOverbooking(lbDepart.SelectedItem.Text, tbDepartTwoBerth.Text,tbDepartFourBerth.Text)
check2 =checkOverbooking(lbReturn.SelectedItem.Text, tbReturnTwoBerth.Text,tbReturnFourBerth.Text)
end if
if (check1 = 1 OR check2 = 1) then
lblOverbook.Text = "Cabins are overbooked!"
else
makeReservation()
end if
end sub
function checkOverbooking(tempDate as DateTime, tempTwoBerth as Integer, tempFourBerth as Integer) as Integer
dim curTwoBerth, curFourBerth as Integer
dim objOBCmd as OleDbCommand = newOleDbCommand("SELECT TwoBerth, FourBerth FROM tblSailings WHERE Date=#"& tempDate.ToString("g", strCultureInfo) & "#" , objConn)
dim objOBReader as OleDbDataReader
try
objOBCmd.Connection.Open()
objOBReader= objOBCmd.ExecuteReader
objOBReader.Read()
curTwoBerth = objOBReader.GetInt32(0)
curFourBerth = objOBReader.GetInt32(1)
objOBCmd.Connection.Close()
catch
objOBCmd.Connection.Close()
end try
if (curTwoBerth - tempTwoBerth) < 0 then
return 1
else if (curFourBerth - tempFourBerth) < 0 then
return 1
end if
objOBCmd.Connection.Close()
return 2
end function
*********************END OF CODE ***********************
Could you please point out the line on which you are receiving the error?

Sorry about that...I'm receiving the error at:
check2 = checkOverbooking(lbReturn.SeletectedItem.Text, tbReturnTwoBerth.Text, tbReturnFourBerth.Text)
Thanks.

Could it be possible that my SQL statement would be returning a null value which would cause

objOBReader= objOBCmd.ExecuteReader
to try to read that NULL value?

Just in case anyone was following along or ends up finding this post ina search, the problem I was having is that I had left the drop downfield for the return trip blank. Therefore, when I passed thedrop down list field in my function call, I was passing a NULL value.
Dumb on my part, but at least the problem is solved...

0 comments:

Post a Comment