public void CreateControlLedgers() { string controlName = MainWindow.listControl.Where(x => x.ControlName == txtNameMainCategory.Text).Select(x => x.ControlName).FirstOrDefault(); if (controlName != null) { Message.ShowMessage("It exists!"); } else { string mainCatName = txtNameMainCategory.Text; string mainCatDes = txtDescribeMainCategory.Text; if (mainCatName == string.Empty) { Message.ShowMessage("Check field"); } else if (mainCatName.Length > 50) { Message.ShowMessage("More than 50 Character is not supported"); } else if (mainCatDes.Length > 75) { Message.ShowMessage("More than 75 Character is not supported"); } else { int maxInvCode, maxSalesCode, maxSalesReturnCode, maxPurchaseReturnCode, maxCOGSCode, maxSalesReturnAdjCode, maxWeightLossCode, maxWeightGainCode; int InvCode, SalesCode, SalesReturnCode, PurchaseReturnCode, COGSCode, SalesReturnAdjCode, WeightLossCode, WeightGainCode; InvCode = SalesCode = SalesReturnCode = PurchaseReturnCode = COGSCode = SalesReturnAdjCode = WeightLossCode = WeightGainCode = 0; maxInvCode = MainWindow.listControl.Where(x => x.GeneralCode == 201).Select(x => x.ControlCode).LastOrDefault(); if (maxInvCode != 0) { if (maxInvCode != 20199) { maxInvCode = MainWindow.listControl.Where(x => x.GeneralCode == 201).Max(x => x.ControlCode); maxSalesCode = MainWindow.listControl.Where(x => x.GeneralCode == 601).Max(x => x.ControlCode); maxSalesReturnCode = MainWindow.listControl.Where(x => x.GeneralCode == 602).Max(x => x.ControlCode); maxPurchaseReturnCode = MainWindow.listControl.Where(x => x.GeneralCode == 707).Max(x => x.ControlCode); maxCOGSCode = MainWindow.listControl.Where(x => x.GeneralCode == 701).Max(x => x.ControlCode); maxSalesReturnAdjCode = MainWindow.listControl.Where(x => x.GeneralCode == 706).Max(x => x.ControlCode); maxWeightLossCode = MainWindow.listControl.Where(x => x.GeneralCode == 705).Max(x => x.ControlCode); maxWeightGainCode = MainWindow.listControl.Where(x => x.GeneralCode == 708).Max(x => x.ControlCode); InvCode = maxInvCode + 1; SalesCode = maxSalesCode + 1; SalesReturnCode = maxSalesReturnCode + 1; PurchaseReturnCode = maxPurchaseReturnCode + 1; COGSCode = maxCOGSCode + 1; SalesReturnAdjCode = maxSalesReturnAdjCode + 1; WeightLossCode = maxWeightLossCode + 1; WeightGainCode = maxWeightGainCode + 1; } else { Message.ShowMessage("99 Product Sector has alreadey been added!"); return; } } else { InvCode = 20101; SalesCode = 60101; SalesReturnCode = 60201; PurchaseReturnCode = 70701; COGSCode = 70101; SalesReturnAdjCode = 70601; WeightLossCode = 70501; WeightGainCode = 70801; } string Query = "INSERT INTO CHeads VALUES(@CCODE, @GCODE, @MCODE, @CHOA, @DESCRIPTION)"; string CS = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString; using (SqlConnection con = new SqlConnection(CS)) { con.Open(); SqlTransaction Trans = con.BeginTransaction(); try { SqlClass.ExecuteTransaction(Query, Param(InvCode, 201, 2, mainCatName, "Inventories"), con, Trans).ExecuteNonQuery(); SqlClass.ExecuteTransaction(Query, Param(SalesCode, 601, 6, mainCatName, "Sales"), con, Trans).ExecuteNonQuery(); SqlClass.ExecuteTransaction(Query, Param(SalesReturnCode, 602, 6, mainCatName, "Sales return"), con, Trans).ExecuteNonQuery(); SqlClass.ExecuteTransaction(Query, Param(PurchaseReturnCode, 707, 7, mainCatName, "Purchase return"), con, Trans).ExecuteNonQuery(); SqlClass.ExecuteTransaction(Query, Param(COGSCode, 701, 7, mainCatName, "Cost of Goods Sold"), con, Trans).ExecuteNonQuery(); SqlClass.ExecuteTransaction(Query, Param(SalesReturnAdjCode, 706, 7, mainCatName, "Sales return adjustment"), con, Trans).ExecuteNonQuery(); SqlClass.ExecuteTransaction(Query, Param(WeightLossCode, 705, 7, mainCatName, "Weight Loss"), con, Trans).ExecuteNonQuery(); SqlClass.ExecuteTransaction(Query, Param(WeightGainCode, 708, 7, mainCatName, "Weight Gain"), con, Trans).ExecuteNonQuery(); Trans.Commit(); ListClass lc = new ListClass(); MainWindow.listControl.Clear(); MainWindow.listControl = lc.ControlList(con); Message.ShowMessage("Necessary Ledgers have been created"); PopulateComboBox(); int cboIndex = cboMainCategory1.Items.Count - 1; cboMainCategory1.SelectedIndex = cboIndex; cboMainCategory2.SelectedIndex = cboIndex; txtNameMainCategory.Text = txtDescribeMainCategory.Text = string.Empty; } catch (Exception ex) { Trans.Rollback(); Message.ShowMessage(ex.ToString()); } } } } }