Chidiebere Mgbemena

Chidiebere Mgbemena

  • NA
  • 179
  • 12.9k

error in calculation

May 22 2020 11:30 AM
Hello guys
 
Please i need help in finding the cause of this error. I got this error when i clicked total button to calculate.
 
thanks 
 
 
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace NSPM_Sales_Invoice
{
public partial class frmNewBill : Form
{
SqlConnection con = new SqlConnection(Properties.Settings.Default.NSPM_Sales_InvoiceCon);
SqlCommand cmd;
public frmNewBill()
{
InitializeComponent();
txtDeleteUpdate.Visible = false;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void frmNewBill_Load(object sender, EventArgs e)
{
cmbDescription.Select();
cmbDescription.Items.Clear();
con.Open();
cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select ProName from tbl_Products order by ProName asc";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
cmbDescription.Items.Add(dr["ProName"].ToString());
}
con.Close();
dataGridView1.Columns[5].Visible = false;
dataGridView1.Columns[6].Visible = false;
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (cmbDescription.Text == "")
{
MessageBox.Show("Description is Empty");
}
else if (txtQuantity.Text == "")
{
MessageBox.Show("Quantity is Empty");
}
else if (txtUnitPrice.Text == "")
{
MessageBox.Show("Unit Price is Empty");
}
else if (txtJobNo.Text == "")
{
MessageBox.Show("JobNo is Empty");
if (txtDeleteUpdate.Text == "")
{
int row = 0;
dataGridView1.Rows.Add();
row = dataGridView1.Rows.Count - 1;
dataGridView1["Description", row].Value = cmbDescription.Text;
dataGridView1["Quantity", row].Value = txtQuantity.Text;
dataGridView1["UnitPrice", row].Value = txtUnitPrice.Text;
dataGridView1["GoodValue", row].Value = txtGoodValue.Text;
dataGridView1["JobNo", row].Value = txtJobNo.Text;
dataGridView1["Date", row].Value = dateTimePicker1.Value.ToString("yyyy-MM-dd");
dataGridView1.Refresh();
cmbDescription.Focus();
if (dataGridView1.Rows.Count > 0)
{
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1];
}
}
else
{
btnAdd.Text = "UPDATE";
int i = 0;
DataGridViewRow row = dataGridView1.Rows[i];
row.Cells[1].Value = cmbDescription.Text;
row.Cells[2].Value = txtQuantity.Text;
row.Cells[3].Value = txtUnitPrice.Text;
row.Cells[4].Value = txtGoodValue.Text;
row.Cells[5].Value = txtJobNo.Text;
btnAdd.Text = "ADD";
}
cleartextbox();
gridGoodValue();
}
}
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = (e.RowIndex + 1).ToString();
}
int i;
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
i = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[i];
cmbDescription.Text = row.Cells[1].Value.ToString();
txtQuantity.Text = row.Cells[2].Value.ToString();
txtUnitPrice.Text = row.Cells[3].Value.ToString();
txtGoodValue.Text = row.Cells[4].Value.ToString();
txtJobNo.Text = row.Cells[5].Value.ToString();
txtDeleteUpdate.Text = row.Cells[0].Value.ToString();
btnAdd.Text = "UPDATE";
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (txtDeleteUpdate.Text == "")
{
MessageBox.Show("Select Product to Delete");
}
else
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
if (!row.IsNewRow) dataGridView1.Rows.Remove(row);
}
}
btnAdd.Text = "ADD";
gridGoodValue();
cleartextbox();
}
public void cleartextbox()
{
cmbDescription.Text = "";
txtQuantity.Text = "";
txtUnitPrice.Text = "";
txtGoodValue.Text = "";
txtJobNo.Text = "";
txtDeleteUpdate.Text = "";
}
public void CalGoodValue()
{
double a1, b1, i;
double.TryParse(txtUnitPrice.Text, out a1);
double.TryParse(txtQuantity.Text, out b1);
i = a1 * b1;
if (1 > 0)
{
txtGoodValue.Text = i.ToString("C").Remove(0, 1);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void txtQuantity_Leave(object sender, EventArgs e)
{
CalGoodValue();
}
private void txtQuantity_TextChanged(object sender, EventArgs e)
{
}
private void txtUnitPrice_Leave(object sender, EventArgs e)
{
CalGoodValue();
}
public void gridGoodValue()
{
double sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToDouble(dataGridView1.Rows[i].Cells[4].Value);
}
txtTotalGoodValue.Text =sum.ToString();
}
public void VatCal()
{
const double VatRate = 0.05;
double TotalGoodValue = Double.Parse(txtTotalGoodValue.Text);
double Vat = TotalGoodValue * VatRate;
double Total = TotalGoodValue + Vat;
txtVat.Text = Vat.ToString();
txtTotal.Text = Total.ToString();
}
public void StampDuty()
{
double a2, b2, c2, d2, e2, i;
double.TryParse(txtTotalGoodValue.Text, out a2);
double.TryParse(txtVat.Text, out b2);
double.TryParse(txtStampDuty.Text, out c2);
double.TryParse(txtDeliveryCharges.Text, out d2);
double.TryParse(txtOtherCharges.Text, out e2);
i = a2 + b2 + c2 + d2 + e2;
if (i > 0)
{
txtTotal.Text = i.ToString("C").Remove(0, 1);
}
}
private void txtValue_TextChanged(object sender, EventArgs e)
{
}
private void txtTotalGoodValue_TextChanged(object sender, EventArgs e)
{
VatCal();
}
private void txtVat_TextChanged(object sender, EventArgs e)
{
VatCal();
}
private void txtStampDuty_Leave(object sender, EventArgs e)
{
StampDuty();
}
private void txtDeliveryCharges_Leave(object sender, EventArgs e)
{
StampDuty();
}
private void txtOtherCharges_Leave(object sender, EventArgs e)
{
StampDuty();
}
private void btnSave_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count < 1)
{
MessageBox.Show("Add Minimum of one Product to Bill");
}
else
{
//Save Leader data
con.Open();
cmd = new SqlCommand("Insert Into Tbl_HeaderData (JobNo,Date,GoodValue,StampDuty,Vat,DeliveryCharges,OtherCharges,Total) values ('" + txtJobNo.Text + "','" +dateTimePicker1.Value.ToString("yyyy-MM-dd")+ "','" + txtGoodValue.Text + "','" + txtVat.Text + "','" + txtStampDuty.Text + "','" + txtDeliveryCharges.Text + "','" + txtOtherCharges.Text + "','" + txtTotal.Text + "')", con);
cmd.ExecuteNonQuery();
//Save row data
for(int i = 0; i < dataGridView1.Rows.Count;i++)
{
SqlCommand cmd1 = new SqlCommand("Insert Into Tbl_RowData(SINO,Description,Price,Quantity,Value,JobNo) values('" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[3].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[4].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[5].Value.ToString() + "')", con);
cmd1.ExecuteNonQuery();
}
MessageBox.Show("Bill Saved");
con.Close();
cleartextbox();
txtTotalGoodValue.Text = "";
txtVat.Text = "";
txtStampDuty.Text = "";
txtDeliveryCharges.Text = "";
txtOtherCharges.Text = "";
txtTotal.Text = "";
cmbDescription.Select();
}
}
}
}
 

Answers (4)