加入购物车功能

时间:2018-07-09 12:17:34

标签: html

我正在尝试使用分配给订单的订单号执行添加到购物车功能,但出现一条错误消息: 对象引用未设置为对象的实例。

源错误:

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

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

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

}

}

如果要紧的话,我也有一个dataroutine.cs页面

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data.Sql;

/// <summary>
/// Summary description for DataRoutine
/// </summary>
public class DataRoutine:System.Web.UI.Page
{
public DataRoutine()
{
    //
    // TODO: Add constructor logic here
    //
}
public void CreateRecs(string strCustId)
{
    SqlConnection mDB = new SqlConnection(@"Data Source=TEAFAMILY;Initial Catalog=BolsenF1;Integrated Security=True; MultipleActiveResultSets=true;");
    SqlCommand cmd;
    SqlDataReader rdr;
    string strSql;
    string strOFlag = "F";

    // check to see if there is an active order record
    strSql = "SELECT OStatus FROM Orders WHERE OCustID = @CustId "
        + "AND OStatus =\"Ordering\" ORDER BY OOrderID DESC;";
    cmd = new SqlCommand(strSql, mDB);
    cmd.Parameters.Add("@CustId", strCustId);
    mDB.Open();
    rdr = cmd.ExecuteReader();
    Boolean booRows = rdr.HasRows;
    if (booRows)  // when booRows is true, there are order records for the user 
    {
        rdr.Read();
        if ((string)rdr["OStatus"] != "Ordering") //status of an active order is "Ordering"
        {
            strOFlag = "T"; // "T" means there is a need to create a new Orders record
        }
    }
    else { strOFlag = "T"; }
    mDB.Close();
    if (strOFlag == "T")
    {
        // insert a new order record to create a new order number in the database
        String strStatus = "Ordering";
        strSql = "INSERT INTO Orders (OCustID, OStatus) VALUES (@CustId, @Status)";
        cmd = new SqlCommand(strSql, mDB);
        cmd.Parameters.Add("@CustId", strCustId);
        cmd.Parameters.Add("@Status", strStatus);
        mDB.Open();
        cmd.ExecuteNonQuery();
        mDB.Close();

    }

    // retrieve order No - this order No is needed when the user buys an item
    strSql = "SELECT OOrderID FROM Orders WHERE OCustID = @CustId "
        + " AND OStatus =\"Ordering\" ORDER BY OOrderID DESC;";
    cmd = new SqlCommand(strSql, mDB);
    cmd.Parameters.Add("@CustId", strCustId);
    mDB.Open();
    rdr = cmd.ExecuteReader();
    rdr.Read();
    Session["sOrderNo"] = rdr["OOrderID"];  // save order no in session variable sOrderNo
    mDB.Close();
    return;
} //end of CreateRecs method

}

0 个答案:

没有答案