Here is a snippet of my code:
else
{
SubCategory subCat = new SubCategory
{
SubCategoryName = name,
Active = false,
CategoryID=Convert.ToInt32(ddlCategory.SelectedValue)
};
db.SubCategories.InsertOnSubmit(subCat);
}
db.SubmitChanges();
The following line is causing an error:
CategoryID=Convert.ToInt32(ddlCategory.SelectedValue)
I have confirmed that the SelectedValue in my DDL is an int, and that the database is expecting an int, so I don’t understand why asp.net gives me a YSOD saying “Input string was not in a correct format.”
If I assign CategoryID a number manually, it works.
EDIT: The problem was because I was populating the drop down list in my code behind and I didn’t wrap it in a (!IsPostBack). So it was destroying the list, repopulating it and setting the index at 0 each time on post back.
so try this then