system.FormatExcerption..how解决这个问题? [英] system.FormatExcerption..how to solve this?

查看:64
本文介绍了system.FormatExcerption..how解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (string.IsNullOrEmpty(profileList[i].CurrencyID))
          { boatProfile.CurrencyID = 0; }
          else
 { boatProfile.CurrencyID=int.Parse((profileList[i].CurrencyID)); }



crrencyID被声明为aspublic int? currencyID ..value in(profileList [i] .CurrencyID =Dkk

在else部分,它显示错误..int.Parse((profileList [i] .CurrencyID))扔了一个sysytem.formatException


crrencyID is declared aspublic int? currencyID ..value in (profileList[i].CurrencyID="Dkk"
In the else part it is showing the error..""int.Parse((profileList[i].CurrencyID)) threw a sysytem.formatException""

推荐答案

使用 TryParse [ ^ ]



use TryParse[^]

else
        {
            boatProfile.CurrencyID = 0;
            int.TryParse(profileList[i].CurrencyID, out boatProfile.CurrencyID);
        }


int j;
bool result = Int32.TryParse("-105", out j);
if (true == result)
    Console.WriteLine(j);
else
    Console.WriteLine("String could not be parsed.");





参考: http://msdn.microsoft.com/en-us/library/bb397679.aspx [ ^ ]



这个可以帮忙。



Refer:http://msdn.microsoft.com/en-us/library/bb397679.aspx[^]

This may help.


尝试这样转换: -

try to convert like this:-
boatProfile.CurrencyID=Convert.ToInt16((profileList[i].CurrencyID));





您尝试将数字字符串转换为数字数据类型,代码显示示例。





You attempt to cast a numeric string to a numeric data type, following code shows example.

string strInput = "10";
int intResult = 0;
try {
    intResult = Convert.ToInt16(strInput);
}
catch (Exception ex) {
    MessageBox.Show(("Error: " + ex.Message));
}







希望有帮助......




hope it helps...


这篇关于system.FormatExcerption..how解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆