Generating one number with multiple rows in datagridview

  • Thread starter Thread starter MAHDI SEDAQAT
  • Start date Start date
M

MAHDI SEDAQAT

Guest
I am trying to create a billing form. I have two tables in sql-server. The text boxes should insert the date to table 1 and datagridview to table 2.

I am not getting any errors but I have a text box which generates auto number, and under each number there should be multiple rows in datagridview.

The code below is saving only the last row in the table.here is the code


con = new SqlConnection("data source=localhost;initial catalog=testDb;integrated security=true");
con.Open();
cmd = new SqlCommand(@"select CashirReport.ReportId,CashirReport.ReportNumber,CashirReport.ReportDate ,
CashirReportDetails.SerialNumber,
CashirReportDetails.RefDetails,CashirReportDetails.Amount from CashirReport
inner join CashirReportDetails on CashirReport.ReportId=CashirReportDetails.ReportDetailsNumber", con);
da = new SqlDataAdapter();
da.SelectCommand = cmd;
dt = new DataTable();
da.Fill(dt);


}

private void btnsave_Click(object sender, EventArgs e)
{
for (int i = 0; i < dgvDetails.Rows.Count - 1; i++)
{
cmd = new SqlCommand(@"insert into CashirReport(ReportDate,Total)values(@ReportDate,@Total);
insert into CashirReportDetails(SerialNumber,RefDetails,Amount)values(@SerialNumber,@RefDetails,@Amount)",con);

cmd.Parameters.AddWithValue("@ReportDate", SqlDbType.Date).Value = dtDate.Value.Date;
cmd.Parameters.AddWithValue("@Total", SqlDbType.Money).Value = txtAmount.Text;


cmd.Parameters.AddWithValue("@SerialNumber", dgvDetails.Rows.Cells[0].Value);
cmd.Parameters.AddWithValue("@RefDetails", dgvDetails.Rows.Cells[1].Value);
cmd.Parameters.AddWithValue("@Amount", dgvDetails.Rows.Cells[2].Value);
}
cmd.ExecuteNonQuery();

Continue reading...
 
Back
Top