Convert Eval value from int to string

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

This is my attempt at Eval:

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

This is my attempt to 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 get the following 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 used "SalaryType" in Eval, since this is a parameter that contains information from the database. I'm not completely sure what I'm doing wrong.

+5
source share
3 answers

Even if you know that the field SalaryTypewill be intin your aspx, it will be passed as objectafter Eval (). Make an explicit cast like this:

<%# ChangeSalaryType(Convert.ToInt32(Eval("SalaryType"))) %>
+9
source

Try converting to intyour Eval part;

Convert.ToInt32(Eval("SalaryType"))

Evalprobably returns objectas a return type.

+1

DataItem :

<%#RenderSalaryType(Container.DataItem)%>

and by code

protected string RenderSalaryType(object oItem)
{
    int salaryType = (int)DataBinder.Eval(oItem, "SalaryType");

    // rest of your code
}
0
source

All Articles