Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
ERROR:

Must declare the scalar variable "@ProductName".




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 :


C#
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();
///////////////////////////////////////////////////////////////////////////////////////

ERROR:

Must declare the scalar variable "@ProductName".
Posted
Updated 7-May-13 23:15pm
v2
Comments
crazie.coder 13-May-13 8:59am    
Made change as shown below
con.Open();
string qry1 = "SELECT * from Stock WHERE ProductName LIKE" + '"+cmbProductName.Text.Trim()+"';
SqlDataAdapter da = new SqlDataAdapter(qry1, con);
SqlCommand com = new SqlCommand(qry1, con);
com.ExecuteNonQuery();
da.Fill(dt);
SqlDataReader dr = com.ExecuteReader();

I believe your issue is here:
C#
da.SelectCommand.Parameters.AddWithValue("@ProductName", cmbProductName.Text);


You need to change it to:

C#
com.Parameters.AddWithValue("@ProductName", cmbProductName.Text);

As it is the com you are executing and not the data adapter.
 
Share this answer
 
Comments
Member 10036452 8-May-13 5:57am    
Fristly, I would like to appreciate your help.
secondly, I made the changes in my code. But after the changes I get the same (Must declare the scalar variable "@ProductName".) error when I want to fill my table abstract table in following codes: da.Fill(dt);

maybe my procedure is wrong, here the scenario of my code is. I want to take the text from the combobox, find the text in a culumnname : ProductName , and show the UnitPrice and Quantity which are in that particular Row of ProductName. All of the ProductName and UnitPrice and Quantity are in the same table called : Stock

so if you have any idea about this, I would appreciate if you share with me.
Pheonyx 8-May-13 6:04am    
Before you made the change, were you getting the error on the same line or the next line? i.e. SqlDataReader dr = com.ExecuteReader();

I suspect it was on the second one.

From what I make of your code you are in essence trying to read data twice, once using a data adapter and once using a command object.

I would suggest choosing one approach and working only with that.

You could use the data adapter approach, fill the data table then check it for number of rows.
If it has rows then data was retrieved, else no entry was found.

I'll draw up a quick sample.
Adding to Solution 1, since you are mentioning the Product as a dropdown, you need to make this change as well:
C#
com.Parameters.AddWithValue("@ProductName", cmbProductName.SelectedItem.Text); // Instead of cmbProductName.Text.
 
Share this answer
 
Comments
Member 10036452 8-May-13 6:03am    
Fristly, I would like to appreciate your help.
secondly, I made the changes which you mentioned in my code. But, I get an error, its not possible to put this code :cmbProductName.SelectedItem.Text, I am only allowed to use this code : cmbProductName.SelectedItem.ToString(). Even by using this, after the changes I get the same (Must declare the scalar variable "@ProductName".) error when I want to fill my table abstract table in following codes: da.Fill(dt); maybe my procedure is wrong, here the scenario of my code is. I want to take the text from the combobox, find the text in a culumnname : ProductName , and show the UnitPrice and Quantity which are in that particular Row of ProductName. All of the ProductName and UnitPrice and Quantity are in the same table called : Stock
so if you have any idea about this, I would appreciate if you share with me.
try
{
    DataTable dt = new DataTable();
    con.Open();
    string qry1 = " SELECT * from Stock WHERE (ProductName LIKE @ProductName)" ;
    SqlDataAdapter da = new SqlDataAdapter(qry1, con);

 
    da.SelectCommand.Parameters.AddWithValue("@ProductName" , cmbProductName.Text);

    da.Fill(dt);
              
    if (dt.Rows.Count == 0 )
    {
         MessageBox.Show("Item is not in the Stock, Please Add it first");
         Return;

     }else if (dr.Rows.Count > 1)
     {
         MessageBox.Show("Unique item not found");
         Return;
     }
 
     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 = float.Parse(txtUnitPrice.Text.ToString()) *           
                                               float.Parse(nudQuantity.Text.ToString());
             txtTotalPrice.Text = totalPrice.ToString();
     }
     catch (Exception error)
     {
             MessageBox.Show(error.ToString());
     }
                        
}catch (Exception error)
{
     MessageBox.Show(error.ToString());
}
Finally
{
    if(con.State == ConnectionState.Open) con.Close();
}


Rather than mixing approaches this uses the data adapter approach only.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900