Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to insert multiple records into a database table that are entered into a datagrid. With a single shot of Save all the records in the datagrid should be updated using C# . Can any one help me in this regard.

Please tell me the solution as i have wasted near about whole day on this. I did not find help in google search.

Any help appreciated
Posted

Try this :-
C#
foreach(DataGridViewRow row in dg.Rows)
{
  string mSQL= "Insert Into Table_Name (Field1,Field2....) Values                                                        ('"+row.Cells["Col1.Name"].Value+"','"+row.Cells["Col2.Name"].Value+"'...)";
            SqlConnection mConn = new SqlConnection(connString);
            SqlCommand mCmd = new SqlCommand(mSQL, mConn);
            int mReturnVal;
            Exception ex = new Exception();

            try
            {
                mConn.Open();
                mReturnVal = mCmd.ExecuteNonQuery();
            }

            catch
            {
                throw new Exception(ex.Message, ex);
            }

            finally
            {
                mConn.Close();
            }
 
}


Hope this will help you.
 
Share this answer
 
SQL
INSERT INTO `tablename`(column1,column2,column3) VALUES
('aaa','aaa','aaa'),
('bbb','bbb','bbb'),
('ccc','ccc','ccc');
 
Share this answer
 

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