Database updating error

Keltron / Lm525
March 9, 2009 at 02:00:18
Specs: Windows XP, 512mb
I have created a Gridview and added a command button to insert the selected value to database table. But it adds twice to the database. can u tell why? the code is given below....
protected void productviewh_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
String a, b, c, d, g;
SqlConnection con = new SqlConnection("server=.;database=online;uid=sa");
SqlCommand cmd1 = new SqlCommand("select pid from products where pid=@pid", con);
con.Open();
cmd1.Parameters.Add("@pid", SqlDbType.Int).Value = Convert.ToInt32(productviewh.DataKeys[e.NewSelectedIndex].Values[0].ToString());
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
prid1.Text = Convert.ToString(dr1[0]);
}
con.Close();
con.Open();
SqlCommand cmd2 = new SqlCommand("select pname,price,image,cname,model from products where pid=" + this.prid1.Text + "", con);
SqlDataReader dr2 = cmd2.ExecuteReader();
if (dr2.Read())
{
g = Convert.ToString(dr2[4]);
d = Convert.ToString(dr2[3]);
c = Convert.ToString(dr2[2]);
b = Convert.ToString(dr2[1]);
a = Convert.ToString(dr2[0]);
}

con.Close();
con.Open();
SqlCommand cmd3 = new SqlCommand("insert into selected values('" + this.prid1.Text + "','" + a + "','" + b + "','" + c + "','" + d + "','" + g + "','" + this.userid1.Text + "')", con);
cmd3.ExecuteNonQuery();
con.Close();
}


See More: Database updating error

Report •


#1
March 9, 2009 at 05:29:45
The event fires twice.

Report •
Related Solutions


Ask Question