将两个标签的值相加
本文关键字:标签 两个 | 更新日期: 2023-09-27 17:56:52
我正在尝试让这段代码工作。我不明白为什么它不起作用。有人可以看看我在这里做错了什么吗?
错误消息是
"输入字符串格式不正确"
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();
验证您的标签不是空还是空:
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();
此时它中断 int iV1 = Convert.ToInt32(label31.文本);这 值为 408.34
显然,408.34
不是一个有效的整数。这就是为什么你会得到FormatException
.
你得到的最接近的东西是作为一个double
。将其解析为使用具有.
NumberDecimalSeparator
的IFormatProvider
加倍,例如 InvariantCulture
.
double iV1 = double.Parse(label31.Text, CultureInfo.InvariantCulture);
这将使您的iV1
成为408.34
。您可以将此值与分配给标签文本的iV2
一起使用。
如果你想截断if的小数部分,就这样做;
int i = (int)iV1; // i will be 408