Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将数据从DropDownList插入数据库asp C#_C#_Asp.net - Fatal编程技术网

如何将数据从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事件时,才会给出列表中的第一项??并验证您选择的项目