gpt4 book ai didi

mysql - 捕获复选框列表输入并分配到文本区域

转载 作者:行者123 更新时间:2023-11-29 21:02:37 25 4
gpt4 key购买 nike

我正在使用 asp.net 和 MySQL 开发一个 Web 应用程序。在我的应用程序中,用户可以添加食谱。这是一条将被插入的记录。我还必须将成分输入数据库。我正在 try catch 输入成分(以复选框的形式)。请查看 GetIngredientsInput() 方法。我正在检查哪些复选框被选中,并保存选中项目的文本。我想以列表的形式捕获输入。 (字符串列表。)我为此使用了 StringBuilder,并将其分配给文本区域控件。但代码似乎没有生效。提前致谢。

   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();
}

}


}

最佳答案

试试这个:-

 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
}

关于mysql - 捕获复选框列表输入并分配到文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37081973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com