Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys I have got these SqlCommands which inserts and selects. I need to add to prikaz2 (which inserts) condition it would INSERT INTO only these rows wich were selected by checkbox. I have wondered about this for quiet long time but none of my codes were able to do that. Do you guys would suggest me something? In my opinion I have to add "WHERE" ?
C#
SqlCommand comm = new SqlCommand("Select MAX (ID_K) FROM klient", spojeni);
          spojeni.Open();
          int max = (int)comm.ExecuteScalar();
          spojeni.Close();

          for (int i = 0; i < dtg_ksluzby.Rows.Count; i++)
          {
              SqlCommand prikaz2 = new SqlCommand("INSERT INTO klisluz(text,pocet,akce,subkey) values(@val1,@val2,@val3,@val4) ", spojeni); // here I need to add condition to INSERT INTO only selected rows
              prikaz2.Parameters.AddWithValue("@val1", dtg_ksluzby.Rows[i].Cells["text"].Value);
              prikaz2.Parameters.AddWithValue("@val2", dtg_ksluzby.Rows[i].Cells["pocet"].Value);
              prikaz2.Parameters.AddWithValue("@val3", dtg_ksluzby.Rows[i].Cells["akce"].Value);
              prikaz2.Parameters.AddWithValue("@val4", max + 1);                     spojeni.Open();
              prikaz2.ExecuteNonQuery();
              spojeni.Close();
}
   private void dtg_ksluzby_CellValueChanged(object sender,
                                 DataGridViewCellEventArgs e)
This is my code for selecting row.e
C#
foreach (DataGridViewRow row in dtg_ksluzby.Rows)
{

    DataGridViewCheckBoxCell chk1 = row.Cells[3] as DataGridViewCheckBoxCell;
    if (Convert.ToBoolean(chk1.Value) == true)
    {

        MessageBox.Show("Služba byla vybrána");
    }
    else
    {
    }
}
Posted
Updated 17-Jul-13 4:51am
v3

First of all, i would suggest you to NOT use insert/update/delete statements in code. Have you ever heard about SQL injection[^]? Better way is to use stored procedures[^].


Second, you need to declare variable (for example) oCell, type: DataGridViewCheckBoxCell. Then you need to cast specific cell to DataGridViewCheckBoxCell to read its checked property value.

C#
DataGridViewCheckBoxCell oCell;
foreach (DataGridViewRow row in dgv1.Rows)
{
   //oCell = row.Cells["name of checkboxcolumn"] as DataGridViewCheckBoxCell;
   oCell = row.Cells[checkboxcolum_index] as DataGridViewCheckBoxCell;
   bool bChecked = (null != oCell && null != oCell.Value && true == (bool)oCell.Value);
   if (true == bChecked)
   {
      //call stored procedure here
   }
}


HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET[^]
Walkthrough: Using Only Stored Procedures (C#)[^]
How to: Execute a Stored Procedure that Returns No Value[^]
How to: Execute a Stored Procedure that Returns Rows[^]
 
Share this answer
 
Hello,
U can try it:

C#
for (int i = 0; i < dgv1.Rows.Count; i++)
{
    if((bool)this.dgv1.Rows[i].Cells["checkBoxColumn"].Value == true)
    {
        SqlCommand prikaz2 = new SqlCommand("INSERT INTO klisluz(text,pocet,akce,subkey) values(@val1,@val2,@val3,@val4) ", spojeni); // here I need to add condition to INSERT INTO only selected rows
        prikaz2.Parameters.AddWithValue("@val1", dtg_ksluzby.Rows[i].Cells["text"].Value);
        prikaz2.Parameters.AddWithValue("@val2", dtg_ksluzby.Rows[i].Cells["pocet"].Value);
        prikaz2.Parameters.AddWithValue("@val3", dtg_ksluzby.Rows[i].Cells["akce"].Value);
        prikaz2.Parameters.AddWithValue("@val4", max + 1); spojeni.Open();
        prikaz2.ExecuteNonQuery();
        spojeni.Close();
    }
} 


Best regards!
 
Share this answer
 
Comments
mareksip 17-Jul-13 11:11am    
Hello, first of all I would like to thank you for answering. On the other hand your code gives this error: Object reference not set to an instance of an object.

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