2017-05-03 40 views
-1

這段代碼給我空數據庫如何從DropDownList的數據插入到數據庫的ASP C#

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(); 
+1

顯示您的完整代碼。你在DropDownListCategory.SelectedItem.Value中得到什麼 – DevelopmentIsMyPassion

+0

沒有更多的信息就無法說出你的代碼有什麼問題。在[如何問]上鎖定(https://stackoverflow.com/help/how-to-ask)。 「DropDownListCategory.SelectedItem.Value」的值是什麼? –

+0

你有沒有試過'DropDownListCategory.SelectedValue'? –

回答

0

試試這個,

cmd2.Parameters.AddWithValue("@Fk_CatID", DropDownListCategory.SelectedValue); 

並在此之前,驗證所選值不爲空

string s=DropDownListCategory.SelectedValue; 
+0

這隻給列表中的第一項 – Abdulrhman

+0

你在處理DropDownListCategory- OnSelectedIndexChanged事件嗎?並驗證您選擇的項目 – user7415073

+0

否如何處理OnSelectedIndexChanged事件 – Abdulrhman

0

如果你想插入的數據,寫插入查詢,而不是選擇。

如果你想選擇數據......你可以驗證如下,

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

試試看

cmd2.Parameters.AddWithValue( 「@ Fk_CatID」,DropDownListCategory .SelectedValue);