why does itemcommand fires on page refresh or reload
i am using a Datalist to display products on my page .... and i am using item command to store value of corresponding row using link button inside datalist .. once i click addtocart button .. the corresponding item information store in database , but the same event fires happens again even if i click refresh button of my browser .. that is the value again enter in database even on refreshing the page
here is my code of itemcommand:
public void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionSt rings["eshopConnectionString"].ConnectionString);
//ShoppingCartID is value of session that i have created when user login
UserId = shoppingCartId;
conn.Open();
string prodname = ((Label)DataList1.Items[e.Item.ItemIndex].FindControl("l1")).Text;
string produrl = ((Image)DataList1.Items[e.Item.ItemIndex].FindControl("sh1")).ImageUrl.ToString();
int prodprice = int.Parse(((Label)DataList1.Items[e.Item.ItemIndex].FindControl("l2")).Text);
string productid = ((Label)DataList1.Items[e.Item.ItemIndex].FindControl("l3")).Text;
SqlCommand cmd1 = new SqlCommand(("select * from cartnew where productid ='" + productid + "'and userid='" + UserId + "' "), conn);
DataSet ds1 = new DataSet();
SqlDataAdapter adp1 = new SqlDataAdapter();
adp1.SelectCommand = cmd1;
adp1.Fill(ds1);
int k = ds1.Tables[0].Rows.Count;
if (k == 0)
{
if (IsPostBack)
{
//InsertData is a function to insert value of item if it doesnt exist in table
InsertData("insert into cartnew(Prodname,prodprice,produrl,userid,quantity ,productid)values('" + prodname + "','" + prodprice + "','" + produrl + "','" + UserId + "','" + 1 + "','" + productid + "')");
}
}
//to update quantity if item already exist in table
else
{
SqlCommand cmd2 = new SqlCommand("select * from cartnew where productid ='" + productid + "'and userid='" + UserId + "'", conn);
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd2;
DataSet ds = new DataSet();
adp.Fill(ds);
int val = int.Parse(ds.Tables[0].Rows[0]["quantity"].ToString());
string s1 = ds.Tables[0].Rows[0]["productid"].ToString();
string s2 = ds.Tables[0].Rows[0]["userid"].ToString();
val = val + 1;
SqlCommand cmd3 = new SqlCommand("update cartnew set quantity='" + val + "' where productid ='" + s1 + "'and userid='" + s2 + "'", conn);
cmd3.ExecuteNonQuery();
}
|