[英]Failed to convert parameter value from a DropDownList to a String
I here trying to make dropdownlist value insert into my sqldatabase
but it does not work. 我在这里尝试将dropdownlist值插入到我的
sqldatabase
但是它不起作用。 Can you help me find what I did wrong? 您能帮我找出我做错了什么吗?
This some my asp.net code for dropdownlist: 这是我的asp.net代码的dropdownlist:
<p> Hardware Type :
<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>
this some my .cs for dropdownlist 这是我的.cs下拉列表
comu.Parameters.Add("Hwtype", SqlDbType.VarChar, 50);
comu.Parameters["Hwtype"].Value = DropDownList1;
comu.Connection = con;
comu.ExecuteNonQuery();
con.Close();
}
}
i get this error : Failed to convert parameter value from a DropDownList to a String.
我收到此错误:
Failed to convert parameter value from a DropDownList to a String.
Problem : You are trying to assign the DropDownList
control instead of it's SelectedValue
. 问题:您正在尝试分配
DropDownList
控件而不是它的SelectedValue
。
Replace This: 替换为:
comu.Parameters["Hwtype"].Value = DropDownList1;
With This: 有了这个:
comu.Parameters["Hwtype"].Value = DropDownList1.SelectedValue;
OR 要么
comu.Parameters["Hwtype"].Value = DropDownList1.Items[DropDownList1.SelectedIndex];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.