Must Declare a Scalar Variable

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi guys:
I am implementing a POS system. My database and everything is ready. My Purchase Entry page is connected to my Stock database. Therefor, I want the user to select the product from a Combobox and application should Find the product in database and assign the UnitPrice and Quantity into the textboxes provided in Form.
Error:
Note: in my code : TotalPrice is to calculate : unitPrice * Quantity and is not comming from database. it should be done once cmbProductName_SelectedIndexChanged occured.
My code :


private void cmProductName_SelectedIndexChanged(object sender, EventArgs e)
{

// string ProductName = cmbProductName.Text.ToString();
try
{
DataTable dt = new DataTable();


con.Open();
string qry1 = "SELECT * from Stock WHERE (ProductName LIKE @ProductName)";
SqlDataAdapter da = new SqlDataAdapter(qry1, con);
SqlCommand com = new SqlCommand(qry1, con);

da.SelectCommand.Parameters.AddWithValue("@ProductName", cmbProductName.Text);
// com.Parameters.AddWithValue("@ProductName", this.txtProductID.Text);
// com.Parameters.Add(new SqlParameter("@ProductName",this.txtProductID.Text.ToString()));
com.ExecuteNonQuery();
da.Fill(dt);
SqlDataReader dr = com.ExecuteReader();


while (dr.Read())
{
if (dr.HasRows == true)
{

nudQuantity.Value =Int32.Parse( dt.Rows[0][4].ToString());
nudQuantity.Maximum = Int32.Parse(dt.Rows[0][4].ToString());
txtUnitPrice.Text = dt.Rows[0][5].ToString();
try
{
float totalPrice;
totalPrice = float.Parse(txtUnitPrice.Text.ToString()) * float.Parse(nudQuantity.Text.ToString());
//int totalPrice = (Convert.ToInt32(txtUnitPrice.Text)) * 2;//(float.Parse(numericUpDown1.ToString()));
txtTotalPrice.Text = totalPrice.ToString();
//Convert.ToString(totalPrice);

//printDocument1.Print();
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}



}
}

if (dr.HasRows == false)
{
MessageBox.Show("Item is not in the Stock, Please Add it first");
}


}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
con.Close();

///////////////////////////////////////////////////////////////////////////////////////

View the full article
 
Back
Top