Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 31-Mar-13 20:55pm
v2
Comments
[no name] 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 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 1-Apr-13 11:47am    
And what do you see when you DEBUG the code?
mayuri koul 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

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?
 
Share this answer
 
Comments
mayuri koul 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900