以下是该按钮的代码,但是当我单击该按钮时,它不会将我转发到所需的页面。
我的DataReader
循环有问题吗?
{
SqlConnection connBadge = new SqlConnection("Data Source =localhost;" +
"Initial Catalog = BreastCancer; Integrated Security = SSPI");
connBadge.Open();
SqlCommand cmdfBadge = new SqlCommand("SELECT * FROM Products WHERE pid=1", connBadge);
SqlDataReader dr;
dr = cmdfBadge.ExecuteReader();
while (dr.Read())
{
String pName = dr["pName"].ToString();
String pPrice = dr["pPrice"].ToString();
int b = Convert.ToInt16(pPrice);
int a = Convert.ToInt16(ddQty1.SelectedValue.ToString());
int g = a * b;
String Badge = "INSERT into Cart (Name,Price,Quantity,gPrice) Values('" + pName + "', '" + b + "', '" + a + "','" + g + "')";
SqlCommand cmdBadge = new SqlCommand(Badge, connBadge);
cmdBadge.ExecuteNonQuery();
}
dr.Close();
connBadge.Close();
Response.Redirect("Cart.aspx");
}
答案 0 :(得分:0)
使用try / catch语句包装代码以查看是否抛出任何异常。
答案 1 :(得分:0)
使用参数并尝试使用sqldata适配器:
{
SqlConnection connBadge = new SqlConnection("Data Source =localhost;" +
"Initial Catalog = BreastCancer; Integrated Security = SSPI");
connBadge.Open();
SqlCommand cmdfBadge = new SqlCommand("SELECT * FROM Products WHERE pid='1'", connBadge);
var dSet = new DataSet();
var dt = new Datatable();
var da = new SqlDataAdapter(cmdfBadge);
da.Fill(dSet);
dt = dSet.Tables[0];
foreach(Datarow a in dt.Rows)
{
String pName = a["pName"].ToString();
String pPrice = a["pPrice"].ToString();
int b = Convert.ToInt16(pPrice);
int a = Convert.ToInt16(ddQty1.SelectedValue.ToString());
int g = a * b;
String Badge = "INSERT into Cart (Name,Price,Quantity,gPrice) Values(@Name,@Price,@Quantity,@gPrice)";
SqlCommand cmdBadge = new SqlCommand(Badge, connBadge);
sqlCommand.Addwithvalue("@Name",pName);
sqlCommand.Addwithvalue("@Price",b)
sqlCommand.Addwithvalue("@Quantity",a)
sqlCommand.Addwithvalue("@gPrice",g)
cmdBadge.ExecuteNonQuery();
}
connBadge.Close();
Response.Redirect("Cart.aspx");
}
此致
答案 2 :(得分:0)
验证你的insert语句,我相信如果你的价格,数量和gprice的数据类型是整数类型,它将会失败:
String Badge = "INSERT into Cart (Name,Price,Quantity,gPrice) Values('" + pName + "', '" + b + "', '" + a + "','" + g + "')";
您不应将它们用引号括起来,删除引号然后重试。
String Badge = "INSERT into Cart (Name,Price,Quantity,gPrice) Values('" + pName + "', " + b + ", " + a + "," + g + ")";
将来尝试使用描述性变量名,using语句和try-catch语句。