Johan Hjalmarsson
Johan Hjalmarsson

Reputation: 3493

invalid cast. Viewstate and gridview row

I stumbled upon an error in my code that I can't figure out. Here is the code in question:

int indexToSave = -1;
const string EDIT = "edit";
if (ViewState[EDIT] != null)
{
    for (int i = 0; i < gvSensorList.Rows.Count; i++)
    {
        if (((string)ViewState[EDIT]) == (string)gvList.Rows[i].ClientID)
        {
            indexToSave = i;
        }
    }
}

The line that throws the exception is if(((string)ViewState[EDIT] == (string)gvList.Rows[i].ClientID). The exception I get is Unable to cast object of type 'System.Int32' to type 'System.String' but as far as I can tell, I dont cast any int to string?

Upvotes: 0

Views: 191

Answers (1)

astian
astian

Reputation: 684

Have you tried if ((ViewState[EDIT].ToString()) == gvList.Rows[i].ClientID.ToString()) ?

Upvotes: 1

Related Questions