using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { SqlDataAdapter da; String ConnectionString = @"Data Source=SHAKIR-PC\SQLEXPRESS;Initial Catalog=trydb;Integrated Security=True;Pooling=False"; String cnnStr; SqlCommand cmd; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'trydbDataSet1.payments' table. You can move, or remove it, as needed. this.paymentsTableAdapter.Fill(this.trydbDataSet1.payments); } private void button1_Click(object sender, EventArgs e) { int i = 0; List<int> ChkedRow = new List<int>(); for (i = 0; i <= dataGridView1.RowCount - 1; i++) { if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Column1"].Value) == true) { ChkedRow.Add(i); } } if (ChkedRow.Count == 0) { MessageBox.Show("Select one checkbox"); return; } foreach (int j in ChkedRow) { cnnStr = @"INSERT INTO deposits (date,custname,description,chqno,duedate,accname,amount) VALUES ('" + dataGridView1.Rows[j].Cells["dateDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["custnameDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["descriptionDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["chqnoDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["duedateDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["accnameDataGridViewTextBoxColumn"].Value + "','" + dataGridView1.Rows[j].Cells["amountDataGridViewTextBoxColumn"].Value + "');"; try { using (SqlConnection cs = new SqlConnection(ConnectionString)) { using (cmd = new SqlCommand(cnnStr,cs)) { cs.Open(); cmd.ExecuteNonQuery(); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } MessageBox.Show("Records Added Succesfully"); } } }
pre
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)