传递null时获取格式异常 [英] Getting the format exception when passing null
本文介绍了传递null时获取格式异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到格式异常错误问题是,如果 如果他们不发送任何内容,则字段为空  ""将生成
,我需要这些金额将其转换为Int32,并从这些"&"算作字符串这个对话不会执行,我得到异常错误。
if(oInsert.Insert_Goods(Convert.ToString(tbGoodsName.Text),Convert.ToString(tbGoodsCode) .Text),Convert.ToInt64(cmbGoodsUnit.SelectedValue),Convert.ToInt64(cmbGoodsCat.SelectedValue),Convert.ToInt32(tbGoodsMax.Text.Replace(",","")),Convert.ToInt32(tbGoodsMin) .Text.Replace(",","")),Convert.ToInt32(tbOrderPoint.Text.Replace(",","")),Convert.ToInt64(tbBuyPrice.Text.Replace (",","")),Convert.ToInt64(tbDiscount.Text.Replace(",","")),Convert.ToInt32(tbSellPrice.Text.Replace(",, ","")),Convert.ToString(tbDes.Text))== false)
解决方案
如果要将字符串转换为整数,请使用TryParse
C#7示例,其中结果有效在方法中使用Tryparse 。知道
out variables 。if(int.TryParse(textBox1.Text,out var results))
{
//使用结果
}
else
{
//结果不是有效的int
}在C#7之前
int result = 0;
if(int.TryParse(textBox1.Text,out result))
{
//结果有效
}
其他
{
//结果不是有效的int
}
I get format exception error the problem is if fields are empty if they don't send anything "" will be produced
and i need these amounts to convert it to Int32 and from when these "" count as string this conversation wont be executed and i get exception error.
if (oInsert.Insert_Goods(Convert.ToString(tbGoodsName.Text), Convert.ToString(tbGoodsCode.Text), Convert.ToInt64(cmbGoodsUnit.SelectedValue), Convert.ToInt64(cmbGoodsCat.SelectedValue), Convert.ToInt32(tbGoodsMax.Text.Replace(",", "")), Convert.ToInt32(tbGoodsMin.Text.Replace(",", "")), Convert.ToInt32(tbOrderPoint.Text.Replace(",", "")), Convert.ToInt64(tbBuyPrice.Text.Replace(",", "")), Convert.ToInt64(tbDiscount.Text.Replace(",", "")), Convert.ToInt32(tbSellPrice.Text.Replace(",", "")), Convert.ToString(tbDes.Text)) == false)
解决方案
If you want to convert a string to integer then use TryParse
C# 7 example where result is valid any place in the method Tryparse is used. Know as out variables.
if (int.TryParse(textBox1.Text, out var result)) { // use result } else { // result is not a valid int }Prior to C# 7
int result = 0; if (int.TryParse(textBox1.Text, out result)) { // result is valid } else { // result is not a valid int }
这篇关于传递null时获取格式异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文