添加两个标签的值

时间:2015-06-18 13:56:00

标签: c#

我想让这段代码工作。我不明白为什么它不起作用。有人可以看看我在这里做错了吗?

错误消息是

  

“输入字符串格式不正确”

sda = new SqlDataAdapter(@"SELECT salesT, SUM(CONVERT(DECIMAL(8,2),[salesA])) AS [A],
                           SUM(CONVERT(DECIMAL(8,2),[salesB])) AS [B], 
                           FROM details WHERE method= 'cont'  GROUP BY salesT", con);
DataSet ds = new DataSet();
sda.Fill(ds);
DataRow dr = ds.Tables[0].Select("salesT= '01062015'").FirstOrDefault();

if (dr != null)
{
    label31.Text = dr["A"].ToString();
    label30.Text = dr["B"].ToString();
}

sda1 = new SqlDataAdapter(@"SELECT salesT, SUM(CONVERT(DECIMAL(8,2),[salesA])) AS [A], 
                            SUM(CONVERT(DECIMAL(8,2),[salesB])) AS [B], 
                            FROM details WHERE method= 'cred'  GROUP BY salesT", con);
DataSet ds1 = new DataSet();
sda1.Fill(ds1);
DataRow dr1 = ds1.Tables[0].Select("salesT= '01062015'").FirstOrDefault();

if (dr1 != null)
{
    label22.Text = dr1["A"].ToString();
    label21.Text = dr1["B"].ToString();                            
}

int iV1 = Convert.ToInt32(label31.Text); 
int iV2 = Convert.ToInt32(label22.Text);
int iV3 = iV1 + iV2;
label40.Text = iV3.ToString();

2 个答案:

答案 0 :(得分:0)

确认您的标签不为空或空:

sda = new SqlDataAdapter(@"SELECT salesT, SUM(CONVERT(DECIMAL(8,2),[salesA])) AS [A],SUM(CONVERT(DECIMAL(8,2),[salesB])) AS [B], 
                                   FROM details WHERE method= 'cont'  GROUP BY salesT", con);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    DataRow dr = ds.Tables[0].Select("salesT= '01062015'").FirstOrDefault();
    if (dr != null)
    {
        label31.Text = dr["A"].ToString();
        label30.Text = dr["B"].ToString();
    }
    sda1 = new SqlDataAdapter(@"SELECT salesT, SUM(CONVERT(DECIMAL(8,2),[salesA])) AS [A], SUM(CONVERT(DECIMAL(8,2),[salesB])) AS [B], 
                                   FROM details WHERE method= 'cred'  GROUP BY salesT", con);
    DataSet ds1 = new DataSet();
    sda1.Fill(ds1);
    DataRow dr1 = ds1.Tables[0].Select("salesT= '01062015'").FirstOrDefault();
    if (dr1 != null)
    {
        label22.Text = dr1["A"].ToString();
        label21.Text = dr1["B"].ToString();                            
    }
    //You declare A as Decimal, so Integer doesn't work for you
    double iV1 = 0, iV2 = 0;        
    double.TryParse(label31.Text, out iV1); 
    double.TryParse(label22.Text, out iV2);
    double iV3 = iV1 + iV2;

    //Or decimal value type
    //decimal iV1, iV2;        
    //decimal.TryParse(label31.Text, out iV1); 
    //decimal.TryParse(label22.Text, out iV2);
    //decimal iV3 = iV1 + iV2;
    label40.Text = iV3.ToString();

答案 1 :(得分:0)

  

It breaks at this point int iV1 = Convert.ToInt32(label31.Text); The value is 408.34

显然,408.34不是有效整数。这就是你获得FormatException的原因。

你得到的最近的东西是double。将其IFormatProvider解析为加倍,其.NumberDecimalSeparator InvariantCulture

double iV1 = double.Parse(label31.Text, CultureInfo.InvariantCulture);

这会使您的iV1成为408.34。您可以将此值与iV2一起用于分配标签文字。

如果你想截断if的小数部分,就这样做吧;

int i = (int)iV1; // i will be 408