Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i use the code below to save data into sql database. each row is given a unique number (IDENTITY) it works alright but always saves the last row on the gridview which does not contain any data. HOW do i prevent this

code
C#
for (int i = 0; i < dgvCategory.Rows.Count; i++)
                        {
                            StrQuery = @"INSERT INTO CategorynA VALUES ('" + this.txtCategory.Text + "', '" + dgvCategory.Rows[i].Cells["clmActivity"].Value + "');";
                            comm.CommandText = StrQuery;
                            conn.Open();
                            comm.ExecuteNonQuery();
                            conn.Close();
                        }
Posted

for (int i = 0; i < dgvCategory.Rows.Count -1 ; i++)

don't forget the " - 1 "
 
Share this answer
 
Put a check in to ensure that "dgvCategory.Rows[i].Cells["clmActivity"].Value" is not empty.
I would also suggest using Parameters instead of the approach you are using as it is susceptible to SQL injection attacks.
 
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