Object must implement IConvertible

泪湿孤枕 提交于 2019-12-02 12:19:39

问题


Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of the current web request.

Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Object must implement IConvertible.

Error Detail Line 279:
da.InsertCommand.Parameters.Add("@Total", SqlDbType.Int).Value = txttotal.Text; Line 280: con1.Open(); Line 281:
da.InsertCommand.ExecuteNonQuery();

Code:

SqlDataAdapter da = new SqlDataAdapter();
    da.InsertCommand = new SqlCommand("Insert Into customer_order(ProductID,product_Name,Product_Type,Weight,Unit_Price,No_Of_Master_Pack,Master_Pack_Price,Quantity,Total)VALUES(@ProductID,@product_Name,@Product_Type,@Weight,@Unit_Price,@No_Of_Master_Pack,@Master_Pack_Price,@Quantity,@Total)", con1);
    da.InsertCommand.Parameters.Add("@ProductID",SqlDbType.VarChar).Value=DropDownList3.SelectedItem;
    da.InsertCommand.Parameters.Add("@product_Name",SqlDbType.VarChar).Value=DropDownList2.SelectedItem;
    da.InsertCommand.Parameters.Add("@Product_Type", SqlDbType.VarChar).Value = DropDownList1.SelectedItem;
    da.InsertCommand.Parameters.Add("@Weight",SqlDbType.Int).Value=txtwgt.Text;
    da.InsertCommand.Parameters.Add("@Unit_Price",SqlDbType.Int).Value=txtmrpsinglepack.Text;
    da.InsertCommand.Parameters.Add("@No_Of_Master_Pack",SqlDbType.Int).Value=txtnoofmasterpack.Text;
    da.InsertCommand.Parameters.Add("@Master_Pack_Price",SqlDbType.Int).Value=txtmrpmaster.Text;
    da.InsertCommand.Parameters.Add("@Quantity",SqlDbType.Int).Value=txtquantity.Text;
    da.InsertCommand.Parameters.Add("@Total", SqlDbType.Int).Value = txttotal.Text;
con1.Open();
da.InsertCommand.ExecuteNonQuery();

回答1:


ListControl.SelectedItem returns a ListItem - and it doesn't make much sense for the value of a parameter to be a list item itself, but instead the item's value or text. You probably want something like:

da.InsertCommand.Parameters
  .Add("@ProductID",SqlDbType.VarChar).Value = DropDownList3.SelectedItem.Text;

or

da.InsertCommand.Parameters
  .Add("@ProductID",SqlDbType.VarChar).Value = DropDownList3.SelectedItem.Value;



回答2:


use DropDownList3.SelectedItem.Text or DropDownList3.SelectedItem.Value to assign it to a Value.



来源:https://stackoverflow.com/questions/8754413/object-must-implement-iconvertible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!