9
Reply

DataGridView SAVE FUNCTION

Bineesh  VP

Bineesh VP

Jul 22 2013 1:09 AM
1.2k
Sir, I am doing a ADO.NET Program in c#.

as the Program's table has fk, I became confused.

let me show you my form;




and my coding for save is ;

sql Table defenition;


sql Stored procedure to add

ALTER PROCEDURE purchaseAdd
@voucherNo numeric(18,0),
@puchaseDate datetime,
@purchaseinvoiceNo numeric(18,0),
@vendorName nvarchar(20),
@grandTotal numeric(18,0)
AS

 if not exists(select voucherNo, puchaseDate, purchaseinvoiceNo, vendorName, grandTotal from tbl_PurchaseDetail where voucherNo=@voucherNo and puchaseDate=@puchaseDate and purchaseinvoiceNo=@purchaseinvoiceNo and vendorName=@vendorName and grandTotal=@grandTotal)
 begin
INSERT INTO tbl_PurchaseDetails
                      (voucherNo, puchaseDate, purchaseinvoiceNo, vendorName, grandTotal)
VALUES     (@voucherNo,@puchaseDate,@purchaseinvoiceNo,@vendorName,@grandTotal)
end


here is the Sp class of purchase


public void purchaseAdd(purchaseInfo InfoPurchase)
        {
            try
            {
                if (sqlCon.State == ConnectionState.Closed)
                {
                    sqlCon.Open();
                }
                SqlCommand c = new SqlCommand("purchaseAdd", sqlCon);
                c.CommandType = CommandType.StoredProcedure;
                c.Parameters.Add("@voucherNo", SqlDbType.Decimal).Value = InfoPurchase.voucherNo;
                c.Parameters.Add("@puchaseDate", SqlDbType.DateTime).Value = InfoPurchase.puchaseDate;
                c.Parameters.Add("@purchaseinvoiceNo", SqlDbType.Decimal).Value = InfoPurchase.purchaseinvoiceNo;
                c.Parameters.Add("@vendorName", SqlDbType.NVarChar).Value = InfoPurchase.vendorName;
                c.Parameters.Add("@grandTotal", SqlDbType.Decimal).Value = InfoPurchase.grandTotal;
                int inCount = c.ExecuteNonQuery();
                if (inCount > 0)
                {
                    MessageBox.Show("Saved Successfully");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("purSP01" + ex.Message);
            }
            finally
            {
                sqlCon.Close();
            }
               
        }

and finally the code in btnSave_click event

  int purchaseRate=0;
            int Quantity=0;
            int Discount=0;
            int grandTotal = purchaseRate * Quantity - Discount;
            purchaseInfo infoPurchase = new purchaseInfo();
            purchaseSP SpPurchase = new purchaseSP();
            infoPurchase.voucherNo = decimal.Parse(txtvoucherNo.Text);
            infoPurchase.vendorName = txtvenderName.Text;
            infoPurchase.purchaseinvoiceNo = decimal.Parse(txtpurchaseInvNo.Text);
            infoPurchase.puchaseDate = DateTime.Parse(dtpPurchaseDate.Text);
            infoPurchase.grandTotal = decimal.Parse(dgvPurchaseDetails.CurrentRow.Cells[5].ToString());
            SpPurchase.purchaseAdd(infoPurchase);

My task is:-

1) enter voucherNo,InvoiceNo,purchase Date and vender Name

2) then if I click on Add Product btn, want to go to the form frmAddProduct and when i select one of the row in gridview, the itemname(in combox column of purchase details) and purchase rate( in the purchase rate column of gridview in purchaseDetails form) to be displyed.

3) then enter Quantity, discount and the grand Total will be increment automatically.

4) then save these data by clicking in btnsave.



Answers (9)