网站上显示错误。我使用asp net visual C#webform,访问数据源(MS访问)当我点击productdetails.aspx上的Add to Cart按钮时
Line 41: int intOrderNo = (int)Session["sOrderNo"];
Line 42: string strUnitPrice = (string)Session["sUnitPrice"];
Line 43: decimal decUnitPrice = decimal.Parse(strUnitPrice);
对于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");
答案 0 :(得分:1)
试试这个
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"];
答案 1 :(得分:0)
尝试以下代码:
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);
答案 2 :(得分:0)
检查Session变量是否为Null -
if( Session["sOrderNo"] != null && all the session variables )
{
//Now check your condition here
}
else {
//Perform any operation
}