Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i hv used the following code..it is not showing any error but its nt working..i mean new values r not being inserted

private void PopulateComboBox()
 {
 try
 {

 List<string> _items = new List<string>();

 _items.Add("select * from lol");
 _items.Add("select * from datejoin");
 comboBox1.DataSource = _items;

 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
 }

 private void Form1_Load(object sender, EventArgs e)
 {
 PopulateComboBox();
 }

 private void PopulateGridView(string connString, string sqlQuery)
 {

 String strconnection = connString;

 SqlConnection con = new SqlConnection(strconnection);



 try
 {

 con.Open();

 SqlCommand sqlCmd = new SqlCommand();

 sqlCmd.Connection = con;
 sqlCmd.CommandType = CommandType.Text;
 sqlCmd.CommandText = sqlQuery;

 SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

 DataTable dtRecord = new DataTable();
 sqlDataAdap.Fill(dtRecord);

 dataGridView1.DataSource = dtRecord;
 dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
 con.Close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
 }


 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
 if (comboBox1.SelectedValue != null)
 {
 PopulateGridView(textBox1.Text,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.SelectedText + " 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 !! ");
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.ToString());
 }


 }
 }
 private void button1_Click(object sender, EventArgs e)
 { 
 InsertInfo();

 }

 private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
 {
 lstNewRows.Add(e.Row.Index);
 }
 }
 }
Posted
Comments
vishal.shimpi 14-Mar-13 4:39am    
check what the string is in 'insrtQry' variable
mayuri koul 14-Mar-13 5:25am    
there's some problem here foreach (DataGridViewCell cell in dataGridView1.Rows[rowIndex].Cells)

vishal.shimpi 14-Mar-13 5:53am    
Hi mayuri,
use follwing code to get cell value

int cols = yourGridView.Rows[0].Cells.Count;
foreach (GridViewRow gr in yourGridView.Rows)
{
for (int i = 0; i < cols; i++)
{
//create your string here

}
}

1 solution

update your Inser Function like..
 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.SelectedText + " values(";
 
int cols = yourGridView.Rows[0].Cells.Count;
 foreach (GridViewRow gr in yourGridView.Rows)
 {
     for (int i = 0; i < cols; i++)
     {
         insrtQry += "'" + gr.Cell[i].Text.ToString() + "',";
     }
 }

 
 insrtQry = insrtQry.TrimEnd(",".ToCharArray());
 
 insrtQry += ")";
 

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

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

 }
 }

if it is helpful to you then rate the solution
 
Share this answer
 
Comments
mayuri koul 14-Mar-13 6:41am    
its not working it is showing an error in word cell (in for each loop) and after debugging it is showing an error box in which it is written that incorrect syntax near keyword values
vishal.shimpi 14-Mar-13 7:59am    
sorry, use gr.Cells[i].Text
vishal.shimpi 14-Mar-13 8:01am    
and one more thing... replace 'yourGridView' with 'dataGridView1'

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