FormatException: Input string was not in the correct format

时间:2022-03-05 06:20:27

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?

它在这里抛出异常.. :(即使我将它分隔为int intID = int.Parse(DataList1.DataKeyField.ToString());为什么?

3 个解决方案

#1


3  

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.)

这表明DataList2.DataKeyField不是当前语言环境中整数的有效字符串表示形式。 (顺便说一句,您可能应该使用CultureInfo.InvariantCulture进行此格式化和可能的解析。只应使用用户的文化来处理用户输入。)

#2


1  

Because 'DataList2.DataKeyField' most likely isnt parseable, and not in the correct format.

因为'DataList2.DataKeyField'最有可能是不可解析的,而且格式不正确。

#3


1  

Server.Transfer does not support query string, it is not a page request, it is just transfer of control to another page. Store data you want to pass to another page in Context.Items['x']=yourVal and use them on another page.

Server.Transfer不支持查询字符串,它不是页面请求,它只是将控件转移到另一个页面。将要传递的数据存储到Context.Items ['x'] = yourVal中的另一个页面,并在另一个页面上使用它们。

#1


3  

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.)

这表明DataList2.DataKeyField不是当前语言环境中整数的有效字符串表示形式。 (顺便说一句,您可能应该使用CultureInfo.InvariantCulture进行此格式化和可能的解析。只应使用用户的文化来处理用户输入。)

#2


1  

Because 'DataList2.DataKeyField' most likely isnt parseable, and not in the correct format.

因为'DataList2.DataKeyField'最有可能是不可解析的,而且格式不正确。

#3


1  

Server.Transfer does not support query string, it is not a page request, it is just transfer of control to another page. Store data you want to pass to another page in Context.Items['x']=yourVal and use them on another page.

Server.Transfer不支持查询字符串,它不是页面请求,它只是将控件转移到另一个页面。将要传递的数据存储到Context.Items ['x'] = yourVal中的另一个页面,并在另一个页面上使用它们。