代码如下:
 long zj;
 zj= (tczj + Convert.ToInt64(jl.Text) + Convert.ToInt64(cf.Text));其中 tczj是个Long 变量...现在给出的提示是 未处理 System.FormatException
 输入字符串的格式不正确.我现在需要的是计算出他们相加的结果..要如何实现?小弟初学C#,求大虾帮忙~!

解决方案 »

  1.   

    zj= Convert.Double(Convert.ToInt64(tczj) + Convert.ToInt64(jl.Text) + Convert.ToInt64(cf.Text));
      

  2.   

    jl.Text cf.Text
    格式对吗?
      

  3.   

    可以换一种方法,用zj=tczj+long.Parse(jl.Text)+long.Parse(cf.Text);
    外面用try一下,截获输入非法问题;
      

  4.   

    还是不行..提示 输入字符串的格式不正确。完整代码如下:
            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);
        
            }求大虾...帮帮看下.
      

  5.   

    dinoLgrace(淡泊以明志,宁静而致远) ( ) 信誉:100    Blog   加为好友  2007-05-10 15:09:58  得分: 0  
     
     
       zj= Convert.Double(Convert.ToInt64(tczj) + Convert.ToInt64(jl.Text) + Convert.ToInt64(cf.Text));上述方法应该对吧
    好像也可以先定义一个类型转换的函数,然后调用
      
     
      

  6.   

    之前的代码里也用到过类型的转换:
    long zj;
    zj = (Convert.ToInt64(jbgz.Text) + Convert.ToInt64(dhbt.Text) + Convert.ToInt64(jtbt.Text) + Convert.ToInt64(ylbt.Text) + Convert.ToInt64(zfbt.Text));这个执行 没有问题...
      

  7.   

    把执行zj= (tczj + Convert.ToInt64(jl.Text) + Convert.ToInt64(cf.Text));前jl.Text cf.Text的值贴出来
      

  8.   

    上面的问题已经解决了.
        原来是jl.Text和cf.Text为空时会提示错误.
    但是要如何解决呢?
      

  9.   

    zj= (tczj + Convert.ToInt64(jl.Text==""?0:jl.Text) + Convert.ToInt64(cf.Text==""?0:cf.Text));
      

  10.   

    数据库设置为空了的...好像用类型转换的时候..不支持NULL.我刚刚初学的.不太清楚.
      

  11.   

    hdt(倦怠) ( ) 信誉:120    Blog   加为好友  2007-05-10 16:06:12  得分: 0  
     
     
       zj= (tczj + Convert.ToInt64(jl.Text==""?0:jl.Text) + Convert.ToInt64(cf.Text==""?0:cf.Text));  
     
    恩..我也是这样的逻辑的实现了功能..不过我的方法笨了点.