简体   繁体   中英

Double Records inserted by Datatable to sql server database

i have a DataTable and i'm inserting records to SQL server database. i already have my user-defined table type and stored procedure and it works fine. the records from my Datatable were all inserted to the database but the problem is, whenever i click the Save button, two the same records will be inserted to my database. for example, my datatable contains only one record

TRANSACTION ID | QTY | UNIT | ARTICLES | UNIT PRICE | AMOUNT
TR-001            2     CASE  PRODUCT-A    100.00     200.00

but when i click the SAVE button, two same records will be inserted to my sql server table like this:

TRANSACTION ID | QTY | UNIT | ARTICLES | UNIT PRICE | AMOUNT
TR-001            2     CASE  PRODUCT-A    100.00     200.00
TR-001            2     CASE  PRODUCT-A    100.00     200.00

i have tried different kind of for loop mechanism like for loop and for each loop but same result. what's the problem there? here is my code:

                 public DataTable GetDataTable()
                {
                 dt_SalesInv.Columns.Add("TRANSACTION ID");
                 dt_SalesInv.Columns.Add("QTY");
                 dt_SalesInv.Columns.Add("UNIT");
                 dt_SalesInv.Columns.Add("ARTICLES");
                 dt_SalesInv.Columns.Add("UNIT PRICE");
                 dt_SalesInv.Columns.Add("AMOUNT");

                 DataRow drLocal = null;
                foreach (DataGridViewRow dr in dgvSalesInv.Rows)
               {
                drLocal = dt_SalesInv.NewRow();
                drLocal["TRANSACTION ID"] = dr.Cells["TRANSACTION ID"].Value;
                drLocal["QTY"] = dr.Cells["QTY"].Value;
                drLocal["UNIT"] = dr.Cells["UNIT"].Value;
                drLocal["ARTICLES"] = dr.Cells["ARTICLES"].Value;
                drLocal["UNIT PRICE"] = dr.Cells["UNIT PRICE"].Value;
                drLocal["AMOUNT"] = dr.Cells["AMOUNT"].Value;
                dt_SalesInv.Rows.Add(drLocal);

               }

                return dt_SalesInv;
            }

          public void Save_SalesInvoice()
         {


            try
           {

            //calling stored proc
            SqlConnection connectionstring = connectionString.Getconnection();
            SqlCommand comm = new SqlCommand("sp_Save_SalesInvoice_2", connectionstring);
            comm.CommandType = CommandType.StoredProcedure;

                GetDataTable();
                comm.Parameters.AddWithValue("@data", dt_SalesInv);
                comm.ExecuteNonQuery();



            if (comm.ExecuteNonQuery() >= 1)
                {
                    MessageBox.Show("New Sales Invoice has been added to the Database!", "Sub- 
                Category", MessageBoxButtons.OK, MessageBoxIcon.Information);

                 }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Try Catch Error Message", MessageBoxButtons.OK, 
            MessageBoxIcon.Error);

        }
    }

please help. thank you

You should avoid calling ExecuteNonQuery() multiple times, because it would execute the instruction that amount of times, and that is not what you need in this case.

You could also consider adding primary keys or unique keys to your datatable, so wrong data is avoided in case bad things happen.

GetDataTable();
comm.Parameters.AddWithValue("@data", dt_SalesInv);
int rowsAffected = comm.ExecuteNonQuery();
if (rowsAffected >= 1)
{
    MessageBox.Show("New Sales Invoice has been added to the Database!", "Sub-Category", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM