I'm trying to make this projects work no problems. But it is really hard to find a solution for me even asked 3 posts in here. So I've decided to ask new one. I'm gonna unleash every part of project here. May be someone will train and or do some fixes. So I asked the project maker to fix this issue but he said it's been 3 years gone and can't check it. So then for based my little experience I can't really solve it this by myself and even type on several lines of code and can't fix it. So please check it really careful.
There is no error. Only after hit print and all commands are work on into my database it deduced qty but happened like this picture:
Here is the first one the salesn.cs file of the sales:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace superpos3
{
public partial class salesn : Form
{
public salesn()
{
InitializeComponent();
}
public static int totalPrice = 0;
public static int payment = 0;
public static int balance = 0;
MySqlConnection con = new MySqlConnection("server= localhost; database =superpos; username= root; password=; ");
public void invoice()
{
con.Open();
string query = "select max(id) from salesmain ";
MySqlCommand cmd2 = new MySqlCommand(query, con);
MySqlDataReader dr;
dr = cmd2.ExecuteReader();
if (dr.Read())
{
string val = dr[0].ToString();
if (val == "")
{
lbinvoice.Text = "1";
}
else
{
int a;
a = int.Parse(dr[0].ToString());
a = a + 1;
lbinvoice.Text = a.ToString();
}
con.Close();
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void txtno_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtqty.Enabled = true;
txtqty.Focus();
}
}
private void txtqty_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
try
{
string txt = "select * from products where id='" + txtno.Text + "'";
MySqlCommand cmd = new MySqlCommand(txt, con);
con.Open();
MySqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
int price = int.Parse(txtqty.Text.ToString()) * int.Parse(r[4].ToString());
totalPrice = totalPrice + price;
//discount
// totalPrice = totalPrice - totalPrice* Payment.discount/100;
dataGridView1.Rows.Add(dataGridView1.RowCount + 1, r[0], r[1], txtqty.Text.Trim(), r[4], price);
}
lbitems.Text = " " + (dataGridView1.RowCount + 0) + "";
lbtotal.Text = " " + totalPrice + " ";
con.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, "Error From Database");
}
txtno.Focus();
txtno.Clear();
txtqty.Enabled = false;
txtqty.Clear();
}
}
private void txtqty_TextChanged(object sender, EventArgs e)
{
}
private string name;
public string Staffname
{
get { return name; }
set { name = value; }
}
public void RemoveItem()
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
string tot = row.Cells[5].Value.ToString();
if (!row.IsNewRow)
{
dataGridView1.Rows.Remove(row);
lbtotal.Text = (int.Parse(lbtotal.Text) - int.Parse(tot)).ToString();
int finaltot = int.Parse(lbtotal.Text);
totalPrice = finaltot;
lbitems.Text = "" + (dataGridView1.RowCount - 0) + "";
}
}
}
public void updatedb()
{
for (int row = 0; row < dataGridView1.Rows.Count; row++)
{
string itmno = dataGridView1.Rows[row].Cells[1].Value.ToString();
string itmqty = dataGridView1.Rows[row].Cells[3].Value.ToString();
string t = "select * from products where id= '"+ itmno + "'";
string oldqty = "", newqty = "";
Connnew.DbSearch(t);
while (Connnew.dr.Read())
{
oldqty = Connnew.dr[5].ToString();
newqty = (int.Parse(oldqty) - int.Parse(itmqty)).ToString();
}
string t2 = "update products SET qty = '" + newqty + "' WHERE id = '" + itmno + "'";
// t2 = t2.Replace("{0}", itmno);
// t2 = t2.Replace("{2}", newqty);
Connnew.DbUpdate(t2);
}
}
private void salesn_Load(object sender, EventArgs e)
{
label6.Text = Staffname;
lbldate.Text = DateTime.Today.ToString("dd/MM/yyyy");
lbltime.Text = DateTime.Now.ToShortTimeString();
invoice();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandText = "Insert into salesproducts(saleid,productname,qty,grosstotal)values(@saleid,@productname,@qty,@grosstotal)";
cmd.Parameters.AddWithValue("@saleid", lbinvoice.Text);
cmd.Parameters.AddWithValue("@productname", dataGridView1.Rows[i].Cells[2].Value);
cmd.Parameters.AddWithValue("@qty", dataGridView1.Rows[i].Cells[3].Value);
cmd.Parameters.AddWithValue("@price", dataGridView1.Rows[i].Cells[4].Value);
cmd.Parameters.AddWithValue("@grosstotal", dataGridView1.Rows[i].Cells[5].Value);
MySqlCommand cmd1 = new MySqlCommand();
cmd1.Connection = con;
cmd1.CommandText = "insert into salesmain(id,date,time,cashername,qty,grosstotal)values(@id,@date,@time,@cashername,@qty,@grosstotal)";
cmd1.Parameters.AddWithValue("@id", lbinvoice.Text);
cmd1.Parameters.AddWithValue("@date", lbldate.Text);
cmd1.Parameters.AddWithValue("@time", lbltime.Text);
cmd1.Parameters.AddWithValue("@cashername", label6.Text);
cmd1.Parameters.AddWithValue("@qty", lbitems.Text);
cmd1.Parameters.AddWithValue("@grosstotal", lbtotal.Text);
con.Open();
int x = cmd.ExecuteNonQuery();
int y = cmd1.ExecuteNonQuery();
MessageBox.Show("Record added ..........");
updatedb();
dataGridView1.Rows.Clear();
lbtotal.Text = "0";
lbitems.Text = "0";
txtno.Focus();
totalPrice = 0;
con.Close();
invoice();
}
}
private void button2_Click(object sender, EventArgs e)
{
pay p = new pay();
p.Show();
}
private void label6_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void salesn_Activated(object sender, EventArgs e)
{
label7.Text = "Баланс = " + balance;
}
private void button3_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("No Transactions");
txtno.Focus();
}
else
{
DialogResult result = MessageBox.Show("Are you going delete the record ?", "POS", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
RemoveItem();
txtno.Focus();
}
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void txtno_TextChanged(object sender, EventArgs e)
{
}
}
}
like the design look like this.
Connew
sing MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace superpos3
{
class Connnew
{
public static MySqlConnection con = new MySqlConnection("server= localhost; database =superpos; username= root; password=; ");
public static MySqlCommand poscmd = new MySqlCommand();
public static MySqlDataReader dr;
public static void DbSearch(string txt)
{
con.Close();
poscmd.Connection = con;
poscmd.CommandText = txt;
con.Open();
dr = poscmd.ExecuteReader();
}
public static void DbUpdate(string txt)
{
con.Close();
poscmd.Connection = con;
poscmd.CommandText = txt;
con.Open();
poscmd.ExecuteNonQuery();
}
}
}
The products, inventory, user, reports, item change and items all works fine into my database tables. Only error happens after buy products from products inventory and can't getting proceed the after like:
updatedb();
dataGridView1.Rows.Clear();
lbtotal.Text = "0";
lbitems.Text = "0";
txtno.Focus();
totalPrice = 0;
con.Close();
invoice();
}
}
Because after the update() method inside the for loop that's happens the error.
please click and check out the error
So after click the copy details button of errors it is like:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=superpos3
StackTrace:
at superpos3.salesn.updatedb() in D:\superpos3\superpos3\superpos3\salesn.cs:line 174
at superpos3.salesn.button1_Click(Object sender, EventArgs e) in D:\superpos3\superpos3\superpos3\salesn.cs:line 259
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at superpos3.Program.Main() in D:\superpos3\superpos3\superpos3\Program.cs:line 19
Please could you provide any solutions or any additional code of lines?
User contributions licensed under CC BY-SA 3.0