Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
suppose,
i have one grid view with check box.
when i checked multiple check box those values are store in database...

i have gridview with checked check box and i want to insert that checked row in database..
if i checked one row then it will insert that row in the database.. if i checked two rows then it will insert those two rows in the database...
Posted
Updated 11-Apr-12 2:01am
v2
Comments
[no name] 11-Apr-12 7:04am    
OK, now lets suppose you actually provide useful information and ask an actual question.

1 solution

Hi ,
Try this
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (((CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox1")) is CheckBox)
        {
            using (
                SqlConnection con =
                    new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("UPDATE dbo.cate  SET cate = @cate , chk = @chk  where id  = @id", con);
                cmd.Parameters.AddWithValue("@cate",
                                            ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text);

                cmd.Parameters.AddWithValue("@chk",
                                            ((CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox1")).Checked);

                cmd.Parameters.AddWithValue("@id",
                                            ((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text);

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                GridView1.EditIndex = -1;
                bind();
            }
        }


Best Regards
M.Mitwalli
 
Share this answer
 
v3
Comments
[no name] 11-Apr-12 8:14am    
Try researching using blocks and SQL Injection

SqlCommand.CommandType defaults to Text so there is no reason to specifically set it.

It is also very inefficient to create the SqlConnection and SqlCommand each time in this method.
Mohamed Mitwalli 11-Apr-12 8:52am    
Thanks mark for the notification ,But I already know about the SQL Injection and using block but i want to make it so simple Maybe you will Disagree but that's what i thought .
[no name] 11-Apr-12 9:00am    
Yes, it is much easier to write poor code and say you know better
Mohamed Mitwalli 11-Apr-12 9:51am    
check this one
Mohamed Mitwalli 11-Apr-12 9:51am    
if you got any notification let me know .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900