public string DoSave(Ledgers.ARAPRECEIPTSRow drmain,Ledgers.ARAPRECEIPTSDETAILSDataTable dt)
        {
            SqlTransaction tran = null;
            var isnew = false;
            string retval = string.Empty;
            try
            {
                var cn = new SqlConnection (_constr);
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);

                var taPurPaid = new ARAPRECEIPTSTableAdapter {Connection = {ConnectionString = _constr}};

                var dtmain = new Ledgers.ARAPRECEIPTSDataTable();
                var ds = taPurPaid.Fill(dtmain);

                var taPurPaidDet = new ARAPRECEIPTSDETAILSTableAdapter();

                var taApAr = new LedgersTableAdapters.APARMAINTableAdapter();

                var taLedger = new LedgersTableAdapters.LEDGERSTableAdapter();
                taLedger.AssignConnection(cn, tran);

                taPurPaid.AssignConnection(cn,tran);
                var dm = new DataManager();
                taPurPaidDet.AssignConnection(cn,tran);
                  

                var dr = dtmain.FindByARAPTRANID(drmain.ARAPTRANID);
                if (dr != null)
                {
                    dr.BeginEdit();
                }
                else
                {
                    dr = (Ledgers.ARAPRECEIPTSRow) dtmain.NewRow();
                    Utils.PurPayment = dm.GetAutoId(19).ToString("D6");
                    drmain.ARAPTRANID =Utils.PurPayment;
                    dr.ARAPTRANID = Utils.PurPayment;
                    isnew = true;
                }
                dr.AMOUNT = drmain.AMOUNT;
                dr.TDATE = drmain.TDATE;
                dr.BANKID = drmain.BANKID;
                dr.POSTED = false;
                string cmdtxt = string.Empty;
                foreach (Ledgers.ARAPRECEIPTSDETAILSRow drdet in dt.Rows)
                {
                    if (drdet.RowState != DataRowState.Deleted)
                    {
                        drdet.ARAPTRANID = dr.ARAPTRANID;
                        cmdtxt = "update APARMAIN set PAIDAMOUNT = " + drdet.AppliedAmt +
                                     ", BALANCE = 0 where  TRANID = " + drdet.TRANID;
                            var cmd = new SqlCommand(cmdtxt, cn, tran);
                            cmd.ExecuteNonQuery();
                           taLedger.Insert("S", drdet.REFNO, DateTime.Now.Date, drdet.INVOICEDATE, dr.ARAPTRANID,drdet.INVOICENO, drdet.INVAMOUNT,0, 18, Utils.Company, Utils.Userid, "Purchase Payment");
                    }
                }
                if (isnew)
                {
                    dtmain.Rows.Add(dr);
                    taPurPaid.Update(dtmain);
                }
                else
                {
                    dr.EndEdit();
                    taPurPaid.Update(dr); 
                }
                taPurPaidDet.Update(dt);
                tran.Commit();
                dm.UpdateAutoNum(19);

                retval = "1";
            }
            catch (Exception ex)
            {
                tran.Rollback();
                retval = ex.ToString();
            }
            return retval;
        }
        public string DoPost(string batchno,Ledgers.ARAPRECEIPTSDETAILSDataTable dt)
        {
            string retval = string.Empty;
            SqlTransaction tran = null;
            try
            {
                var cn = new SqlConnection(_constr);
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);

                var ta = new ARAPRECEIPTSTableAdapter();
                var tadet = new ARAPRECEIPTSDETAILSTableAdapter();

                var taLedger = new LEDGERSTableAdapter();
                 ta.AssignConnection(cn,tran);// tadet.AssignConnection(cn,tran);
               
                taLedger.AssignConnection(cn,tran);
                string arapno = string.Empty;
                foreach (Ledgers.ARAPRECEIPTSDETAILSRow drdet in dt.Rows)
                {
                            if (drdet.RowState != DataRowState.Deleted)
                            {
                                arapno = drdet.ARAPTRANID;
                                var cmdtxt = "update APARMAIN set PAIDAMOUNT = 0 , BALANCE =  INVAMOUNT where  TRANID = " + drdet.TRANID;
                                var cmd = new SqlCommand(cmdtxt, cn, tran);
                                cmd.ExecuteNonQuery();
                                taLedger.Insert("S", drdet.REFNO, DateTime.Now.Date, drdet.INVOICEDATE, batchno,
                                drdet.INVOICENO, - drdet.INVAMOUNT, 0, 18, Utils.Company, Utils.Userid,"Payment Reverse");
                            }
                 }
                tran.Commit();
                //tadet.DeleteByARAPTRANID(batchno);
                //ta.DeleteByARAPTRANID(batchno);
                 ta.DoPost(batchno);
                
                retval = "1";
               
                
            }
            catch (Exception ex)
            {
                if (tran != null) tran.Rollback();
                retval = ex.ToString();
            }
            return retval;
        }