代码如下:
long zj;
zj= (tczj + Convert.ToInt64(jl.Text) + Convert.ToInt64(cf.Text));其中 tczj是个Long 变量...现在给出的提示是 未处理 System.FormatException
输入字符串的格式不正确.我现在需要的是计算出他们相加的结果..要如何实现?小弟初学C#,求大虾帮忙~!
long zj;
zj= (tczj + Convert.ToInt64(jl.Text) + Convert.ToInt64(cf.Text));其中 tczj是个Long 变量...现在给出的提示是 未处理 System.FormatException
输入字符串的格式不正确.我现在需要的是计算出他们相加的结果..要如何实现?小弟初学C#,求大虾帮忙~!
格式对吗?
外面用try一下,截获输入非法问题;
long tczj;
private void dataGrid2_Click(object sender, EventArgs e)
{
if (dataGrid2.VisibleRowCount > 0)
{
ygh2 = dataGrid2[dataGrid2.CurrentCell.RowNumber, 0].ToString();
SqlConnection mysql = new SqlConnection("user id=sa;password=;initial catalog=test;Data Source=127.0.0.1;Connect Timeout=30"); SqlCommand mycmd = new SqlCommand("select ygh,xm,ygxxb.tcm,jbgz,dhbt,jtbt,ylbt,zfbt,zj from ygxxb,gztcb where ygxxb.tcm=gztcb.tcm and ygh='" + ygh2 + "'", mysql);
mysql.Open();
SqlDataReader adp;
adp = mycmd.ExecuteReader();
if (adp.Read())
{
ygh.Text = adp["ygh"].ToString();
xm.Text = adp["xm"].ToString();
tcm.Text = adp["tcm"].ToString();
jbgz.Text = adp["jbgz"].ToString();
dhbt.Text = adp["dhbt"].ToString();
jtbt.Text = adp["jtbt"].ToString();
ylbt.Text = adp["ylbt"].ToString();
zfbt.Text = adp["zfbt"].ToString();
tczj = Convert.ToInt64(adp["zj"].ToString());
adp.Close();
}
mysql.Close(); }
} private void insert_Click(object sender, EventArgs e)
{
if (ygh.Text != "")
{
mystr.sql = "select * from gzffmxb where ygh='"+ygh.Text+"' and jsny='"+System.DateTime.Now.Month+"'";
if (mystr.sqlzx(mystr.sql) == false)
{
long zj;
zj= (Convert.ToInt64(tczj) + Convert.ToInt64(jl.Text) + Convert.ToInt64(cf.Text));
if (MessageBox.Show("员工[" + xm.Text + "]的实发金额为" + zj + ".0元!\n确定发送吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{ }
}
else MessageBox.Show("该员工工资已结算,不要重复操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else MessageBox.Show("请先选择员工,再继续操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}求大虾...帮帮看下.
zj= Convert.Double(Convert.ToInt64(tczj) + Convert.ToInt64(jl.Text) + Convert.ToInt64(cf.Text));上述方法应该对吧
好像也可以先定义一个类型转换的函数,然后调用
long zj;
zj = (Convert.ToInt64(jbgz.Text) + Convert.ToInt64(dhbt.Text) + Convert.ToInt64(jtbt.Text) + Convert.ToInt64(ylbt.Text) + Convert.ToInt64(zfbt.Text));这个执行 没有问题...
原来是jl.Text和cf.Text为空时会提示错误.
但是要如何解决呢?
zj= (tczj + Convert.ToInt64(jl.Text==""?0:jl.Text) + Convert.ToInt64(cf.Text==""?0:cf.Text));
恩..我也是这样的逻辑的实现了功能..不过我的方法笨了点.