I want to insert the selected item index from a combobox into a tinyint
column ( FoodType
) in SQL Server. I wrote the code below, but I get an exception:
Must declare the scalar variable @fType
What must I do?
string query = "INSERT INTO MenuTbl (FoodName,FoodType,FoodUnit, SalePrice)" +
"VALUES (@fName, @fType, @fUnit, @fPrice)";
connection = new SqlConnection(conectionString);
SqlCommand cmd = new SqlCommand(query,connection);
cmd.Parameters.AddWithValue("@fName", FNameTextBox.Text.Trim());
cmd.Parameters.AddWithValue("@fTyp", TypeComboBox.SelectedIndex + 1);
cmd.Parameters.AddWithValue("@funit", UnitComboBox.SelectedIndex +1);
cmd.Parameters.AddWithValue("@fprice", int.Parse(PriceEdit.Text));
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
I think you made a typo with this line of code
cmd.Parameters.AddWithValue("@fTyp", TypeComboBox.SelectedIndex + 1);
It should be @fType atleast according to your select query
string query = "Insert into MenuTbl (FoodName,FoodType,FoodUnit, SalePrice)" +
"VALUES (@fName,@fType,@fUnit, @fPrice)";
And I also think that TypeComboBox.SelectedIndex + 1
will only give you the index+1 numerical value rather than the selected text contents. Is that what you want?
您是否在参数上缺少“ e”?
cmd.Parameters.AddWithValue("@fTyp", TypeComboBox.SelectedIndex + 1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.