Cannot convert from Object to Int

Zooch84

Have the following code, designed to change the text value of the field based on the Int present (i.e. if the Int is 1 then display "Big Cheese" instead.

Gives the following Errors:

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

.aspx Page Code:

<ItemTemplate>
    <asp:Label runat="server" Text='<%#FieldDisplay(Eval("pizza_id")) %>'>
    </asp:Label>
</ItemTemplate>

Code Behind

protected string FieldDisplay(int pizza_id)
    {
        string rtn = "DefaultValue";

            if (pizza_id == 1)
            {
                rtn = "Big Cheese";
            }
            else if (pizza_id == 2)
            {
             rtn = "BBQ Beef";
            }
            else if (pizza_id == 3)
            {
                rtn = "Chicken and Pineapple";
            }
            else if (pizza_id == 4)
            {
                rtn = "Pepperoni Feast";
            }
            else if (pizza_id == 5)
            {
                rtn = "Vegetarian";
            }
        return rtn;
        }

Keep getting error Object cannot be converted to Int . I'm not sure where it is getting this from as the "pizza_id" field in the DB is set to INT....do I need to do some kind of parse somewhere?

Pawan

You need to change your method a little bit as below:

protected string FieldDisplay(object pizza_id)
{
   string rtn = "DefaultValue";
   int pizzaID=0;
   if(int.TryParse(Convert.ToString(pizza_id), out pizzaID))
    {
        if (pizzaID== 1)
        {
            rtn = "Big Cheese";
        }
        else if (pizzaID== 2)
        {
         rtn = "BBQ Beef";
        }
        else if (pizzaID== 3)
        {
            rtn = "Chicken and Pineapple";
        }
        else if (pizzaID== 4)
        {
            rtn = "Pepperoni Feast";
        }
        else if (pizzaID== 5)
        {
            rtn = "Vegetarian";
        }
     }
   return rtn;
}

I recommend you to use switch instead if else ladder.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot convert from int to int**

From Dev

Error: Argument 1: cannot convert from 'int?[]' to 'object[]'

From Dev

cannot convert from boolean to int

From Dev

cannot convert from int to byte

From Dev

how to convert from Object[] to int[]

From Dev

cannot convert from 'object' to 'string'

From Dev

Cannot convert from object to string

From Dev

Type mismatch: cannot convert from boolean to int

From Dev

cannot convert from 'AnonymousType#1' to 'int'

From Dev

Type mismatch: cannot convert from long to int

From Dev

Cannot convert from 'unsigned int *' to 'LPDWORD'

From Dev

'=' : cannot convert from 'CircularDoubleDirectedList<int>::Node *' to 'Node *'

From Dev

Cannot convert from 'int' to 'System.Predicate

From Dev

Cannot convert from '_int64' to 'Data *'

From Dev

Cannot convert from int to boolean in java

From Dev

Cannot Convert From Method Group to Func<int>

From Dev

Error Cannot convert from int to boolean

From Dev

Type mismatch: cannot convert from int to TextView

From Dev

java : cannot convert from String to int

From Dev

Type mismatch: cannot convert from void to int

From Dev

cannot convert from 'AnonymousType#1' to 'int'

From Dev

'=' : cannot convert from 'CircularDoubleDirectedList<int>::Node *' to 'Node *'

From Dev

Cannot convert from int to boolean in java

From Dev

cannot convert from 'ref object' to 'tagVARIANT*'

From Dev

Cannot convert from object to System.ValueType

From Dev

cannot convert argument 1 from int to int && error

From Dev

C++ cannot convert argument 1 from 'int**' to 'const int **'

From Dev

decltype error C2440 cannot convert from 'int *' to 'int *&'

From Dev

Weird compiler error: Cannot convert parameter from 'int' to 'int &&'

Related Related

HotTag

Archive