0

Hello guys I have got these SqlCommands which INSERT INTO and SELECT. I need to add to prikaz2 (which INSERT INTO) 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" ?

  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

     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
         {
         }
     }

2 Answers 2

2

So replace your for loop with a ForEach iterating over dtg_ksluzby.Rows and perform the check to see if it is checked inside the loop.

foreach (DataGridViewRow row in dtg_ksluzby.Rows)
        {
            if (Convert.ToBoolean(row.Cells[3].Value) == true) // check this line here
            {
                SqlCommand prikaz2 = new SqlCommand("INSERT INTO klisluz(text,pocet,akce,subkey) values(@val1,@val2,@val3,@val4) ", spojeni); 
                prikaz2.Parameters.AddWithValue("@val1", row.Cells["text"].Value);
                prikaz2.Parameters.AddWithValue("@val2", row.Cells["pocet"].Value);
                prikaz2.Parameters.AddWithValue("@val3", row.Cells["akce"].Value);
                prikaz2.Parameters.AddWithValue("@val4", max + 1);                     
                spojeni.Open();
                prikaz2.ExecuteNonQuery();
                spojeni.Close();
            }
        }
Sign up to request clarification or add additional context in comments.

4 Comments

Well I need to do this on button click so the INSERT INTO is in button click. Would you please help me with coding it ?
Wouldn't this do it ? I've not tested this so the syntax may be out but it should point you in the right direction ?
Well it says 2 errors: The name "chk1" doesnt exist in current context. The name "i" doesnt exist in current context.
try the last edit and then do some digging. what you need to do is check the cell in the row that contains your checkbox is ticked before you run your insert command.
2

how about joining the two statement?

INSERT INTO klisluz(text,pocet,akce,subkey) 
VALUES (@val1,
        @val2,
        @val3,
        COALESCE((Select MAX (ID_K) FROM klient) + 1, 1)

1 Comment

This is great, but how can I INSERT INTO only selected rows?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.