How can I compute the salary per hour of a person from the database?
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();
Operator "/" cannot be applied to operands of type "string" and "int"
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.