我正在使用asp.net和MySQL开发一个Web应用程序。在我的应用中,用户可以添加食谱。这是一个将被插入的记录。我也必须将成分输入到数据库中。我试图捕获输入成分(以复选框的形式。)请查看GetIngredientsInput()方法。我正在检查选中了哪些复选框,并保存所选项目的TEXT。我想以列表的形式捕获输入。 (字符串列表。)我使用StringBuilder,并将其分配给textarea控件。但代码似乎没有效果。提前谢谢。
protected void AddRecipe(object sender, EventArgs e)
{
GetIngredientsInput();
MySqlConnection con = new MySqlConnection("Server=localhost;Database=FreedomKitchen;Uid=root;Password=;");
con.Open();
String strcmd = "insert into Recipes(Recipe_ID,Food_Category,Meal_Category,Recipe_Name,All_Ingredients,Recipe_Description,Recipe_Instructions) values(@Recipe_ID,@Food_Category,@Meal_Category,@Recipe_Name,@All_Ingredients,@Recipe_Description,@Recipe_Instructions)";
MySqlCommand cmd = new MySqlCommand(strcmd, con);
MySqlCommand cmd_two = new MySqlCommand("select * from Recipes", con);
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = cmd_two;
DataSet ds = new DataSet();
da.Fill(ds, "Rows");
int row_count = ds.Tables["Rows"].Rows.Count;
r_id = row_count + 1;
cmd.Parameters.Add(new MySqlParameter("@Recipe_ID", r_id));
cmd.Parameters.Add(new MySqlParameter("@Food_Category", DropDownFoodCat.Text.ToString()));
cmd.Parameters.Add(new MySqlParameter("@Meal_Category", DropDownMealCat.Text));
cmd.Parameters.Add(new MySqlParameter("@Recipe_Name", txtRecipeName.Text));
cmd.Parameters.Add(new MySqlParameter("@All_Ingredients", txtAllIngredients.Text));
cmd.Parameters.Add(new MySqlParameter("@Recipe_Description", txtDescription.Text));
cmd.Parameters.Add(new MySqlParameter("@Recipe_Instructions", txtInstructions.Text));
cmd.ExecuteNonQuery();
con.Close();
}
public void GetIngredientsInput() {
foreach (ListItem li in CheckBoxListVeg.Items) {
if (li.Selected) {
String ing_name=li.Text.ToString();
str_ing_name.Append(ing_name);
str_ing_name.Append("\n");
txtAllIngredients.Text = ing_name = li.Text.ToString();
}
}
}
答案 0 :(得分:0)
试试这个: -
public string GetIngredientsInput()
{
StringBuilder str_ing_name = new StringBuilder();
foreach (ListItem li in CheckBoxListVeg.Items)
{
if (li.Selected)
{
String ing_name = li.Text.ToString();
str_ing_name.Append(ing_name);
str_ing_name.Append(",");
}
}
return str_ing_name.ToString();
}
protected void AddRecipe(object sender, EventArgs e)
{
txtAllIngredients.Text= GetIngredientsInput();
// Rest of code
}