即使输入详细信息,会话变量仍显示为Null

时间:2018-07-09 16:53:35

标签: c# html

我正在尝试创建订单产品函数,但是我的会话变量似乎显示为Null,我检查了DataRoutine.cs以确保其中包含某些字符串,但它似乎不起作用。我还在SQL中将OOrderId数据属性设置为(IS Identity):是

这是我得到的错误:对象引用未设置为对象的实例。

第72行:intOrderNo =(int)Session [“ sOrderNo”];

这是我的代码:

string="/opt=920MB;4512;4917;0;4855"
echo $string | awk '{split($0,a,";"); print a[1],a[5]}' | sed 's#=.* #=#'
/opt=4855

}

这很重要,这是我的dataroutine.cs页面

这是代码:

public partial class ProductDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    qtytxt.Attributes.Add("placeholder", "Your Quantity");
}
static readonly string scriptStockOut = "<script language=\"javascript\">\n" +
"alert (\"Sorry Stock Out!  Please choose a smaller quantity or another product \");\n" +
"</script>";

static readonly string scriptErrorLogin = "<script language=\"javascript\">\n" +
   "alert (\"Please login or create account first to facilitate buying\");\n</script>";

protected void atcbtn_Click(object sender, EventArgs e)
{
    string strProductId, strSQL;
    int intQuantityOnHand, intBuyQuantity, newQty, intOrderNo;
    decimal decUnitPrice;

    if ((string)Session["sFlag"] != "T")
    {
        Type csType = this.GetType();
        ClientScript.RegisterStartupScript(csType, "Error", scriptErrorLogin); return;
    }

    SqlConnection sqlCon = new SqlConnection(@"Data Source=teafamily;Initial Catalog=BolsenF1;Integrated Security=True;MultipleActiveResultSets=true;");
    sqlCon.Open();
    Type csTypee = this.GetType();
    SqlCommand sqlcmd;
    SqlDataReader rdr;
    string strSQLSelect = "SELECT pProductID FROM Products";
    sqlcmd = new SqlCommand(strSQLSelect, sqlCon);
    rdr = sqlcmd.ExecuteReader();

    DetailsViewRow row0 = DetailsView1.Rows[0];
    strProductId = row0.Cells[1].Text;

    strSQLSelect = "SELECT pQty FROM Products WHERE pProductID=@ProductID";
    sqlcmd = new SqlCommand(strSQLSelect, sqlCon);
    sqlcmd.Parameters.AddWithValue("@ProductID", strProductId);
    object oQty = sqlcmd.ExecuteScalar();
    intQuantityOnHand = (int)oQty;

    strSQLSelect = "SELECT pPrice FROM Products WHERE pProductID=@ProductID";
    sqlcmd = new SqlCommand(strSQLSelect, sqlCon);
    sqlcmd.Parameters.AddWithValue("@ProductID", strProductId);
    object oUnitPrice = sqlcmd.ExecuteScalar();
    decUnitPrice = (decimal)oUnitPrice;

    intBuyQuantity = int.Parse(qtytxt.Text);

    newQty = intQuantityOnHand - intBuyQuantity;

    if (intQuantityOnHand < intBuyQuantity)
    {
        Type csType = this.GetType();
        ClientScript.RegisterStartupScript(csType, "StockOut", scriptStockOut);
    }

    Session["sProductId"] = strProductId;
    Session["sUnitPrice"] = decUnitPrice.ToString();
    Session["sQuantity"] = newQty.ToString();

    **intOrderNo = (int)Session["sOrderNo"];**
    strSQL = "INSERT INTO orderItems(iOrderNo,iProductID, iQty, iUnitPrice)"
        + "VALUES (@OrderNo, @ProductID, @Qty, @UnitPrice)";
    sqlcmd = new SqlCommand(strSQL, sqlCon);
    sqlcmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
    sqlcmd.Parameters.AddWithValue("@ProductID", strProductId);
    sqlcmd.Parameters.AddWithValue("@Qty", intBuyQuantity);
    sqlcmd.Parameters.AddWithValue("@UnitPrice", decUnitPrice);
    sqlcmd.ExecuteNonQuery();

    strSQL = "UPDATE Products SET pQty=@NewQty WHERE pProductID = @ProductID";
    sqlcmd = new SqlCommand(strSQL, sqlCon);
    sqlcmd.Parameters.AddWithValue("@NewQty", newQty);
    sqlcmd.Parameters.AddWithValue("@ProductID", strProductId);
    sqlcmd.ExecuteNonQuery();

    sqlCon.Close();

    Response.Redirect("ShoppingCart.aspx");

}

}

0 个答案:

没有答案