SqlCommand INSERT INTO only selected rows by checkbox

  • Thread starter Thread starter Marek Šíp
  • Start date Start date
M

Marek Šíp

Guest
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" ?

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.Cells["text"].Value);
prikaz2.Parameters.AddWithValue("@val2", dtg_ksluzby.Rows.Cells["pocet"].Value);
prikaz2.Parameters.AddWithValue("@val3", dtg_ksluzby.Rows.Cells["akce"].Value);
prikaz2.Parameters.AddWithValue("@val4", max + 1); spojeni.Open();
prikaz2.ExecuteNonQuery();
spojeni.Close();
}

This is my code for selecting row

private void dtg_ksluzby_CellValueChanged(object sender,
DataGridViewCellEventArgs 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
{
}
}

Continue reading...
 
Back
Top