I am trying to insert into two tables as one is main details of customer and another table is sub details of customer. Means 1st row will take parameters from controls and 2nd table will take from datagridview. Those two has to done in one method. I tried as
public void CstmrInsUp(string cstName, string cstSName, string AdLn1, string AdLn2, string AdCity, string AdPin, string SAdLn1, string SAdLn2, string SAdCity, string SAdPin, string TelPh1, string TelPh2, string FaxNo, string MailId, string TinNo,string cstDtlName,string cstDsgntn,string cstDtMobl,string cstDtEmail)
{
try
{
int cstId=1;
int? grntdId=0;
cstmrDC.Connection.Open();
trns = cstmrDC.Connection.BeginTransaction();
cstmrDC.Transaction = trns;
cstmrDC.customers_iu(cstId, cstName, cstSName, AdLn1, AdLn2, null, AdCity, AdPin, SAdLn1, SAdLn2, null, SAdCity, SAdPin, TelPh1, TelPh2, FaxNo, MailId, null, TinNo, 1,ref grntdId);
cstmrDC.customerscntcts_iu(null, cstId, cstDtlName, cstDsgntn, cstDtMobl, cstDtEmail, 1);
trns.Commit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
if (trns != null)
trns.Rollback();
}
}
Here customersdtls table should fire as in datagridview rows. Iam passing parameters as
for (int i = 0; i < dgvCustInfo.Rows.Count - 1; i++)
{
cstnam = dgvCustInfo.Rows[i].Cells[0].Value.ToString();
dsgntn = dgvCustInfo.Rows[i].Cells[1].Value.ToString();
mblNo = dgvCustInfo.Rows[i].Cells[3].Value.ToString();
eMail = dgvCustInfo.Rows[i].Cells[4].Value.ToString();
//cst.InsrtDgv(cstnam, dsgntn, extNo, mblNo, eMail);
//write a log event with the user information , system information, and activity
}
cstCls.CstmrInsUp(txtCustmr.Text, txtShrtNam.Text, txtLn1.Text, txtLn2.Text, txtCity.Text,pin.ToString(),txtSpLn1.Text,txtSpLn2.Text,txtSpCty.Text,pin.ToString(), Phn1.ToString(), Phn2.ToString(), fax.ToString(),txtEmail.Text,txtTinNo.Text,cstnam,dsgntn,mblNo,eMail);
By doing this. First one record is only saving. How can i implement to insert all rows from datagridview and controls. Thank you
In your Method
CstmrInsUpvariablecstIdis always1. I think this is the primary key value, so it is inserting the first record with value1. Then after that it cannot insert another row with samecustid. So it is throwing an error and the transaction is rolled back.