NullReferenceException error

Posts   
 
    
rai
User
Posts: 41
Joined: 25-Jan-2007
# Posted on: 25-Jul-2007 05:50:42   

This is my function

static public bool CheckRecords(string RecNo, int Pub) { DataAccessAdapter adapter = new DataAccessAdapter(dbConnectionString()); IPredicateExpression filter = new PredicateExpression(); filter.Add(SubsFields.AccountNumber == RecNo); filter.Add(SubsFields.PubCode == Pub); filter.Add(SubsFields.Update != true); bool value = (bool)adapter.GetScalar(SubsFields.Update, null, AggregateFunction.None, filter); adapter.Dispose(); return value; }

I am getting NullReferenceException was unhandled by user code. Object reference not set to an instance of an object.

This function I have a created to check whether a record is updated or not. If true then record is updated.

any sugesstion please

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Jul-2007 09:28:16   

please post the stack trace.

rai
User
Posts: 41
Joined: 25-Jan-2007
# Posted on: 25-Jul-2007 10:26:54   

Walaa wrote:

please post the stack trace.

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 92: // if (adapter.GetScalar(SubsFields.Update, null, AggregateFunction.None, filter) != null) Line 93: // { Line 94: value = (bool)adapter.GetScalar(SubsFields.Update, null, AggregateFunction.None, filter); Line 95: adapter.Dispose(); Line 96: // }

Source File: C:\SubsTrans.cs Line: 94

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] SubscriptionBL.SubsTrans.CheckRecords(String AccountNo, Int32 Publication) in C:\SubsTrans.cs:94 WebGUIInterface.JournalEntry.btnUpdate_Click(Object sender, EventArgs e) in C:\JournalEntry.aspx.cs:143 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Jul-2007 11:28:19   

Please tell me if using any of the following would make a difference:

value = (bool)adapter.GetScalar(SubsFields.Update, null, AggregateFunction.Max, filter);

value = (bool)adapter.GetScalar(SubsFields.Update, filter, null);
rai
User
Posts: 41
Joined: 25-Jan-2007
# Posted on: 25-Jul-2007 22:51:47   

Walaa wrote:

Please tell me if using any of the following would make a difference:

value = (bool)adapter.GetScalar(SubsFields.Update, null, AggregateFunction.Max, filter);

value = (bool)adapter.GetScalar(SubsFields.Update, filter, null);

when i used :value = (bool)adapter.GetScalar(SubsFields.Update, null, AggregateFunction.Max, filter); It gave the following error ORMQueryExecutionException was unhandled by user code An exception was caught during the execution of a retrieval query: Operand data type bit is invalid for max operator.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

Just because i think cant use AggregateFunction.Max because SubsFields.Update datatype is bit.

The other one is not working at all..cant compile...

any other suggestions please....

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Jul-2007 08:51:48   

What if GetScalar return nothing?

Try:


object objValue = adapter.GetScalar(SubsFields.Update, null, AggregateFunction.None, filter);

bool value = (objValue != null) ? (bool) objValue : false;
David Elizondo | LLBLGen Support Team
rai
User
Posts: 41
Joined: 25-Jan-2007
# Posted on: 27-Jul-2007 05:56:21   

daelmo wrote:

What if GetScalar return nothing?

Try:


object objValue = adapter.GetScalar(SubsFields.Update, null, AggregateFunction.None, filter);

bool value = (objValue != null) ? (bool) objValue : false;

Thanks a lot for the suggestion. Thats where the error was, the object was returning null

Once again thank u.