C# Update Database for every DataGridView Row

Mac

I want to update a Table in Database for every row in the DataGridView by using a Update button. Here's The following Update Button Code :

private void btUpdate_Click(object sender, EventArgs e)
{
    SqlConnection cn = new SqlConnection(con);
    cn.Open();
    SqlCommand cmdUpdate = new SqlCommand("UPDATE OINV SET U_IDU_HFP = @HFP, U_IDU_FAKTURPAJAK = @FP, U_IDU_FPDATE = @FPDATE WHERE DocEntry = @DocEntry;", cn);
    foreach (DataGridViewRow item in dgvInputPKP.Rows)
    {
        cmdUpdate.Parameters.AddWithValue("@HFP", item.Cells[10].Value);
        cmdUpdate.Parameters.AddWithValue("@FP", item.Cells[11].Value);
        cmdUpdate.Parameters.AddWithValue("@FPDATE", item.Cells[9].Value);
        cmdUpdate.Parameters.AddWithValue("@DocEntry", item.Cells[1].Value);
        cmdUpdate.ExecuteNonQuery();
    }
    MessageBox.Show("Record Updated Successfully");
    cn.Close();
}

When I press the Update Button only the 1st row from DataGridViews was Updated Succesfully in the Database, but not with the other rows. It also return some error with following detail:

The variable name '@HFP' has already been declared. Variable names must be unique within a query batch or stored procedure.

I was try to searching and googling but never find the right one.

Please help me to fix this issue. Thanks.

Steve

You need to clear the Command.Parameters collection at each loop

foreach (DataGridViewRow item in dgvInputPKP.Rows)
{
    cmdUpdate.Parameters.Clear();
    ....
    cmdUpdate.ExecuteNonQuery();
}

However there are a couple of things that could be done here to optimize and make your code more safe and remove weakness.

using(SqlConnection cn = new SqlConnection(con))
using(SqlCommand cmdUpdate = new SqlCommand("UPDATE OINV SET U_IDU_HFP = @HFP, U_IDU_FAKTURPAJAK = @FP, U_IDU_FPDATE = @FPDATE WHERE DocEntry = @DocEntry;", cn))
{
    cn.Open();
    using(SqlTransaction tr = cn.BeginTrasaction())
    {
        cmdUpdate.Transaction = tr;
        cmdUpdate.Parameters.Add("@HFP", SqlDbType.NVarChar);
        cmdUpdate.Parameters.Add("@FP", SqlDbType.NVarChar);
        cmdUpdate.Parameters.Add("@FPDATE", SqlDbType.NVarChar);
        cmdUpdate.Parameters.Add("@DocEntry", SqlDbType.NVarChar);
        foreach (DataGridViewRow item in dgvInputPKP.Rows)
        {
            cmdUpdate.Parameters["@HFP"].Value = item.Cells[10].Value;
            cmdUpdate.Parameters["@FP"].Value = item.Cells[11].Value;
            cmdUpdate.Parameters["@FPDATE"].Value = item.Cells[9].Value;
            cmdUpdate.Parameters["@DocEntry"].Value = item.Cells[1].Value;
            cmdUpdate.ExecuteNonQuery();
       }
       tr.Commit();
   }
}
MessageBox.Show("Record Updated Successfully");

The parameter collection could be declared before entering the loop while inside the loop you update only the value. Notice also that the command and the connection are disposable object and should be declared and initialized inside a using block. Also to make your code more robust, I have added a transaction that ensures that all of your records are written to the database or none of them will be written in case of failure. The SqlTransaction.Commit at the end of the loop ensures this.

Finally, I don't know the datatype of the fields that receive the parameters value. You need to change the SqlDbType in the creation of the parameters according to the datatype of your columns in the datatable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# perform action for every row in a dataGridView

From Dev

How to update Access database using a datagridview in C#

From Dev

Update newly added rows of datagridview to oledb database using c#

From Dev

datagridview update row to csv file

From Dev

Why wont DataGridView Update Database?

From Dev

C# MySQL - "Update" is selecting every row/everything

From Dev

C# - SQL - Reading every row in a database, datamining the row, then saving results in another database - How to increase speed

From Dev

C# DataGridView show Tooltip text over every row on mouse hover

From Dev

How to update every nth row

From Dev

How to update every nth row

From Dev

MySQL loop for every row and update

From Dev

Database every row in different div

From Dev

DataGridView row text disappears on every row except the selected one

From Dev

DataGridView row text disappears on every row except the selected one

From Dev

Sending updates from datagridview to existing row in database

From Dev

Sending updates from datagridview to existing row in database

From Dev

database connection to datagridview's row only

From Dev

How to delete selected row from datagridview and database?

From Dev

C# datagridview row toggle

From Dev

C# DataGridView Row Coloring

From Dev

Update mongodb database when datagridview is changed

From Dev

How to update SQL Server database using Datagridview binding source C#

From Dev

C# Using Two DataTables in DataGridView without Losing Ability to Update Database

From Dev

Remove/Update row in database with jquery

From Dev

Unable Update row in mysql database

From Dev

Row won't update in database

From Dev

Can't update row in database

From Dev

MySQL: Update every second row string

From Dev

Run UPDATE query for every row in another SELECT

Related Related

  1. 1

    C# perform action for every row in a dataGridView

  2. 2

    How to update Access database using a datagridview in C#

  3. 3

    Update newly added rows of datagridview to oledb database using c#

  4. 4

    datagridview update row to csv file

  5. 5

    Why wont DataGridView Update Database?

  6. 6

    C# MySQL - "Update" is selecting every row/everything

  7. 7

    C# - SQL - Reading every row in a database, datamining the row, then saving results in another database - How to increase speed

  8. 8

    C# DataGridView show Tooltip text over every row on mouse hover

  9. 9

    How to update every nth row

  10. 10

    How to update every nth row

  11. 11

    MySQL loop for every row and update

  12. 12

    Database every row in different div

  13. 13

    DataGridView row text disappears on every row except the selected one

  14. 14

    DataGridView row text disappears on every row except the selected one

  15. 15

    Sending updates from datagridview to existing row in database

  16. 16

    Sending updates from datagridview to existing row in database

  17. 17

    database connection to datagridview's row only

  18. 18

    How to delete selected row from datagridview and database?

  19. 19

    C# datagridview row toggle

  20. 20

    C# DataGridView Row Coloring

  21. 21

    Update mongodb database when datagridview is changed

  22. 22

    How to update SQL Server database using Datagridview binding source C#

  23. 23

    C# Using Two DataTables in DataGridView without Losing Ability to Update Database

  24. 24

    Remove/Update row in database with jquery

  25. 25

    Unable Update row in mysql database

  26. 26

    Row won't update in database

  27. 27

    Can't update row in database

  28. 28

    MySQL: Update every second row string

  29. 29

    Run UPDATE query for every row in another SELECT

HotTag

Archive