protected void UpdateShoppingCart()
{
if(AddProdCount==0)
{
CreateCartTable();
SubTotal.Text="0.00";
Tax.Text="0.00";
Total.Text="0.00";
}
else
{
DataTable nowTable=new DataTable("nowCartTable");
nowTable=(DataTable)Session["myCartTable"];
int pn=nowTable.Rows.Count;
int i=0;
bool hasone=false;
int nowProdID;
while(i<pn && !hasone)
{
nowProdID=Int32.Parse(nowTable.Rows[i][0].ToString());
if(nowProdID==AddProdID)
{
hasone=true;
}
else
{
i++;
}
}
if(hasone)
{
DataRow oldDR;
oldDR=nowTable.Rows[i];
oldDR["ProdCount"]=Int32.Parse(oldDR["ProdCount"].ToString())+1;
oldDR["TotalPrice"]=Int32.Parse(oldDR["ProdCount"].ToString())*Double.Parse(oldDR["UnitPrice"].ToString());
}
else
{
DataRow newDR;
double unitp;
SqlConnection myConnection=new SqlConnection("server=.;database=Aspnet;uid=sa;pwd=;");
string strSQL="select * from Products where ProductID="+AddProdID+"";
SqlDataAdapter myCommand=new SqlDataAdapter(strSQL,myConnection);
myCommand.Fill(ds,"Addp");
newDR=nowTable.NewRow();
newDR[0]=AddProdID;
newDR[2]=ds.Tables["AddP"].Rows[0]["ProductName"].ToString();
unitp=Double.Parse(ds.Tables["AddP"].Rows[0]["UnitPrice"].ToString());
newDR[3]=unitp;
newDR[4]=unitp;
nowTable.Rows.Add(newDR);
}
ShoppingCartDlt.DataSource=nowTable.DefaultView;
ShoppingCartDlt.DataBind();
Session["myCarTable"]=nowTable;
double subt=0;
pn=nowTable.Rows.Count;
for(i=0;i<pn;i++)
{
subt+=Double.Parse(nowTable.Rows[i]["TotalPrice"].ToString());
}
this.DisplayMoney(subt);
}
}
以上的程序显示了黑体字部分如下错误:
未将对象引用设置到对象的实例
黑体字上面的那一句在下面的代码里定义了(黑体部分)
protected void CreateCartTable()
{
DataTable newDT=new DataTable("CartTable");
ds.Tables.Add(newDT);
DataColumn newDC;
newDC=new DataColumn("ProdID",System.Type.GetType("System.Int32"));
ds.Tables["CartTable"].Columns.Add(newDC);
newDC=new DataColumn("ProdCount",System.Type.GetType("System.Int32"));
newDC.DefaultValue=1;
ds.Tables["CartTable"].Columns.Add(newDC);
newDC=new DataColumn("ProdName",System.Type.GetType("System.String"));
ds.Tables["CartTable"].Columns.Add(newDC);
newDC=new DataColumn("UnitPrice",System.Type.GetType("System.Double"));
ds.Tables["CartTable"].Columns.Add(newDC);
newDC=new DataColumn("TotalPrice",System.Type.GetType("System.Double"));
ds.Tables["CartTable"].Columns.Add(newDC);
Session["myCartTable"]=newDT;
ShoppingCartDlt.DataSource=ds.Tables["CartTable"].DefaultView;
ShoppingCartDlt.DataBind();
}