简体   繁体   English

NullReferenceException:对象引用未设置为对象#2的实例

[英]NullReferenceException: Object reference not set to an instance of an object #2

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 我正在使用ASP Net Visual C#Webform,访问数据源(MS Access)当我单击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, 对于Ms Access中的myOrder表,有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");

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 空vs DBNull.Value

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);

Just check that if Session variable is Null- 只需检查Session变量是否为Null-

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:NullReferenceException:未将对象引用设置为对象的实例 - ERROR: NullReferenceException: Object reference not set to an instance of an object NullreferenceException - 对象引用未设置为对象的实例 - NullreferenceException - Object Reference is not set to an instance of a object 未处理NullReferenceException,未将对象引用设置为对象的实例 - NullReferenceException was unhandled, Object Reference not set to an instance of an object 数据存储库实例Null-NullReferenceException吗? 你调用的对象是空的 - Data Repository instance Null - NullReferenceException? Object reference not set to an instance of an object XDocument.Descendants()返回NullReferenceException:未将对象引用设置为对象的实例 - XDocument.Descendants() returns NullReferenceException : Object reference not set to an instance of an object System.NullReferenceException - 未将对象引用设置为对象的实例 - System.NullReferenceException – Object reference not set to an instance of an object Foreach System.NullReferenceException:未将对象引用设置为对象的实例 - Foreach System.NullReferenceException: Object reference not set to an instance of an object NullReferenceException:当用户单击按钮时“未将对象引用设置为对象的实例” - NullReferenceException: “Object reference not set to an instance of an object” when user clicks a button unity c# - NullReferenceException:未将对象引用设置为对象的实例 - Unity c# - NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:对象引用未设置为对象的实例 - System.NullReferenceException: Object reference not set to an instance of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM