简体   繁体   English

每次更改组合框值时,如何从数据库中计算数字并将其显示给标签?

[英]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.是一个string22是一个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 :所以更安全的方法是尝试使用decimalTryParse方法:

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.

相关问题 如何从组合框中获取值并将其显示到文本框 - how can i get value from combo box and show it to textbox 如何更改标签以匹配组合框的选定索引? - How do i change a label to match the selected index of a combo box? 如何显示绑定项的组合框的显示值? - How do I show the display value for a combo box for the bound item? 如何在组合框中显示枚举值? - How do i show enum values in a combo-box? 如何根据组合框中的选定值从数据库检索数据? - How to retrieve data from database based on selected value in combo box? 如何将我的标签值更改为随机数? - How do I change my label value to the random number? 如何从组合框中选择一个值以在 c# 中显示关联的值 - How do I select a value from a combo box to display an associated value in c# 如何使用组合框更改排序顺序? - How do I use a combo-box to change the sort order? 如何使用访问数据库中的数据填充组合框并使用其筛选数据网格? - How can i populate a combo box with data from an access database and use it to filter a datagrid? 选择路径名称时,如何从文本框中的组合框中获取选定的数据库名称 - How do I get selected database name from Combo box in a text box when I choose the Path name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM