我有一个名为PurchaseInvoiceForm的表格。当我在DataGridView中添加多个产品时,它具有许多产品。他从不保存所有项目,但显示出一些错误。我有一个我创建的DLL 用于数据保存|检索并对其执行一些其他操作。这是我使用的以下代码。
这是我单击“保存按钮”时的第一件事。
private void SaveMetroTile_Click(object sender, EventArgs e)
{
if (IsFormOk())
{
if (this.IsUpdate)
{
//Do Update Process
SaveOrUpdateStock("usp_PurchaseInvoiceSaveOrUpdateStockDetails");
ShowSucessMessage("Purchase Invoice Has Been Updated Successfully");
}
else
{
//Do Save Process
SaveOrUpdateStock("usp_PurchaseInvoiceSaveOrUpdateStockDetails");
ShowSucessMessage("Purchase Invoice Has Been Added Successfully");
}
}
}
这是用于保存或更新接受存储过程和对象的记录的方法。
private void SaveOrUpdateStock(string storedProceName)
{
DbSQLServer db = new DbSQLServer(AppSetting.ConnectionString());
db.SaveOrUpdateRecord(storedProceName, GetObject());
}
private StockDetails GetObject()
{
StockDetails stock = new StockDetails();
try
{
stock.ProcessType = (this.IsUpdate) ? (int)ProcessType.Update : (int)ProcessType.Save;
stock.PurchaseInvoiceId = (this.IsUpdate) ? this.Id : 0; //Zero Means No Id Just Save
stock.PurchaseInvoiceNo = Convert.ToInt32(InvoiceNoMetroTextBox.Text.Trim());
stock.PurchaseInvoiceDate = PurchaseInvoiceDateMetroDateTime.Value.Date;
stock.InvoiceNo = Convert.ToInt32(PurchaseInvoiceNoMetroTextBox.Text.Trim());
stock.InvoiceDate = PurchaseInvoiceDateMetroDateTime.Value.Date;
stock.PartyId =Convert.ToInt32(PartyNameMetroComboBox.SelectedValue);
stock.FromCity = Convert.ToInt32(FromCityMetroComboBox.SelectedValue);
stock.ToCity = Convert.ToInt32(ToCityMetroComboBox.SelectedValue);
foreach (DataGridViewRow row in PurchaseInvoiceMetroGrid.Rows)
{
stock.ProductName= row.Cells["ProductName"].Value.ToString();
stock.ProductCode = Convert.ToInt32(row.Cells["ProductCode"].Value);
stock.Box = Convert.ToDecimal(row.Cells["Box"].Value);
stock.Quantity = Convert.ToInt32(row.Cells["Quantity"].Value);
stock.UnitPrice = Convert.ToInt32(row.Cells["Price"].Value);
stock.DiscountInPercent = Convert.ToDecimal(row.Cells["DiscountInPercent"].Value);
stock.DiscountAmount = Convert.ToDecimal(row.Cells["DiscountAmount"].Value);
stock.Amount = Convert.ToDecimal(row.Cells["Amount"].Value);
}
stock.SubTotal =Convert.ToDecimal(SubTotalMetroTextBox.Text.Trim());
stock.DiscountedAmount = Convert.ToDecimal(DiscountedAmountMetroTextBox.Text);
stock.DiscountPercentage = Convert.ToDecimal(DiscountPercentageMetroTextBox.Text);
stock.Tax = Convert.ToInt32(TaxMetroTextBox.Text.Trim());
stock.Freight = Convert.ToInt32(FreightMetroTextBox.Text.Trim());
stock.NetAmount = Convert.ToDecimal(NetAmountMetroTextBox.Text.Trim());
stock.AllFreight =Convert.ToDecimal(AllFreightMetroTextBox.Text.Trim());
stock.AllTax = Convert.ToDecimal(AllTaxMetroTextBox.Text.Trim());
stock.DriverName = DriverNameMetroTextBox.Text.Trim();
stock.Mobile = MobileMetroTextBox.Text.Trim();
stock.Vehicle = VehicleNoMetroTextBox.Text.Trim();
stock.Remarks = RemarksMetroTextBox.Text.Trim();
stock.LastUpdatedBy = LoggedInUser.UserName;
stock.CreatedBy = LoggedInUser.UserName;
}
catch(Exception ex)
{
ShowErrorMessage("Error : " + ex.Message);
}
return stock;
}