如何将数据从DropDownList插入数据库asp C#
此代码在数据库中为空如何将数据从DropDownList插入数据库asp C#,c#,asp.net,C#,Asp.net,此代码在数据库中为空 cmd2.Parameters.AddWithValue("@Fk_CatID", DropDownListCategory.SelectedItem.Value); string queryCategory = "select CatName from CategoryItemsTBL"; DataTable dtCategory = GetDataCategory(queryCategory); DropDownListCategory.DataSource = dt
cmd2.Parameters.AddWithValue("@Fk_CatID", DropDownListCategory.SelectedItem.Value);
string queryCategory = "select CatName from CategoryItemsTBL";
DataTable dtCategory = GetDataCategory(queryCategory);
DropDownListCategory.DataSource = dtCategory;
DropDownListCategory.DataTextField = "CatName";
DropDownListCategory.DataValueField = "CatName";
DropDownListCategory.DataBind();
此代码在数据库中为空
cmd2.Parameters.AddWithValue("@Fk_CatID", DropDownListCategory.SelectedItem.Value);
string queryCategory = "select CatName from CategoryItemsTBL";
DataTable dtCategory = GetDataCategory(queryCategory);
DropDownListCategory.DataSource = dtCategory;
DropDownListCategory.DataTextField = "CatName";
DropDownListCategory.DataValueField = "CatName";
DropDownListCategory.DataBind();
试试这个
cmd2.Parameters.AddWithValue("@Fk_CatID", DropDownListCategory.SelectedValue);
在此之前,请验证所选值是否为空
像
如果要插入数据,请编写插入查询,而不是选择 如果要选择数据。。。您可以按如下方式进行验证:
using(SqlDataReader reader=cmd.ExecuteReader())
{
if(reader.Hasrows)
{
while(reader.Read())
{
//We can do validation like this
ClassnameObj.YourLabelName=reader.IsDBNull(0)?null:reader.GetString(0);
}
}
}
试试看
cmd2.Parameters.AddWithValue(“@Fk_CatID”,DropDownListCategory.SelectedValue)
显示您的完整代码。在DropDownList Category.SelectedItem.Value中获得的内容如果没有更多信息,就无法说出代码的错误。有一个锁。
DropDownListCategory.SelectedItem.value
的值是多少?您是否尝试过DropDownListCategory.SelectedValue
?仅当您处理DropDownListCategory-OnSelectedIndexChanged事件时,才会给出列表中的第一项??并验证您选择的项目