Search code examples
c#asp.netformatexception

FormatException: Input string was not in the correct format


The exception is always thrown at the following statement..:

    protected void DataList2_SelectedIndexChanged(object sender, EventArgs e)
    {
x= should get the primary key of the item selected from  a datalist
        Server.Transfer("AnswerQuestion.aspx?x=" + int.Parse(DataList2.DataKeyField) + "&question=" +"bla bla") + "&time=" + DateTime.Now);
    }

It throws exception here..:( even when i seperate it int threadID = int.Parse(DataList1.DataKeyField.ToString()); Why?


Solution

  • Well, I strongly suspect it was this call:

    int.Parse(DataList2.DataKeyField)
    

    That suggests DataList2.DataKeyField isn't a valid string representation of an integer in the current locale. (As an aside, you should probably be doing this formatting and possibly parsing using CultureInfo.InvariantCulture. Only user input should be treated using the user's culture.)