Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
the following code is working fine to insert new values in one row of tables...but when i try to insert multiple values then in the output those values get saved twice in the table...plz help
private void Form1_Load(object sender, EventArgs e)
 {
 String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
 
 SqlConnection con = new SqlConnection(strConnection);
 try
 {
 
 con.Open();
 
 SqlCommand sqlCmd = new SqlCommand();
 
 sqlCmd.Connection = con;
 sqlCmd.CommandType = CommandType.Text;
 sqlCmd.CommandText = "Select table_name from information_schema.tables";
 
 SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
 
 DataTable dtRecord = new DataTable();
 sqlDataAdap.Fill(dtRecord);
 comboBox1.DataSource = dtRecord;
 comboBox1.DisplayMember = "TABLE_NAME";
 comboBox1.ValueMember = "TABLE_NAME";
 
 con.Close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
 }
 private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
 {
 var collection = this.dataGridView1.Rows;
 string output = "";
 foreach (DataGridViewRow row in collection)
 {
 foreach (DataGridViewCell cell in row.Cells)
 {
 if (cell.Value != null)
 {
 output += cell.Value.ToString() + " ";
 this.Text = output;
 }
 }
 }
 }
 private void PopulateGridView(string tablename)
 
 {
 
 if (tablename == "System.Data.DataRowView")
 return;
 String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
 
 SqlConnection con = new SqlConnection(strConnection);
 try
 {
 
 con.Open();
 
 SqlCommand sqlCmd = new SqlCommand();
 
 sqlCmd.Connection = con;
 sqlCmd.CommandType = CommandType.Text;
 sqlCmd.CommandText = "Select * from " + tablename;
 
 SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
 
 DataTable dtRecord = new DataTable();
 sqlDataAdap.Fill(dtRecord);
 dataGridView1.DataSource = dtRecord;
 
 con.Close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
 }
 
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
 
 if (comboBox1.SelectedValue != null)
 {
 PopulateGridView(comboBox1.SelectedValue.ToString());
 }
 }
 private void InsertInfo()
 {
 string connectionString = null;
 SqlConnection connection;
 SqlDataAdapter adapter = new SqlDataAdapter();
 
 connectionString = @"Data Source=HP\SQLEXPRESS;database=MK;Integrated Security=true";
 connection = new SqlConnection(connectionString);
 foreach (int rowIndex in lstNewRows)
 {
 

 string insrtQry = "insert into " + comboBox1.Text + " values(";
 
 foreach (DataGridViewCell cell in dataGridView1.Rows[rowIndex].Cells)
 {
 insrtQry += "'" + cell.Value.ToString() + "',";
 }
 
 insrtQry = insrtQry.TrimEnd(",".ToCharArray());
 
 insrtQry += ")";
 

 
 try
 {
 connection.Open();
 adapter.InsertCommand = new SqlCommand(insrtQry, connection);
 adapter.InsertCommand.ExecuteNonQuery();
 

 
 MessageBox.Show("Row inserted !! ");
 connection.Close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.ToString());
 }
 }
 }
 

 

 
 private void insert_Click(object sender, EventArgs e)
 {
 InsertInfo();
 }
 
 private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
 {
 lstNewRows.Add(e.Row.Index);
 }
 

 }
 }
Posted 31-Mar-13 21:41pm
Edited 31-Mar-13 21:55pm
v2
Comments
BindassFrind at 1-Apr-13 3:04am
   
Do you use DataGridView for insert values in table ?
you use in your question "i try to insert multiple values then in the output", What is output i cant understand output mining ?
mayuri koul at 1-Apr-13 3:20am
   
yes i use dgv to insert new values...and by output i meant after debugging the code and inserting multiple new values...the table in sql gets updated with the new values but repeated twice
Sandeep Mewara at 1-Apr-13 11:47am
   
And what do you see when you DEBUG the code?
mayuri koul at 2-Apr-13 1:43am
   
i see a dgv filled with contents of table i select frm combobox and when i insert new values in more than 1 new rows the data is not saved in my sql db....(the values i insert in first new row gets saved but values stored in other new row does not get saved)...please help

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Only looked quickly - but aren't you populating the grid with the rows from the table - then iterating that collection of rows and inserting them? So you're inserting all the records that are already there? So it's not inserting them twice, it's just inserting existing rows again?
  Permalink  
Comments
mayuri koul at 2-Apr-13 1:44am
   
nopes,it is showing the new values that i enter in new row twice

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100