Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting value of an Eval from int to string

Tags:

c#

asp.net

eval

I have an integer stored in my database that I need to convert string.

This is my attempt at the Eval:

<%# ChangeSalaryType(Eval("SalaryType")) %>

This is my attempt at the function:

public static string ChangeSalaryType(int salaryType)
{
    string salaryTime = string.Empty;

    if (salaryType == 1)
    {
        salaryTime = "per hour";
    }
    else if (salaryType == 2)
    {
        salaryTime = "per week";
    }
    else if (salaryType == 3)
    {
        salaryTime = "per annum";
    }
    return salaryTime;
}

But I am getting these errors:

Argument 1: cannot convert from 'object' to 'int'   
Error   2   The best overloaded method match for 'controls_allPlacements.ChangeSalaryType(int)' has some invalid arguments   
Error   4   The best overloaded method match for 'controls_allPlacements.ChangeSalaryType(int)' has some invalid arguments

I have used "SalaryType" in the Eval as that is the parameter that has the information from the database in. I'm not completetly sure what I am doing wrong..

like image 697
wilcode Avatar asked Apr 07 '13 15:04

wilcode


2 Answers

Even though you know that the SalaryType field will be an int in your aspx it will be cast as object after the Eval(). Do an explicit cast like so:

<%# ChangeSalaryType(Convert.ToInt32(Eval("SalaryType"))) %>
like image 144
Tahbaza Avatar answered Nov 06 '22 15:11

Tahbaza


Try to convert to int on your Eval part;

Convert.ToInt32(Eval("SalaryType"))

Eval probably return object as a return type.

like image 1
Soner Gönül Avatar answered Nov 06 '22 17:11

Soner Gönül