public string DoSavePurchaseInvoiceInRemote(DsPurchase.PURINVMAINRow mainrow, DsPurchase.PURINVDETAILDataTable purinvdetail)
        {
            var dm = new DataManager();
            string retval="0";
            SqlTransaction tran = null;
          //var possql = new SqlAsyncConnectionString();
            var cn = new SqlConnection(_constr);
            try
            {
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);
                var ds = new DsPurchase();
                var dt = ds.PURINVMAIN;
                var dtdetail = ds.PURINVDETAIL;

                

                var taPurMain = new PURINVMAINTableAdapter();

                var taPurDetail = new PURINVDETAILTableAdapter();

                var taapar = new LedgersTableAdapters.APARMAINTableAdapter();
                var taledger = new LedgersTableAdapters.LEDGERSTableAdapter();

                taPurMain.Connection.ConnectionString = _constr;
                taPurMain.FillByPurid(dt, mainrow.PURINVID);

                   // Assaign Transaction
                taPurMain.AssignConnection(cn, tran);
                taPurDetail.AssignConnection(cn, tran);
                
                taapar.AssignConnection(cn, tran);
                taledger.AssignConnection(cn, tran);
                DsPurchase.PURINVMAINRow dr = null;
               
                if (dt.Rows.Count == 0)
                {
                    dr = (DsPurchase.PURINVMAINRow) dt.NewRow();
                    dr.PURINVID = mainrow.PURINVID;
                    dr.USERID = mainrow.USERID;
                    dr.COMPANY = mainrow.COMPANY;
                    dr.UDATE = mainrow.UDATE;
                    if (!mainrow.IsEDATENull())
                        dr.EDATE = mainrow.EDATE;
                    if (!mainrow.IsEUSERIDNull())
                        dr.EUSERID = mainrow.EUSERID;
                    dr.SUPID = mainrow.SUPID;
                    dr.SUPNAME = mainrow.SUPNAME;
                    dr.INVDATE = mainrow.INVDATE;
                    dr.INVNO = mainrow.INVNO;
                    dr.DUEDAYS = mainrow.DUEDAYS;
                    dr.SPDISCOUNT = mainrow.SPDISCOUNT;
                    dr.WHID = mainrow.WHID;
                    dr.ADJVALUE = mainrow.ADJVALUE;
                    dr.NETVALUE = mainrow.NETVALUE;
                    dr.HOLD = mainrow.HOLD;
                    dt.Rows.Add(dr);
                    taPurMain.Update(dt);
                    //LEDGER
                    taapar.Insert("S", dr.SUPID, mainrow.UDATE, dr.INVDATE, mainrow.PURINVID, dr.INVNO, dr.NETVALUE, 0, dr.NETVALUE, Utils.Company, 1, Utils.Userid, 0);
                    taledger.Insert("S", dr.SUPID, DateTime.Now.Date, dr.INVDATE, mainrow.PURINVID, dr.INVNO, 0,dr.NETVALUE, 1, Utils.Company, Utils.Userid, "Purchase Invoice");
                    foreach (DsPurchase.PURINVDETAILRow ditemdetarow in purinvdetail)
                    {
                        var newdetrow = dtdetail.NewRow();
                        foreach (DataColumn dc in purinvdetail.Columns)
                        {
                            if (!dc.ReadOnly)
                            {
                                newdetrow[dc.ColumnName] = ditemdetarow[dc.ColumnName];
                            }
                        }
                        dtdetail.Rows.Add(newdetrow);
                    }
                    taPurDetail.Update(dtdetail);
                }
                else
                {
               
                    dr = dt.FindByPURINVID(mainrow.PURINVID);
                    var cmd = "UPDATE [PURINVMAIN] SET  SUPID = '" + dr.SUPID + "' ,SUPNAME = '" + dr.SUPNAME +
                              "', INVNO = '" + dr.INVNO + "' ,INVDATE  = '" + dr.INVDATE.ToString("yyyy-MM-dd HH:mm:ss") + "', DUEDAYS] = " + dr.DUEDAYS + " WHERE  PURINVID ='" + dr.PURINVID + "'";
                var sqlcmd2 = new SqlCommand();
                    sqlcmd2.Connection = cn;
                    sqlcmd2.Transaction = tran;
                    sqlcmd2.CommandText = cmd;


                    cmd = "update APARMAIN set REFNO = '" + dr.SUPID + "',INVOICEDATE ='" + dr.INVDATE.ToString("yyyy-MM-dd HH:mm:ss") + "',invoiceno= '" + dr.INVNO + "'  where SOURCEID=1 and  TYPE='S' AND VOUCHERNO='" + dr.PURINVID + "'";
                    var sqlcmd = new SqlCommand();
                    sqlcmd.Connection = cn;
                    sqlcmd.Transaction = tran;
                    sqlcmd.CommandText = cmd;
                    
                    sqlcmd.ExecuteNonQuery();
                    cmd = "update LEDGERS set REFNO = '" + dr.SUPID + "',INVOICEDATE = '" + dr.INVDATE.ToString("yyyy-MM-dd HH:mm:ss") + "' ,invoiceno= '" + dr.INVNO + "'   where  SORUCE =1 and LEDGERTYPE='S' AND VOUCHERNO= '" + dr.PURINVID + "'";
                    var sqlcmd1 = new SqlCommand();
                    sqlcmd1.Connection = cn;
                    sqlcmd1.Transaction = tran;
                    sqlcmd1.CommandText = cmd;
                    sqlcmd1.ExecuteNonQuery();
                }
                tran.Commit();
                retval = "1";
            }
            catch (Exception ex)
            {
                if (cn != null)
                {
                    if (tran != null)
                        tran.Rollback();
                }
                retval = ex.ToString();
            }
            return retval; 
        }
        public string DoSave(DsPurchase.PURINVMAINRow drow, DsPurchase.PURINVDETAILDataTable purinvdetail,bool issale=false, string tostore = "")
        {
            var dm = new DataManager();
            string retval;
            SqlTransaction tran = null;
            var isnew = false;
            try
            {
                var cn = new SqlConnection(_constr);
                cn.Open();
                tran = cn.BeginTransaction(IsolationLevel.ReadUncommitted);
                var ds = new DsPurchase();
                var dt = ds.PURINVMAIN;

                var taPurMain = new PURINVMAINTableAdapter();
                var taPurDetail = new PURINVDETAILTableAdapter();
                var taStockMaster = new STOCKMASTERTableAdapter();

                var taPriceMaster = new DsMasterTableAdapters.PRICEMASTERTableAdapter();

                var taItemMaster = new DsMasterTableAdapters.ITEMMASTTableAdapter();

                var taapar = new LedgersTableAdapters.APARMAINTableAdapter();
                var taledger = new LedgersTableAdapters.LEDGERSTableAdapter();

                taPurMain.Connection.ConnectionString = _constr;

                taPurMain.FillByPurid(ds.PURINVMAIN, drow.PURINVID);

            //  taPurMain.Fill(ds.PURINVMAIN);
           //   Assaign Transaction

                taPurMain.AssignConnection(cn, tran);
                taPurDetail.AssignConnection(cn, tran);
                taStockMaster.AssignConnection(cn, tran);
                taPriceMaster.AssignConnection(cn, tran);

                taItemMaster.AssignConnection(cn, tran);
                taapar.AssignConnection(cn, tran);
                taledger.AssignConnection(cn, tran);

                var dr = dt.FindByPURINVID(drow.PURINVID);
                if (dr != null)
                { dr.BeginEdit(); }
                else
                { dr = (DsPurchase.PURINVMAINRow)dt.NewRow(); }
                if (string.IsNullOrEmpty(drow.PURINVID))
                {
                    var autoid = dm.GetAutoId(1);
                    autoid++;
                    Utils.Purinvid = dm.CreateFormatedAutoNo(autoid);
                    dr.PURINVID = Utils.Purinvid;
                    dr.USERID = Utils.Userid;
                    dr.COMPANY = Utils.Company;
                    dr.UDATE = DateTime.Now;
                    isnew = true;
                }
                else
                {
                    Utils.Purinvid = drow.PURINVID;
                    dr.EDATE = DateTime.Now;
                    dr.EUSERID = Utils.Userid;
                }
                dr.SUPID = drow.SUPID;
                dr.SUPNAME = drow.SUPNAME;
                dr.INVDATE = drow.INVDATE;
                dr.INVNO = drow.INVNO;
                dr.DUEDAYS = drow.DUEDAYS;
                dr.SPDISCOUNT = drow.SPDISCOUNT;
                dr.WHID = drow.WHID;
                dr.ADJVALUE = drow.ADJVALUE;
                dr.NETVALUE = drow.NETVALUE;
                dr.HOLD = drow.HOLD;
                if (isnew)
                {
                    dt.Rows.Add(dr);
                    taPurMain.Update(dt);
                }
                else
                {
                    dr.EndEdit();
                    taPurMain.Update(dr);
                }
                //bool issale = false;//string tostore = string.Empty;

                //STOCK
                if (!drow.HOLD)
                {
                    if (issale)
                    {
                        new StockMaster().CreateStock(ref cn, ref tran, purinvdetail, drow.WHID, "S", true, false);
                    }
                    else
                    {
                        new StockMaster().CreateStock(ref cn, ref tran, purinvdetail, drow.WHID, "P", true, false);
                    }
               //Price Master
                    taPriceMaster.Update(new PriceMaster().UpdatePriceMaster(purinvdetail, drow.SUPID));
               //UPDATE LASTPRICE,LASTEXPIRY,LASTCOSTPRICE 

                //LEDGER
                    taapar.Insert("S", dr.SUPID, DateTime.Now.Date, dr.INVDATE, Utils.Purinvid, dr.INVNO, dr.NETVALUE, 0,
                             dr.NETVALUE, Utils.Company, 1, Utils.Userid, 0);
                    taledger.Insert("S", dr.SUPID, DateTime.Now.Date, dr.INVDATE, Utils.Purinvid, dr.INVNO, 0,
                                dr.NETVALUE, 1, Utils.Company, Utils.Userid,"Purchase Invoice");
                }

                foreach (DsPurchase.PURINVDETAILRow purd in purinvdetail.Rows)
                {
                    if ((purd.RowState != DataRowState.Deleted))
                    {
                        purd.PURINVID = Utils.Purinvid;
                        if (!purd.IsISSUEFORSALENull())
                        {
                            issale = purd.ISSUEFORSALE;
                            tostore = purd.TOSTORE;
                        }
                        // Item Master Price
                        if (!drow.HOLD)
                        {
                            taItemMaster.UpdateCPSP(purd.SPRICE, purd.CPRICE, purd.ITEMNO);
                        }
                    }
                }
                taPurDetail.Update(purinvdetail);
                tran.Commit();
                dm.UpdateAutoNum(1);
                retval = "1";
            }
            catch (Exception ex)
            {
                if (tran != null)
                    tran.Rollback();
                if (isnew)
                    Utils.Purinvid = string.Empty;
                retval = ex.ToString();
            }
            return retval;
        }