0

Error shown on the website. Im using asp net visual C# webform, access data source (MS access) When I click on Add to Cart button on productdetails.aspx

Line 41:         int intOrderNo = (int)Session["sOrderNo"];
Line 42:         string strUnitPrice = (string)Session["sUnitPrice"];
Line 43:         decimal decUnitPrice = decimal.Parse(strUnitPrice);

For myOrder table in Ms Access There is oOrderNo, oDate, oUserName, oPaymentMode, oStatus,

 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    // test to remind customer to login first
    if ((string)Session["sFlag"]!="T")
    {
        Type csType = this.GetType();
        ClientScript.RegisterStartupScript(csType, "Error", scriptErrorLogin);
    }

    // Connect to database  
    OleDbConnection mDB = new OleDbConnection();
    mDB.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data source=" + Server.MapPath("~/App_Data/webBase.accdb");
    mDB.Open();
    OleDbCommand cmd;
    DetailsViewRow row0 = DetailsView1.Rows[0];
    string strProductID = row0.Cells[1].Text;
    mDB.Close();

    // save as session variables
    Session["sProductID"] = strProductID;
    DetailsViewRow row4 = DetailsView1.Rows[4];
    Session["sUnitPrice"] = row4.Cells[1].Text;


    int intOrderNo = (int)Session["sOrderNo"];
    string strUnitPrice = (string)Session["sUnitPrice"];
    decimal decUnitPrice = decimal.Parse(strUnitPrice);
    string strSQL = "INSERT INTO orderItems(uOrderNo, uProductID, uUnitPrice)" + "VALUES(@eOrderNo, @eProductID, @eUnitPrice)";
    cmd = new OleDbCommand(strSQL, mDB);
    cmd.Parameters.AddWithValue("@eOrderNo", intOrderNo);
    cmd.Parameters.AddWithValue("@eProductID", strProductID);
    cmd.Parameters.AddWithValue("@eUnitPrice", decUnitPrice);

    mDB.Open();
    cmd.ExecuteNonQuery();
    mDB.Close();

    Response.Redirect("ShoppingCart.aspx");
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Marcus Moo
  • 196
  • 1
  • 2
  • 20
  • Kindly check the session variable values and add the full stack trace – Sureshkumar Panneerselvan Jan 20 '14 at 04:05
  • Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jan 20 '14 at 04:05
  • use Convert.toString() – ssilas777 Jan 20 '14 at 04:07
  • `Line 42: string strUnitPrice = (string)Session["sUnitPrice"];` can be `Session["sUnitPrice"].ToString()` – Zigma Jan 20 '14 at 04:11
  • @Kayzel Moo : I see that you are trying to cast the value which is stored in a session. in this case the only reason why you get Object reference error would be because session is empty or does not exists. – Venkatesh Ellur Jan 20 '14 at 05:40
  • Before you retrieve and cast value from session, Check for the session is null or not. See my answer for the same. – SpiderCode Jan 20 '14 at 06:03
  • @VenkateshEllur I thank you all for your help. You all list different methods but I'm quite confused which to use – Marcus Moo Jan 21 '14 at 01:36
  • @VenkateshEllur the error is wrong with int(intOrderNo = (int)Session["sOrderNo"]; most of the time it can work, sometimes it dont, so im confused. – Marcus Moo Jan 21 '14 at 01:41
  • @Zigma the error is wrong with int(intOrderNo = (int)Session["sOrderNo"]; most of the time it can work, sometimes it dont, so im confused. – Marcus Moo Jan 21 '14 at 01:41
  • @ssilas777 convert.tostring() don't work because you cant convert string to int – Marcus Moo Jan 21 '14 at 01:44
  • (string)Session["sUnitPrice"]; - I was pointing this lines, as convert.tostring handles null values.BTW you can convert string("3") to int. – ssilas777 Jan 21 '14 at 04:49

3 Answers3

1

Try this

Line 41:         int intOrderNo = Session["sOrderNo"] == DBNull.Value ? 0 : (int)Session["sOrderNo"];
Line 42:         string strUnitPrice = Session["sUnitPrice"] == DBNull.Value ? string.Empty : (string)Session["sUnitPrice"];

null Vs DBNull.Value

Community
  • 1
  • 1
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
0

Try below code :

Line 41:         int intOrderNo = Session["sOrderNo"] == null ? 0 : (int)Session["sOrderNo"];
Line 42:         string strUnitPrice = Session["sUnitPrice"] == null ? string.Empty : (string)Session["sUnitPrice"];
Line 43:         decimal decUnitPrice = string.IsNullOrWhiteSpace(strUnitPrice) ? 0 : decimal.Parse(strUnitPrice);
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
0

Just check that if Session variable is Null-

 if( Session["sOrderNo"] != null && all the session variables )
{
     //Now check your condition here
}
else {
       //Perform any operation
    }