Hello Team,
When I Add item to the list by entring product barcode into the Search textbox and the same item is added again it appears on a new row means while I want the quantity and total sales to increase when the sames item is selected again but in this case it appears on a new rows.
public void ADDToLIST()
{
try
{
// int i = 0;
using (SqlConnection cn = new SqlConnection(dbcon.MyConnection()))
{
cn.Open();
using (SqlCommand cm = new SqlCommand("select ProductId, PCode, ProductDate, ProductName, Category, CostPrice, Profit, SalesPrice, Tax, TotalPrice,StockIn, Barcode from tblProduct where Barcode like @searchBarcode", cn))
{
cm.Parameters.AddWithValue("@searchBarcode", "%" + txt_Search_Product_Barcode.Text + "%");
using (SqlDataReader dr = cm.ExecuteReader())
{
while (dr.Read())
if (txt_Search_Product_Barcode.Text == String.Empty)
{
return;
}
else
{
string productId;
string pcode;
string productname;
string category;
decimal saleprice;
decimal tax;
string totalprice;
decimal totalqtyprice;
productId = dr["ProductId"].ToString();
pcode = dr["PCode"].ToString();
productname = dr["ProductName"].ToString();
category = dr["Category"].ToString();
saleprice = Convert.ToDecimal(dr["SalesPrice"].ToString());
tax = Convert.ToDecimal(dr["Tax"].ToString());
totalprice = dr["TotalPrice"].ToString();
totalqtyprice =Convert.ToDecimal(saleprice * (tax) / 100) + saleprice;
dataGridView1.Rows.Add(productId, pcode, productname, category, saleprice, tax, totalprice, 1, totalqtyprice.ToString("#,##0.00"));
txt_Search_Product_Barcode.Clear();
txt_Search_Product_Barcode.Focus();
}
}
}
}
}
catch (Exception ex)
{
if (dr != null && !dr.IsClosed)
{
dr.Close();
}
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
dataGridView1.Rows[numrows].Cells[7].Value = int.Parse("1") + numtext;
dataGridView1.Rows[numrows].Cells[8].Value = dataGridView1.Rows[numrows].Cells[6].Value * dataGridView1.Rows[numrows].Cells[7].Value;
}
Jithu ThomasPosted Jun 1, 2024, 5:58 AM
Mr. Emmmanuel FIADUFE
I think we solved the issue during our last anydesk session.
This was the code we worked on.
Regards,
Jithu Thomas
Emmmanuel FIADUFEPosted Jun 1, 2024, 6:34 PM
Thank you team, it is working now
Tuhin PaulPosted May 31, 2024, 6:56 PM
The issue is that you're directly adding a new row to your dataGridView1 every time you scan a barcode, without checking if the product already exists in the grid.
To fix this, you need to check if the product already exists in the grid before adding a new row. If it does, you can simply increase the quantity and calculate the new total price.
This code first checks if the product already exists in the grid by comparing the
ProductIdof the scanned product with theProductIdof the products in the grid. If the product exists, it increases the quantity and calculates the new total price. If the product doesn't exist, it adds a new row to the grid.Emmmanuel FIADUFEPosted May 29, 2024, 5:46 PM
I was searching on how to go about and I found this code in C++ but I didn't know how to convert it into C#
Dim exist As Booleen = false, numrow As Integer = 0, numtext As Integer
For Each item As DataGridViewRow In DataGridView.Rows
if item.Cells(0).Value IsNot Nothing Then
if item.Cells(1).Value.ToString = txt_SearchProduct_Barcode.Text then
exist = True
numtext = CInt(item.Cells(7)
Exit for
End If
End If
Next
If exist = False then
Try
cn.Open()
cm = new SqlCommand()
End Try
End If
End Sub