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?
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.
Comments