我正在尝试使用for-if循环来运行一些CheckBoxList控件,以查看是否选中了复选框。如果选中,它将向DB运行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");
}
}
答案 0 :(得分:2)
该行
Response.Redirect("Delivery.aspx");
位于for
循环内。所以第一次进入循环时,你已经被重定向了。只需将重定向移至for
循环后。
你应该严格考虑重构你的代码。除了其他方面,还有太多重复的东西可以转换为方法。