我正在尝试使用 for-if 循环来运行几个 CheckBoxList 控件,以查看是否选中了复选框。如果选中,它将向数据库运行 INSERT 语句。
我的第一个 CheckBoxList (stapleCheckBoxList) 可以正常工作(例如,如果我选择了所有 4 个复选框,则相应的项目将被插入到数据库中)。但是,对于我的第二个 CheckBoxList (seafoodCheckBoxList),它似乎只在第一个选中的复选框上执行 INSERT。谁能找出我下面的代码有什么问题?
protected void btnTSubmit_Click(object sender, EventArgs e)
{
int i;
int intOrderNo = (int)Session["sOrderNo"];
int Qty;
int UnitPrice;
string ProdId;
if (txtPaxQty.Text != String.Empty)
{
UnitPrice = 12;
Qty = int.Parse(txtPaxQty.Text);
ProdId = "PK001";
OleDbConnection DBconn2 = new OleDbConnection();
DBconn2.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data source="
+ Server.MapPath("~/App_Data/ParissDB.mdb");
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = DBconn2;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT INTO itemsTable(iOrderNo, iProductId, iQty, iUnitPrice)"
+ "VALUES (@OrderNo, @ProductId, @Qty, @UnitPrice)";
cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", ProdId);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
DBconn2.Open();
cmd.ExecuteNonQuery();
DBconn2.Close();
}
for (i = 0; i < stapleCheckBoxList.Items.Count; i++)
{
String str = "";
if (stapleCheckBoxList.Items[i].Selected)
{
str = stapleCheckBoxList.Items[i].Text;
OleDbConnection DBconn = new OleDbConnection();
DBconn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data source="
+ Server.MapPath("~/App_Data/ParissDB.mdb");
Qty = 0;
UnitPrice = 0;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = DBconn;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT INTO itemsTable(iOrderNo, iProductId, iQty, iUnitPrice)"
+ "VALUES (@OrderNo, @ProductId, @Qty, @UnitPrice)";
//Staple
if (stapleCheckBoxList.Items[i].Text == "Yang Chow Fried Rice")
{
str = "ST001";
cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", str);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
DBconn.Open();
cmd.ExecuteNonQuery();
DBconn.Close();
}
if (stapleCheckBoxList.Items[i].Text == "Pineapple Fried Rice")
{
str = "ST002";
cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", str);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
DBconn.Open();
cmd.ExecuteNonQuery();
DBconn.Close();
}
if (stapleCheckBoxList.Items[i].Text == "Mee Goreng")
{
str = "ST003";
cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", str);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
DBconn.Open();
cmd.ExecuteNonQuery();
DBconn.Close();
}
if (stapleCheckBoxList.Items[i].Text == "Fried Udon with Seafood")
{
str = "ST004";
cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", str);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
DBconn.Open();
cmd.ExecuteNonQuery();
DBconn.Close();
}
//Response.Redirect("Delivery.aspx");
}
}
for (i = 0; i < seafoodCheckBoxList.Items.Count; i++)
{
String str2 = "";
if (seafoodCheckBoxList.Items[i].Selected)
{
str2 = seafoodCheckBoxList.Items[i].Text;
OleDbConnection DBconn = new OleDbConnection();
DBconn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data source="
+ Server.MapPath("~/App_Data/ParissDB.mdb");
Qty = 0;
UnitPrice = 0;
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = DBconn;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT INTO itemsTable(iOrderNo, iProductId, iQty, iUnitPrice)"
+ "VALUES (@OrderNo, @ProductId, @Qty, @UnitPrice)";
//Seafood
if (seafoodCheckBoxList.Items[i].Text == "Butter Prawns")
{
str2 = "SE002";
cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", str2);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
DBconn.Open();
cmd.ExecuteNonQuery();
DBconn.Close();
}
if (seafoodCheckBoxList.Items[i].Text == "Cereal Prawns")
{
str2 = "SE001";
cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", str2);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
DBconn.Open();
cmd.ExecuteNonQuery();
DBconn.Close();
}
if (seafoodCheckBoxList.Items[i].Text == "Thai Style Prawns")
{
str2 = "SE003";
cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", str2);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
DBconn.Open();
cmd.ExecuteNonQuery();
DBconn.Close();
}
if (seafoodCheckBoxList.Items[i].Text == "BBQ Squid")
{
str2 = "SE004";
cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", str2);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@UnitPrice", UnitPrice);
DBconn.Open();
cmd.ExecuteNonQuery();
DBconn.Close();
}
Response.Redirect("Delivery.aspx");
}
}