Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 709
  • 1k
  • 46k

Incorrect syntax near

Jun 27 2024 10:57 AM

Hello Team,

am using textbox to scan barcode but when I enter the barcode it return this error  Incorrect syntax near '202406273, I tried severall ways to debbug it but no avail.

private void frmQty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar == 13) && (txtQty.Text != String.Empty))
            {
               bool found = false;

                string sdate = DateTime.Now.ToString("yyyyMMdd");
                cn.Open()           
                cm = new SqlCommand("select Invoice from tblCart where Invoice like @date order by CartId desc", cn);
                cm.Parameters.AddWithValue("@date", "%" + sdate + "%");
                cm.Parameters.AddWithValue("@Invoice", fSales.lblIvoiceNo.Text);
                dr = cm.ExecuteReader();
                dr.Read();
                if (dr.HasRows)
                {
                    found = true;
                    //id = dr["CartId"].ToString();
                    //cart_qty = int.Parse(dr["Qty"].ToString());
                    dr.Close();

                }
                else
                {
                    found = false;
                }
                dr.Close();
                cn.Close();

                if (found == true)
                {


                    fSales.txt_Search.Clear();
                    fSales.txt_Search.Focus();
                    fSales.LoadCart();
                    this.Dispose();
                }
                else
                {
                    // validate product quantity to show avoid selling when product is out of stock 
                    if (txtQty.Text == String.Empty)
                    {
                        cn.Close();
                        
                        return;
                    }

                    cn.Open();
                    cm = new SqlCommand("insert into tblCart (Invoice, ProductId, Price, Qty, Sdate)values(@Invoice, @ProductId, @Price, @Qty, @Sdate)", cn);
                    cm.Parameters.AddWithValue("@Invoice", fSales.lblIvoiceNo.Text);
                    cm.Parameters.AddWithValue("@ProductId", lblPID.Text);
                    cm.Parameters.AddWithValue("@price", lblPrice.Text);
                    cm.Parameters.AddWithValue("@qty", int.Parse(txtQty.Text));
                    cm.Parameters.AddWithValue("@sdate", fSales.dtTransactionDate.Value);
                    // cm.Parameters.AddWithValue("@cashier", fSales.lblUser.Text);
                    cm.ExecuteNonQuery();
                    cn.Close();
                    MessageBox.Show("Product quatity has been successfully added", stitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    fSales.txt_Search.Clear();
                    fSales.txt_Search.Focus();
                    fSales.LoadCart();

                    this.Dispose();
                }

            }

        }


Answers (9)