Button can't forward me to next page
Following is the code for the button but when I click the button, it does not forward 开发者_JAVA技巧me to the desired page.
Is there something wrong with myDataReader
loop?
{
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");
}
Use parameter and try sqldata adapter:
{
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");
}
Regards
Wrap the code with a try/catch statement to see if any exceptions are thrown.
Verify your insert statement, I believe it will fail if the datatypes of your price, quantity and gprice are of type integer:
String Badge = "INSERT into Cart (Name,Price,Quantity,gPrice) Values('" + pName + "', '" + b + "', '" + a + "','" + g + "')";
You should not wrap these in quotes, remove the quotes and try again.
String Badge = "INSERT into Cart (Name,Price,Quantity,gPrice) Values('" + pName + "', " + b + ", " + a + "," + g + ")";
Try to use descriptive variable names, the using statement and a try-catch statement, in the future.
精彩评论