protected void btnSave_Click(object sender, EventArgs e) { dtgridList = (DataTable)ViewState["dtgridList"]; string users = Session["id"].ToString(); decimal totalamt = 0; try { foreach (DataRow dr in dtgridList.Rows) { totalamt = totalamt + decimal.Parse(dr["Amount"].ToString()); } FrameWork.PurchaseOrders PRO = new FrameWork.PurchaseOrders(); FrameWork.PurchaseOrderDetails PROsub = new PurchaseOrderDetails(); busPurchaseOrder busPRO = new busPurchaseOrder(); PRO.SupplierID = int.Parse(this.drpSupplier.SelectedValue); PRO.CreatedById = int.Parse(users); if (this.txtDateNeeded.Text.Trim() != string.Empty) { PRO.ExpectedDate = DateTime.Parse(this.txtDateNeeded.Text); } else { PRO.ExpectedDate = DateTime.Parse("01/01/1900"); } PRO.PaymentAmount = totalamt; string x = busPRO.insertPO(PRO); foreach (DataRow dr in dtgridList.Rows) { PROsub.PurchaseOrderID = int.Parse(x); PROsub.Quantity = Single.Parse(dr["Quantity"].ToString()); PROsub.UnitCost = decimal.Parse(dr["UnitPrice"].ToString()); PROsub.ProductID = int.Parse(dr["ProductID"].ToString()); busPRO.insertPurchaseOrderDetails(PROsub); } } catch (Exception ex) { } finally { this.txtQuantity.Text = string.Empty; this.txtUOM.Text = string.Empty; this.drpProduct.SelectedValue = "0"; this.lblDescription.Text = string.Empty; this.txtUnitPrice.Text = string.Empty; this.drpSupplier.SelectedValue = "0"; this.txtTerms.Text = string.Empty; DataTable ds = new DataTable(); ds = null; this.grdList.DataSource = ds; grdList.DataBind(); } }
public FrameWork.PurchaseOrderDetails[] allPurchaseOrderDetails(string ID) { string query = "SELECT a.[ID] ,[Quantity],[UnitCost],[ProductID],[ProductName] FROM [dbo].[PurchaseOrderDetails] a inner join [dbo].Products b on a.ProductID = b.ID where PurchaseOrderID='" + ID + "' order by a.[ID] asc"; DataTable table = new DataTable(); table = DataAccess.DBAdapter.GetRecordSet(query); FrameWork.PurchaseOrderDetails[] dec = new FrameWork.PurchaseOrderDetails[table.Rows.Count]; for (int i = 0; i < table.Rows.Count; i++) { dec[i] = new FrameWork.PurchaseOrderDetails(table.Rows[i]); } return dec; }
public string insertPurchaseOrderDetails(PurchaseOrderDetails drsi) { string Message = string.Empty; int result = 0; con.Open(); SqlTransaction transaction; SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].PurchaseOrderDetails ([Quantity],[UnitCost],[PurchaseOrderID],[ProductID],[IsSubmitted],[PostedToInventory]) VALUES (@Quantity,@UnitCost,@PurchaseOrderID,@ProductID,@IsSubmitted,@PostedToInventory)", con); cmd.Parameters.AddWithValue("@Quantity", drsi.Quantity); cmd.Parameters.AddWithValue("@UnitCost", drsi.UnitCost); cmd.Parameters.AddWithValue("@PurchaseOrderID", drsi.PurchaseOrderID); cmd.Parameters.AddWithValue("@ProductID", drsi.ProductID); cmd.Parameters.AddWithValue("@IsSubmitted", 1); cmd.Parameters.AddWithValue("@PostedToInventory", 1); transaction = con.BeginTransaction(); cmd.Transaction = transaction; try { result = cmd.ExecuteNonQuery(); transaction.Commit(); } catch (Exception ex) { try { transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail, such as // a closed connection. Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } } con.Close(); //string query = "select top 1 ID from [DRSIForm] order by id desc"; //DataTable dt = new DataTable("Customer"); //dt.Clear(); //dt = DataAccess.DBAdapter.GetRecordSet(query); //if (dt.Rows.Count > 0) //{ // Message = dt.Rows[0]["ID"].ToString(); //} return Message; }