Object must implement IConvertible

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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;

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x