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 ThomasRank #437 · 5.7K reputationPosted Jun 1, 2024, 1:58 AM EDT
Mr. Emmmanuel FIADUFE
I think we solved the issue during our last anydesk session.
This was the code we worked on.
public void ADDToLIST() { try { 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 (string.IsNullOrEmpty(txt_Search_Product_Barcode.Text)) { return; } string productId = dr["ProductId"].ToString(); string pcode = dr["PCode"].ToString(); string productname = dr["ProductName"].ToString(); string category = dr["Category"].ToString(); decimal saleprice = Convert.ToDecimal(dr["SalesPrice"]); decimal tax = Convert.ToDecimal(dr["Tax"]); decimal totalprice = Convert.ToDecimal(dr["TotalPrice"]); decimal totalqtyprice = saleprice + (saleprice * tax / 100); bool itemFound = false; foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells["ProductId"].Value != null && row.Cells["ProductId"].Value.ToString() == productId) { // Update the quantity and total price of the existing row int quantity = Convert.ToInt32(row.Cells["Quantity"].Value) + 1; row.Cells["Quantity"].Value = quantity; decimal updatedTotalPrice = saleprice * quantity; row.Cells["TotalPrice"].Value = updatedTotalPrice.ToString("#,##0.00"); itemFound = true; break; } } if (!itemFound) { // Add new row if the item is not found in the grid dataGridView1.Rows.Add(productId, pcode, productname, category, saleprice, tax, totalprice.ToString("#,##0.00"), 1, totalqtyprice.ToString("#,##0.00")); } txt_Search_Product_Barcode.Clear(); txt_Search_Product_Barcode.Focus(); CalculateTotal(); } // Calculate and display total quantity after processing all rows int totalQuantity = CalculateTotalQuantity(); lblTotalQuantity.Text = totalQuantity.ToString(); // Assuming you have a label to display total quantity } } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }Regards,
Jithu Thomas
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted May 29, 2024, 1:46 PM EDT
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
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Jun 1, 2024, 2:34 PM EDT
Thank you team, it is working now
Tuhin PaulRank #7 · 71.9K reputationPosted May 31, 2024, 2:56 PM EDT
public void ADDToLIST() { try { 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()) { if (txt_Search_Product_Barcode.Text == String.Empty) { return; } while (dr.Read()) { string productId = dr["ProductId"].ToString(); // Check if the product already exists in the dataGridView1 bool productExists = dataGridView1.Rows.Cast().Any(row => row.Cells[0].Value.ToString() == productId); if (productExists) { // If the product exists, increase the quantity and calculate the new total price int rowIndex = dataGridView1.Rows.Cast().ToList().FindIndex(row => row.Cells[0].Value.ToString() == productId); decimal currentQuantity = Convert.ToDecimal(dataGridView1.Rows[rowIndex].Cells[7].Value); dataGridView1.Rows[rowIndex].Cells[7].Value = currentQuantity + 1; decimal saleprice = Convert.ToDecimal(dr["SalesPrice"].ToString()); decimal tax = Convert.ToDecimal(dr["Tax"].ToString()); decimal newTotalPrice = Convert.ToDecimal(saleprice * (tax) / 100) + saleprice; dataGridView1.Rows[rowIndex].Cells[8].Value = newTotalPrice.ToString("#,##0.00"); } else { // If the product doesn't exist, add a new row string pcode = dr["PCode"].ToString(); string productname = dr["ProductName"].ToString(); string category = dr["Category"].ToString(); decimal saleprice = Convert.ToDecimal(dr["SalesPrice"].ToString()); decimal tax = Convert.ToDecimal(dr["Tax"].ToString()); string totalprice = dr["TotalPrice"].ToString(); decimal 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); } }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.