Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
in windows form C# SQL server.. I have datagridView1 with a checkBoxColumn
C#
private void buttonUpdate_Click(object sender, EventArgs e)
        {

            {
                List<DataGridViewRow> selectedRows = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
                                                      where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true
                                                      select row).ToList();
                if (MessageBox.Show(string.Format("Do you want to Update {0} rows?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in selectedRows)
                    {
                        using (SqlConnection con = new SqlConnection(ConnectionString))
                        {

                            using (SqlCommand cmd = new SqlCommand("update Customers set Name=@Name,Country=@Country,Mark = (@Mark + 1) WHERE CustomerId = @CustomerId", con))
                            {
                                cmd.CommandType = CommandType.Text;
                                cmd.Parameters.AddWithValue("@CustomerId", row.Cells["CustomerId"].Value);
                                cmd.Parameters.AddWithValue("@Name", row.Cells["Name"].Value);
                                cmd.Parameters.AddWithValue("@Mark", row.Cells["Mark"].Value);
                                con.Open();
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                        }
                    }

                    this.BindGrid();
                }

            }

        }

when the user Tick the checkbox in datagridview and submit by click button... the Stored Procedures will execute it. then the "Selected row" will be updated.
just I want this query to be in a Stored Procedures

SQL
"update Customers set Name=@Name,Mark = (@Mark + 1) WHERE CustomerId = @CustomerId""
Posted
Comments
Maher_ITbv 20-Nov-15 12:07pm    
Actually I trust only "Code Project Company" and its my school
Suvendu Shekhar Giri 20-Nov-15 12:17pm    
So what is the problem?
Just convert your query to a stored procedure.

1 solution

You just need to convert your query to a stored procedure.

Your stored procedure should look something like-
SQL
CREATE PROCEDURE [dbo].[UpdateCustomer]
(
  @CustomerId INT,
  @Name VARCHAR(200), --size should be same as that in field definition
  @Mark INT, --datatype should be same as in field definiton
)
AS
BEGIN
  UPDATE Customers 
  SET Name=@Name,Mark = (@Mark + 1) 
  WHERE CustomerId = @CustomerId
END


If it doesn't help or you need further help implementing this, please let me know :)
 
Share this answer
 

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