I want to batch save the selected rows with chechbox. How can I do this?

I want to batch save the selected rows with chechbox. How can I do this?

Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jun 4, 2023, 6:18 AM EDT
Hi ,
First, you must fetch all records on the selected index change event in Gridview.
After that follow the instructions below,
using (SqlConnection conn = new SqlConnection("your-connection-string")) { using (SqlCommand command = new SqlCommand("", conn)) { try { conn.Open(); //Bulk insert into temp table using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conn)) { bulkcopy.BulkCopyTimeout = 660; bulkcopy.DestinationTableName = "Products_Temp"; bulkcopy.WriteToServer(dt); bulkcopy.Close(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { conn.Close(); Console.WriteLine(list.Count + " Records updated"); } } }Mehmet FatihRank #1,474 · 1.1K reputationPosted Jun 4, 2023, 6:27 AM EDT
I am using OleDbconnection. OleDbBulkCopy gives error..
Mehmet FatihRank #1,474 · 1.1K reputationPosted Jun 4, 2023, 8:43 AM EDT
I have solved the question using the codes by Satyapriya Nayak.
int i = 0; List ChkedRow = new List(); for (i = 0; i <= dataGridView1.RowCount - 1; i++) { if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["id"].Value) == true) { ChkedRow.Add(i); } } if (ChkedRow.Count == 0) { MessageBox.Show("En az bir checkbox seçiniz!"); return; } foreach (int j in ChkedRow) { try { if (conn.State == ConnectionState.Closed) { conn.Open(); } var val1 = dataGridView1.Rows[j].Cells["isim"].Value; var val2 = dataGridView1.Rows[j].Cells["soyisim"].Value; OleDbCommand cmd = new OleDbCommand("select * from gezilistemiz25 where adi = '" + val1 + "' and soyadi= '" + val2 + "' ", conn); OleDbDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { MessageBox.Show(" '" + val1 + "' '" + val2 + "' isimli kisi veritabaninda kayitlidir. Mükerrer kayit yapilamaz. Lütfen ögrenci numarasini kontrol ediniz."); } else { var cmdText = @"INSERT INTO gezilistemiz25 (adi, soyadi, cinsiyet, unvani) VALUES('" + dataGridView1.Rows[j].Cells["isim"].Value + "', '" + dataGridView1.Rows[j].Cells["soyisim"].Value + "', '" + dataGridView1.Rows[j].Cells["cinsiyet"].Value + "', '" + dataGridView1.Rows[j].Cells["unvan"].Value + "')"; var command = new OleDbCommand(cmdText, conn); command.ExecuteNonQuery(); conn.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } MessageBox.Show("Kayitlar Basariyla eklendi!");