错误:输入字符串的格式不正确. [英] ERROR : Input string was not in a correct format.
本文介绍了错误:输入字符串的格式不正确.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用C#中的convert函数将String值转换为Int值.有人可以在这方面帮助我吗?
字符串A = dt.Rows [0] ["Quno"].ToString();
Int B = Int32.Parse(A);
I am trying to convert a String value to a Int value using convert function in C#. Can anybody help me in this regards...
String A = dt.Rows[0]["Quno"].ToString();
Int B =Int32.Parse (A);
推荐答案
如果dt.Rows [0] ["Quno"].ToString();请使用以下命令包含null值,那么它将自动处理并且不会给出错误
Hi,
Please use following, if dt.Rows[0]["Quno"].ToString(); contain null value then it will automatically handle and it will not gives error
if(dt != null && dt.Rows.Count > 0)
{
Int32 B = Convert.ToInt32(dt.Rows[0]["Quno"].ToString());
}
如有任何疑问,请让我知道.
如果有帮助,请提供"投票",如果这是正确的答案,请提供"接受答案".:rose:
谢谢,
Imdadhusen
Please do let me know, if you have any doubt.
Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:
Thanks,
Imdadhusen
这是因为字符串为空或包含对数字无效的字符
我建议使用TryParse
This is because the string is blank or contains characters not valid for a number
I would recommend using TryParse
int iVal;
if (int.TryParse(myString, out iVal))
// this worked
else
// this failed
使用int.TryParse而不是Int32.Parse.确认您输入的是有效字符串.
Use int.TryParse rather than Int32.Parse. Confirm you are entering a valid string.
这篇关于错误:输入字符串的格式不正确.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文