Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My datagriview control having multiple rows like SNO, ITEM, DESCRIPTION, QUANTITY, when I am trying to insert datagridview's multiple rows in sql server it is taking top one row values and rest of the rows not inserting.following is my code block.
kindly suggest...thanks in advance

SqlConnection cn = new SqlConnection(constring);
           foreach (DataGridViewRow Row in dgvholidays.Rows)   //dgv holiday
           {
               if (Row.Cells[0].Value != null)
               {
                   int myear = Convert.ToInt32(Row.Cells[0].Value.ToString());
                   string holname =Convert.ToString(Row.Cells[1].Value);
                   DateTime dt = Convert.ToDateTime(Row.Cells[2].Value);   // Convert.ToInt32(Row.Cells[0].Value.ToString())
                   DateTime dt1 = Convert.ToDateTime(Row.Cells[3].Value);
                   int days = Convert.ToInt32(Row.Cells[4].Value.ToString());
                   dgvholidays.Columns[2].DefaultCellStyle.NullValue = string.Empty;
                   dgvholidays.Columns[3].DefaultCellStyle.NullValue = string.Empty;
                   SqlCommand cmd = new SqlCommand("Update Holiday set mYear='" + myear + "',holidayName='" + holname + "',dtFrom='" + dt.ToLongTimeString() + "',dtTo='" + dt1.ToLongTimeString() + "',tDays='" + days + "',Compcode='" + ccode + "' where IsActice=1", cn);
                   cmd.Connection.Open();
                   cmd.ExecuteNonQuery();
                   cmd.Connection.Close();
               }
           }
Posted
Comments
Richard Deeming 21-Apr-15 9:24am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

The shared code doesn't have the logic for insertion, instead it is meant for updation.
Note: I am using the answer section as you still have issue in the peiece of code you have shared here. Please have a look.

Hav a look at WHERE condition of your query. Are you are missing someting like-
SQL
WHERE IsActice=1 AND SomeId=@SomeId --IsActice or IsActive??

Your current query should update all the records with IsActice=1 and hence you should see the changes done only in the last updation.

Note: You should use parameterized query or stored procedure instead of inline queries to avoid SQL Injection.
 
Share this answer
 
v2
Comments
Meer Wajeed Ali 21-Apr-15 9:47am    
thanks Giri, could you check out my last one comment with code I,e stored procedure.just check and reply
Suvendu Shekhar Giri 21-Apr-15 9:54am    
Informaion is still missing. Instead of putting code in the comment, why not update the original question using Improve Solution widget.

Regarding solution, make sure that you have called this method using a loop for each of the row in the datagridview.

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