views:

319

answers:

5

This code generates and Null Reference exception. Exception comes at the line where the parameter array is initialized. What can be the problem? I don't know howto follow the stack-trace and work any logic over it. thanks in advance.

DAL dal = new DAL();

    string SQL = @"INSERT INTO Assets ([AssetName],[AssetType],[Model],[Description],
                                                          [PurchaseValue],[SalvageValue],[Currency],[DateAcquired,[DateRetire],[ImagePath],
                                                          [InUse])
                                                          VALUES (?,?,?,?,?,?,?,?,?,?,?)";

    OleDbParameter[] par = new OleDbParameter[]{ 
    new OleDbParameter("@assetname",name.Text),
    new OleDbParameter("@assettype",assettypes.SelectedValue.ToString()),
    new OleDbParameter("@model",model.Text),
    new OleDbParameter("@description",description.Text),
    new OleDbParameter("@purchasevalue",purchaseval.Value),
    new OleDbParameter("@salvagevalue",salvageval.Value),
    new OleDbParameter("@currency",currencies.SelectedIndex),
    new OleDbParameter("@dateacquired",purchasedate.Value),
    new OleDbParameter("@dateretire",purchasedate.Value.AddYears((int)lifetime.Value)),
    new OleDbParameter("@imagepath","N/A"),
    new OleDbParameter("@addedby",MDIParent1.User.ID)
    };
+6  A: 

You probably have one or more nullable types (perhaps your dates?) that don't have values, although it could also be that there is no SelectedValue. Check that all of your parameters are non-null before the statement in the debugger to see which.

tvanfosson
Good call. Merging in my answer: if you put each of those onto one line (not inside {}), you'll get a more useful stack trace. Or the debugger method, works nicely, too.
Michael Haren
I think the parameter values are coming from controls and the most likely culprit is the SelectedValue on assettypes.
Andrew Kennan
+1  A: 

It seems most likely that one of the objects you are querying is null.

I suggest printing them out to standard output.

Owen
A: 

One of these lines contains a null reference:

new OleDbParameter("@assetname",name.Text),
new OleDbParameter("@assettype",assettypes.SelectedValue.ToString()),
new OleDbParameter("@model",model.Text),
new OleDbParameter("@description",description.Text),
new OleDbParameter("@purchasevalue",purchaseval.Value),
new OleDbParameter("@salvagevalue",salvageval.Value),
new OleDbParameter("@currency",currencies.SelectedIndex),
new OleDbParameter("@dateacquired",purchasedate.Value),
new OleDbParameter("@dateretire",purchasedate.Value.AddYears((int)lifetime.Value)),
new OleDbParameter("@imagepath","N/A"),
new OleDbParameter("@addedby",MDIParent1.User.ID)

Look at the propery values in the debugger, there's really nothing we can do with the code you have provided.

Ed Swangren
A: 

hmm .. so it does not initialize the whole array thanks a lot

A: 

Really it looks like you are taking input directly from the user without validating or scrubbing it first. My advice, and this will probably solve your NRE, is to validate each user entered value before sending it to the database. This will allow you to catch any errors in input before you pig up the db with a bad query.

And please, dont name variables the same as the objects. I mean

DAL dal = new DAL();

at least underscore the local vars or use a different name.

DAL _dal = new DAL();
DAL dataAccessLayer = new DAL();
StingyJack