Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (cbProductDescription.SelectedItem.ToString() == "Prepaid - 300")
                {
                    command.Parameters.AddWithValue("@Price", 7);
                }
Posted
Updated 18-Feb-15 19:04pm
v2

1 solution

If I understood the question correctly what you should do is either enumerate the choices in you combo box or fetch them from the database and include the key value already generated in the database. Now this key value would be used in your other command parameters. If this is forms for example, use the ValueMember[^] property to assign the key value to a combo box item.
 
Share this answer
 
Comments
_bluRe_ 19-Feb-15 1:28am    
this is the whole code. from the moment the user pick the list of products in the cbproductdescription, i want to have a corresponding value or price that should already generated from the chosen product. is the code below possible for that kind of output? :))
try
{
SqlCeConnection connection = new SqlCeConnection("DATASOURCE=Database1.sdf");
connection.Open();
SqlCeCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO NewInventory(Product Description,Stock,Month,Model Number,Price,Total Cost) VALUES (@Product,@Stock,@Month,@Model,@Price,@Total)";
command.Parameters.AddWithValue("@Product", cbProductDescription.SelectedItem.ToString());
command.Parameters.AddWithValue("@Stock", txtQuantityInventory.Text);
command.Parameters.AddWithValue("@Month", cbMonth.SelectedItem.ToString());
command.Parameters.AddWithValue("@Model", txtModelNumber.Text);
if (cbProductDescription.SelectedItem.ToString() == "Prepaid - 300")
{
command.Parameters.AddWithValue("@Price", 7);
}
command.Parameters.AddWithValue("@Total", 0);
command.Prepare();
command.ExecuteNonQuery();
connection.Close();
Wendelius 19-Feb-15 2:14am    
I don't think that would work correctly. Based on the selection in the combo box you should define a value for both Price and Total in both cases. Otherwise you have a parameter in a query text but you don't provide any value to it.
_bluRe_ 19-Feb-15 2:27am    
i knew it. that's what i've been thinking the whole time. what would be the best solution for this?
Wendelius 19-Feb-15 2:30am    
If you don't have a value then I take it that in your scenario you should set the column as NULL in the database. In order to do that, define the value of the parameter to DBNull.Value[^]
_bluRe_ 19-Feb-15 9:39am    
hmm so that another process right?

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