Thursday, March 22, 2012

System.NullReferenceException: Object reference not set to an instance of an object.

This is the error message I get:
System.NullReferenceException: Object reference not set to an instance of an object.
at MyStore.CustomerFunctions.getCustomerOrders(String CustomerID)
at MyStore.Customer_Orders.Page_Load(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain()
And this is my code:

PublicSharedFunction getCustomerOrders(ByVal CustomerIDAsString)As OrderCollection

Dim strSQLAsString
Dim m_strConnectionStringAsString
Dim MyReaderAs SqlDataReader
Dim mySqlParaAs SqlParameter
Dim MyOrdersAsNew OrderCollection

m_strConnectionString = ConfigurationSettings.AppSettings("ConnectionString_checkout")

strSQL = "sp_WWWgetCustomerOrders"

mySqlPara =New SqlParameter("@dotnet.itags.org.CustomerID", CustomerID)

Try
MyReader = SqlHelper.ExecuteReader(m_strConnectionString, CommandType.StoredProcedure, strSQL, mySqlPara)
MyOrders.Fill(MyReader)

Catch exAs Exception
' Rethrow Exception for the moment
Throw ex

Finally
MyReader.Close()
MyReader =Nothing

EndTry

Return MyOrders

EndFunction


Code that calls the method:

DimaCustomerAs Customer
Dim MyOrdersAs OrderCollection
aCustomer =CType(HttpContext.Current.User, CustomPrincipal).Customer

MyOrders = CustomerFunctions.getCustomerOrders(aCustomer.CustomerID)

If MyOrders.Count > 0Then
Me.rptOpenRecentOrders.DataSource = MyOrders
Me.rptOpenRecentOrders.DataBind()
pnlNoOrdersToDisplay.Visible =False
Else
pnlNoOrdersToDisplay.Visible =True
EndIf

I only get the error occasionally at busy times on the internet, the Stored Proc is valid the connection string is valid because it works some times. The problem is not incorrect data being passed because if no data or incorrect data is past a different exception will be displayed because I have tested. I have no idea why this is happening, I have a feeling its to do with SQL Server because it only happens when it is at its busiest.
If the Stored Proc returns an empty reader there is no error, I have alreadyinitialized My returning object (the Orders Collection) so it shouldn't matter.
I know that the customer id is valid because I have an email sent to me if there is an error in the application via the globalApplication_Errormethod, it gets the customer id from the currently logged in person
Dim aCustomerAs Customer =CType(HttpContext.Current.User, CustomPrincipal).Customer
strCustID = aCustomer.CustomerID
This is displayed correctly when emailed. As I say this only happens at busy times and I am pulling my hair out to identify whats happening, can any one help?
Cheers
Scott


elbandit--
Regarding this...

elbandit wrote:

...As I say this only happens at busy times and I am pulling my hair out to identify whats happening, can any one help?


...I am wondering-- exactly what line is causing the error?
(The line can be discovered by doing a step-through of the code.)
I am going to guess that it is this line...
Do While MyReader.Read = True
...that causes the error.
If so, then you may be able to check for Nothing before the operation, like this...
If (MyReader Is Nothing) Then
'Reader cannot be used, probably due to a database connection issue.
'Maybe retry 1x automatically and if no connection then show message to user and ask the user to retry.
Else
Do While MyReader.Read = True
'Continue.
End If
...which may or may not work based on your use case.
HTH.
Thank you.
--Mark Kamoski
Sorry Mark I posted the wrong function, I have now editied. Please can you have another look,
Thanks
Scott
i can not see in your code
Dim con as New Sqlconenction(connection string her)

is it some where else or that what you miss!!??
elbandit--
Regarding this...

elbandit wrote:

Sorry Mark I posted the wrong function, I have now editied. Please can you have another look...


...I still think that the issue may be that the Reader is not getting instantiated properly when then database is too busy. As I mentioned above, you may need to check for null (Nothing) before using it.
If the database is busy, you may need to increase the timeout (a property on the connection string, seehttp://www.ConnectionStrings.com for details).
Alternately, if the database is really busy maybe your licensing has reached the maximum number of of connections and that's why the Reader is not instantiated properly.
Another idea would be to use a DataAdapter and a DataSet. Yours is a simple case and these objects might be a bit more stable given that the Reader stays open until it is closed and you really do not need an open connection to the database in this case.
The code looks something like this...
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand(COMMAND_TEXT, myConnection);
System.Data.OleDb.OleDbDataAdapter myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(myCommand);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet);
...or you can seehttp://www.WebLogicArts.com/DemoReadingData01.aspx for a full working sample with all the code.
HTH.
Thank you.
--Mark Kamoski

Thanks Mark, I think you are probably right, as it only errors in busy periods I have changed the code to:

MyReader = SqlHelper.ExecuteReader(m_strConnectionString, CommandType.StoredProcedure, strSQL, mySqlPara)

If MyReaderIsNothingThen
Email.sendEmail()
EndIf
MyOrders.Fill(MyReader)

This way it will still error but I will get an email if the problem is that the reader is null, then I will be able to test with populating a dataset then populating my custom collection from that. The reason I didn't do it like that to begin with is that I thought a reader populated a dataset, but I guess if that is true the reader will populate a dataset faster than could populate my custom collection and therefore no error.
I will check out the time out property as well and let you know how I got on, and post the code that works. Cheers.
Thanks for the replies
Scott
P.S.
Fadil, I use MS Data Access Application Block to handle all connections to the database and opening the database and closing it is all done within in the most effiecient manner take a look athttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/daab.asp for more info.

0 comments:

Post a Comment