Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If I were to add more--not just ProductName=@PName--How do you place ProductDimension=@Pdimension in the string query?

Problem: How to get the product id from table where productname = combobox1 selected item and productdimension = combobox2 selected item and productcolor = combobox3 selected item

Please help me fit it in the code below.

C#
string ProductName = cboproductname.SelectedItem.ToString();
           string sql = "SELECT ProductNo FROM RawMat WHERE ProductName=@PName";

           SqlCommand cmd = new SqlCommand(sql, cn);
           cmd.Parameters.AddWithValue("@PName", ProductName);

           SqlDataReader dr = cmd.ExecuteReader();

           while (dr.Read())
           {
               txtproductno.Text = dr["ProductNo"].ToString();
           }

           dr.Close();
Posted
Comments
Herman<T>.Instance 1-Jul-12 11:12am    
what problems do you have?

1 solution

You can easily add the Product Dimension and Product Color in the query same like the Product Name with the "AND"

string ProductName = cboproductname.SelectedItem.ToString();
string ProductDimension= combobox2.SelectedItem.ToString();
string ProductColor= combobox3.SelectedItem.ToString();
string sql = "SELECT ProductNo FROM RawMat WHERE ProductName=@PName      AND ProductDimension=@PDimension AND ProductColor=@PColor";

SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@PName", ProductDimension);
cmd.Parameters.AddWithValue("@PDimension", ProductColor);
cmd.Parameters.AddWithValue("@PColor", ProductName);
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
    txtproductno.Text = dr["ProductNo"].ToString();
}

dr.Close();


Mark it as solution, If it resolved your issue.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx[^]
 
Share this answer
 
v2
Comments
AshishChaudha 2-Jul-12 7:03am    
my 5!
Shemeer NS 2-Jul-12 10:19am    
Thanks....

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