Lm Martiness

Lm Martiness

  • 1.8k
  • 77
  • 552

Store data from datagridview to sql tables

Apr 1 2020 9:07 AM
Hi everyone . I am trying to develop a small POS. The idea its that when the payment its done , the data should be stored into two  SQL tables. 
I am using this code to save data .
 

private void btnfaturo_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("server=localhost;Database =primus; integrated Security = true"))
{
for (int i = 0; i < dtgartikuj.Rows.Count; i++)
{
{
SqlCommand cmdeditimi = new SqlCommand("updatestoku", con);
cmdeditimi.CommandType = CommandType.StoredProcedure;
cmdeditimi.Parameters.Add(new SqlParameter("@barkodi", Convert.ToString(dtgartikuj.Rows[i].Cells[0].Value)));
cmdeditimi.Parameters.Add(new SqlParameter("@sasia", Convert.ToString(dtgartikuj.Rows[i].Cells[2].Value)));
con.Open();
cmdeditimi.ExecuteNonQuery();
con.Close();
}
{
SqlCommand cmd = new SqlCommand("insertfaturimi", con);
cmd.CommandType = CommandType.StoredProcedure;


cmd.Parameters.Add(new SqlParameter("@idfatura", Int64.Parse(txtnrfatures.Text)));
cmd.Parameters.Add(new SqlParameter("@klienti", cmbklienti.Text));
cmd.Parameters.Add(new SqlParameter("@shenime", txtshenime.Text));
cmd.Parameters.Add(new SqlParameter("@nrartikujve", Int64.Parse(lblnumri.Text)));
cmd.Parameters.Add(new SqlParameter("@vleratvsh", Double.Parse(textBox1.Text)));
cmd.Parameters.Add(new SqlParameter("@nentotali", Double.Parse(txttotali.Text)));
cmd.Parameters.Add(new SqlParameter("@zbritje", Double.Parse(txtzbritja.Text)));
cmd.Parameters.Add(new SqlParameter("@totali", Double.Parse(totali.Text)));
cmd.Parameters.Add(new SqlParameter("@kasieri", lbluser.Text));
cmd.Parameters.Add(new SqlParameter("@koha", DateTime.Now));
cmd.Parameters.Add(new SqlParameter("@pagesa", faturimi));
cmd.Parameters.Add(new SqlParameter("@barkodi", Convert.ToString(dtgartikuj.Rows[i].Cells[0].Value)));
cmd.Parameters.Add(new SqlParameter("@emertimi", Convert.ToString(dtgartikuj.Rows[i].Cells[1].Value)));
cmd.Parameters.Add(new SqlParameter("@sasia", Convert.ToString(dtgartikuj.Rows[i].Cells[2].Value)));
cmd.Parameters.Add(new SqlParameter("@tvsh", Convert.ToString(dtgartikuj.Rows[i].Cells[3].Value)));
cmd.Parameters.Add(new SqlParameter("@cmimi", Convert.ToString(dtgartikuj.Rows[i].Cells[4].Value)));
cmd.Parameters.Add(new SqlParameter("@totalipcs", Convert.ToString(dtgartikuj.Rows[i].Cells[5].Value)));
cmd.Parameters.Add(new SqlParameter("@vlerapatvshpcs", Convert.ToString(dtgartikuj.Rows[i].Cells[6].Value)));
cmd.Parameters.Add(new SqlParameter("@vleraetvshpcs", Convert.ToString(dtgartikuj.Rows[i].Cells[7].Value)));
con.Open();
cmd.ExecuteNonQuery();
con.Close();

}
}
}
clear();
}
 
The data qty its updated as i want, also data are being stored in tables, but they are being stored twice or more. To be more clear the same data(2-3 or 4 rows of datagridview) are being stored 2 or 3 times 
 
Could someone help me about the error, where is the fault. Thanks to everyone!!! 

Brought to you by:

Answers (3)