[英]How could I compute the number from the database and show it to the Label everytime I change the combo box value?
I have a database values that I have to show to the label as the person salay per hour.我有一个数据库值,我必须向标签显示每小时工资。 My code is here:
我的代码在这里:
sqc = con.CreateCommand();
string query5 = "SELECT [SalaryGrade] FROM tbl_gradestep where GradeNumber =" + cmbGradeNumber.SelectedItem.ToString() + " and StepNumber =" + cmbStepNumber.SelectedItem.ToString() + "";
sqc.CommandText = query5;
sda = new SqlDataAdapter(sqc);
dt = new DataSet();
sda.Fill(dt);
if(dt.Tables[0].Rows.Count > 0)
{
lblSalary.Text = dt.Tables[0].Rows[0]["SalaryGrade"].ToString();
lblHour.Text = (*dt.Tables[0].Rows[0]["SalaryGrade"].ToString()/22/8);
}
con.Close();
The computation is database values divided by 22 then divide it again by 8.计算是数据库值除以 22,然后再除以 8。
the error is "Operator "/" cannot be applied to operands of type "string" and "int"
错误是“运算符“/”不能应用于“字符串”和“整数”类型的操作数
The error message is pretty informative.错误消息非常有用。 The following
下列
dt.Tables[0].Rows[0]["SalaryGrade"].ToString()
is a string
and 22
is an int
literal.是一个
string
而22
是一个int
字面量。 You can't apply the division operator to them.您不能对它们应用除法运算符。 How could you divide a
string
with an int
, it is a meaningless operation.你怎么能用一个
int
分割一个string
,这是一个毫无意义的操作。 That you need is something like the following:您需要的是以下内容:
((decimal)dt.Tables[0].Rows[0]["SalaryGrade"])/(22*8)
First you cast the SalaryGrade
to a decimal and then you divide this with the hours.首先,您将
SalaryGrade
转换为小数,然后将其除以小时数。
Note : In the above line I have assumed that SalaryGrade
can be casted to a decimal
.注意:在上面的行中,我假设
SalaryGrade
可以转换为decimal
。 If the cast is not possible an exception would be thrown.如果无法进行强制转换,则会抛出异常。 So a more safe approach it would be to try to use the
TryParse
method of decimal
:所以更安全的方法是尝试使用
decimal
的TryParse
方法:
decimal salaryGrade;
if(decimal.TryParse(dt.Tables[0].Rows[0]["SalaryGrade"], out salaryGrade))
{
var salaryPerHour = salaryGrade/(22*8);
}
This is, quite explicitly, a string:这是一个非常明确的字符串:
dt.Tables[0].Rows[0]["SalaryGrade"].ToString()
A string is just text.字符串只是文本。 You don't perform math on it.
你不会对它进行数学运算。 If this string is guaranteed to be an integer, you can convert it directly:
如果这个字符串保证是整数,可以直接转换:
lblHour.Text = (int.Parse(dt.Tables[0].Rows[0]["SalaryGrade"])/22/8);
If it's not guaranteed , then you might want some error checking first:如果不能保证,那么您可能需要先进行一些错误检查:
int salaryGrade;
if (!int.TryParse(dt.Tables[0].Rows[0]["SalaryGrade"], out salaryGrade))
{
// wasn't an integer, handle the error condition here
}
lblHour.Text = (salaryGrade/22/8);
Note: Integer division is going to result only in an integer.注意:整数除法只会产生一个整数。 If you're looking for decimal values, you're going to want to convert your numbers to something with decimal precision.
如果您正在寻找十进制值,您将希望将您的数字转换为具有十进制精度的数字。 (Such as
decimal
, which also has .Parse()
and .TryParse()
methods of course.) Otherwise you may just end up with zeroes and not know why. (例如
decimal
,当然也有.Parse()
和.TryParse()
方法。)否则你可能会以零结束而不知道为什么。
The error occurs because you cannot use a math operation in a string!发生错误是因为您不能在字符串中使用数学运算! Try this: declare an int variable and assign to it the data from the table and then use the int in the "label.Text" assign converting that int to string.
试试这个:声明一个 int 变量并将表中的数据分配给它,然后使用“label.Text”中的 int 分配将该 int 转换为字符串。 Hope helped ya!
希望对你有帮助!
Thank you guys for helping me out here.谢谢你们在这里帮助我。 I read all your answers then modify my code and boom.
我阅读了您的所有答案,然后修改了我的代码并繁荣。 Works like a charm.
奇迹般有效。
decimal salaryGrade;
sqc = con.CreateCommand();
string query5 = "SELECT [SalaryGrade] FROM tbl_gradestep where GradeNumber =" + cmbGradeNumber.SelectedItem.ToString() + " and StepNumber =" + cmbStepNumber.SelectedItem.ToString() + "";
sqc.CommandText = query5;
sda = new SqlDataAdapter(sqc);
dt = new DataSet();
sda.Fill(dt);
if(dt.Tables[0].Rows.Count > 0)
{
lblSalary.Text = dt.Tables[0].Rows[0]["SalaryGrade"].ToString();
if (decimal.TryParse(dt.Tables[0].Rows[0]["SalaryGrade"].ToString(), out salaryGrade))
{
}
lblHour.Text = Convert.ToDecimal(salaryGrade / 22 / 8).ToString("0.00");
}
con.Close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.