4
Answers

How to Store Records In Database Windows Application C#

Jyoti Jodha

Jyoti Jodha

7y
275
1
Table1 

sno

Product name

dec

QTY

Unit_Price

Tax

Total_Amount

Tax_Amount

Grand_Total_Amount

 

1

mobile

no

2

5000

10

10000

1050

11550

 

2

ABC

no

1

500

10

500

1050

11500

 

0

           

1050

11550

 

Table2

sno

Product name

dec

QTY

Unit_Price

Tax

Total_Amount

Tax_Amount

Grand_Total_Amount

 

1

mobile

no

2

5000

10

10000

1000

11000

 

2

ABC

no

1

500

10

500

50

550

 

0

0

0

0

0

0

10500

1050

11550

 

My Records Save like Table1

And I am want to save records table 2

How to do.....???

 

// Insert data in Grid view
private void addbutton_Click(object sender, EventArgs e)
{
if (producttxt.Text != "" && dectxt.Text != "" && qtytxtprice.Text != "" && txtqty.Text != "" && txttax.Text != "")
{
int Qty = Convert.ToInt32(txtqty.Text);

decimal Unit_Price = Convert.ToDecimal(qtytxtprice.Text);

decimal Tax = Convert.ToDecimal(txttax.Text);

decimal Amount = (Qty * Unit_Price);

decimal PercentageAmount = ((Amount * Tax) / 100);

decimal TotalAmount = Amount + PercentageAmount;

int Sno = dataGridView.Rows.Count ;

dataGridView.Rows.Add(Sno, producttxt.Text, dectxt.Text, txtqty.Text, qtytxtprice.Text, txttax.Text, TotalAmount);

btnclr_Click(sender, e);

CalculateTotalAmount();

ClearData();

}

}

public void CalculateTotalAmount()

{

decimal taxtotal=0;

decimal totalamount=0;

for(int i=0;i<dataGridView.Rows.Count-1;i++)

{

decimal TotalAmount = (Convert.ToDecimal(dataGridView.Rows[i].Cells[3].Value) * Convert.ToDecimal(dataGridView.Rows[i].Cells[4].Value));

taxtotal=taxtotal+((Convert.ToDecimal(dataGridView.Rows[i].Cells[5].Value)*TotalAmount)/100);

totalamount=totalamount+Convert.ToDecimal(dataGridView.Rows[i].Cells[6].Value);

}

TotalTaxtxt.Text=taxtotal.ToString(); //tax total amount text box field value setting

totalamounttxt.Text=totalamount.ToString(); //total amount text box field value setting

}

// Insert records in databse

private void btn_Insert_Click(object sender, EventArgs e)

{

int Billno = billno("Select ISNULL(Max(Billno+1),1) From MainBillForm");

foreach (DataGridViewRow g1 in dataGridView.Rows)

{

SqlCommand cmd = new SqlCommand("insert into MainBillForm (Billno,Name,Mobile_no,Date,Order_Number,Sno,Product_Name,Description,Qty,Unit_Price,Tax,Amount,Tax_Amount,Total_Amount) Values ('" + Billno + "','" + Nametxt.Text + "','" + Phtxt.Text + "','" + dateTimePicker1.Value + "','" + ONtxt.Text + "','" + g1.Cells[0].FormattedValue.ToString() + "','" + g1.Cells[1].FormattedValue.ToString() + "','" + g1.Cells[2].FormattedValue.ToString() + "','" + g1.Cells[3].FormattedValue.ToString() + "','" + g1.Cells[4].FormattedValue.ToString() + "','" + g1.Cells[5].FormattedValue.ToString() + "','" + g1.Cells[6].FormattedValue.ToString() + "','" + TotalTaxtxt.Text + "','" + totalamounttxt.Text + "')", con);

con.Open();

cmd.ExecuteNonQuery();

MessageBox.Show("Records Inserted SuccessFully");

this.Hide();

MainBillfrm sd = new MainBillfrm();

sd.Show();

con.Close();

}

}


Answers (4)