2014-04-16 45 views
0

我在这里试图将dropdownlist值插入到我的sqldatabase中,但它不起作用。你能帮我找到我做错了什么吗?无法将参数值从DropDownList转换为字符串

这对于下拉列表一些我的asp.net代码:

<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.

+0

应当'comu.Parameters [ “Hwtype”]值= DropDownList1.SelectedValue; ' – afzalulh

+0

@afzalulh是的,它的工作。谢谢! – user3436232

+0

请在发布之前在您的问题上提出问题,这个问题是非常基本的使用asp.net下拉列表。 –

回答

2

问题:你正在尝试分配DropDownList控件而不是它的SelectedValue

替换此:

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

With本:

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

OR

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

非常感谢你。这项工作! – user3436232

+0

请问您的问题在Stackoverflow之前请谷歌您的答案。 –

+0

@Usama Khalil我做过谷歌他们..但大多数编码是不完全一样的,因为我做...所以很难理解一些其他人提前的代码,然后像我这样的基本代码...请理解,谢谢你。 – user3436232

相关问题