I have a bit of C# code calling a DAL file to add something to the database. I have the following line of code to call the method:
string ticket = new PromotionObject().AddPromotionEntry(formID, user, submission)
// string, AccountUsers object from Session, string
Within PromotionObject class, I have at top:
private PromotionDAL db = new PromotionDAL(); // invokes the dal object
The AddPromotionEntry method is:
public string AddPromotionEntry(string formID, AccountUsers user, string submission) {
try {
return db.AddPromotionEntr(Convert.ToInt64(formID), user.Id, submission);
// calls the method using db variable in form long, long, string;
// The user.Id gets the id of the user object to use in database
}
catch (NullReferenceException ex) {
string msg = ex.Message; return string.Empty;
}
}
Now the problem:
When I run this code for the first time by clicking a button on the page, there is a NullReferenceException
trying to access the return db.AddPromotionEntry(...)
method, identified by debugging and entering the catch. When putting a breakpoint on this line, none of the variables are null (all populated as expected) and the db variable is also not null.
When I run it again (not a page refresh) by the button click, this method call passes OK and works as expected.
I am lost at why the first time it throws a NullReferenceException but not on the second time - it is like the first time invoke is doing a catch up and by the seond invoke, it has caught up.
Any Ideas?? }