Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
System.InvalidCastException: Specified cast is not valid.


HTML
void addControl()
{
   foreach(GridViewRow row in GridView1.Rows)

   {

       int key=(int)GridView1.DataKeys[row.RowIndex].Value;
       if(row.RowType==DataControlRowType.DataRow)
       {
          DropDownList1=new DropDownList();
            DropDownList1.DataSource=list;
            DropDownList1.DataBind();
           string str=ht[key].ToString();
            DropDownList1.SelectedValue=str;
            row.Cells[2].Controls.Add(DropDownList1);
        }
}

       }
Posted
Comments
Kornfeld Eliyahu Peter 15-Dec-14 1:54am    
Debug your code and see what is the actual value you try to cast! For instance you can not cast 'Ravi' to int!!!

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900