Click here to Skip to main content
14,695,660 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m using the following code for insert values to the database. But Its have a soe error"
An unhandled exception of type 'System.NullReferenceException' occurred in TradingDAL.dll. Additional information: Object reference not set to an instance of an object."
I need to a help. please help me . Thanks in advance

dbConnection conn = new dbConnection();
       public int AddPurchase(IPurchase purchase)
       {
       string query = @"INSERT INTO purchase_account1 (posted_date, supplier_id, due_date, date_stamp, purchase_description, transaction_type,
                                               net_totalDiscount, net_purchase_total, net_total)VALUES  (@posted_date,@ supplier_name, @due_date, @date_stamp, @purchase_description,@transaction_type,@net_totalDiscount, @net_purchase_total, @net_total);SELECT purchase_id=SCOPE_IDENTITY()";

           SqlParameter[] parameter = new SqlParameter[9];

           parameter[0] = new SqlParameter("@posted_date", SqlDbType.Date);
           parameter[0]Value = ((DateTime)purchase.Posted).Date;

           parameter[1] = new SqlParameter("@supplier_name", SqlDbType.VarChar);
           parameter[1].Value =purchase.Supplier_Name.ToString() ;

           parameter[2] = new SqlParameter("@due_date", SqlDbType.Date);
           parameter[2].Value =((DateTime)purchase.DueDate).Date;

           parameter[3] = new SqlParameter("@date_stamp", SqlDbType.VarChar);
           parameter[3].Value = Convert.ToString(DateTime.Now);

           parameter[4] = new SqlParameter("@purchase_description", SqlDbType.VarChar);
           parameter[4].Value = purchase.Description.ToString();
          // string s = purchase.Description;
           parameter[5] = new SqlParameter("@transaction_type", SqlDbType.VarChar);
           parameter[5].Value = purchase.Transaction_type.ToString();

           parameter[6] = new SqlParameter("@net_totalDiscount", SqlDbType.Decimal);
           parameter[6].Value =((Decimal) purchase.Discount);

           parameter[7] = new SqlParameter("@net_purchase_total", SqlDbType.Decimal);
           parameter[7].Value = ((Decimal)purchase.Total);

           parameter[8] = new SqlParameter("@net_total", SqlDbType.Decimal);
           parameter[8].Value = ((Decimal)purchase.NetTotal);


           DataTable data = conn.executeSelectQuery(query, parameter);
           int purchaseid = Convert.ToInt32(data.Rows[0][0]);
           return purchaseid;
       }
Posted
Comments
Dave Kreskowiak 10-Dec-15 16:04pm
   
You might want to say which line throws the exception.

Bascially, this is the easist exception to troubleshoot. All you have to do is examine the contents of the variables in the debugger on the line that threw the error and look for the variable that is null. Learn to use it.
Member 12167639 10-Dec-15 17:59pm
   
thanks
CHill60 10-Dec-15 20:34pm
   
An appropriate response would have been to update your post showing which line throws the exception!
ZurdoDev 10-Dec-15 16:28pm
   
This is a very easy error to fix on your own. In fact, since you did not tell us which line causes it we can't know for sure how to fix it anyway. Just look at the line that caused the error and something will be null. Just fix that and you're good.
Member 12167639 10-Dec-15 18:00pm
   
thanks

1 solution

As mentioned in the comments, this is actually a very easy error to fix on your own. In fact, since you did not tell us which line causes it we can't know for sure how to fix it anyway. Just look at the line that caused the error and something will be null. Just fix that and you're good.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900