簡體   English   中英

無法將參數值從DropDownList轉換為字符串

[英]Failed to convert parameter value from a DropDownList to a String

我在這里嘗試將dropdownlist值插入到我的sqldatabase但是它不起作用。 您能幫我找出我做錯了什么嗎?

這是我的asp.net代碼的dropdownlist:

<p> Hardware Type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :
    <asp:DropDownList ID="DropDownList1" runat="server" Height="17px" Width="128px">
        <asp:ListItem>Please Choose</asp:ListItem>
        <asp:ListItem Value="laptop">Laptop</asp:ListItem>
        <asp:ListItem Value="server">Server</asp:ListItem>
        <asp:ListItem Value="television">Television</asp:ListItem>
        <asp:ListItem Value="printer">Printer</asp:ListItem>
    </asp:DropDownList>

這是我的.cs下拉列表

comu.Parameters.Add("Hwtype", SqlDbType.VarChar, 50);
comu.Parameters["Hwtype"].Value = DropDownList1;
comu.Connection = con;
comu.ExecuteNonQuery();
con.Close();
}
}

我收到此錯誤: Failed to convert parameter value from a DropDownList to a String.

問題:您正在嘗試分配DropDownList控件而不是它的SelectedValue

替換為:

comu.Parameters["Hwtype"].Value = DropDownList1;

有了這個:

comu.Parameters["Hwtype"].Value = DropDownList1.SelectedValue;

要么

comu.Parameters["Hwtype"].Value = DropDownList1.Items[DropDownList1.SelectedIndex];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM