Question

I am creating a DB based WebSite application on Asp.NET in C#. I have an error stated Input string was not in a correct format.on my browser. Lines shown is:

Line 162:        protected string ConvertStatus(object status)
Line 163:        {
Line 164:            int y = Convert.ToInt32(status);
Line 165:
Line 166:            if (y == 1)

My related code-behind:

protected string ConvertStatus(object status)
{
   int y = Convert.ToInt32(status);


    if (y == 1)
    {
        return "Enabled";
    }
    else
    {
        return "Disabled";
    }
}

And this is the label in design page:

<asp:TemplateField HeaderText="Status" SortExpression="Status">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# ConvertStatus(Eval("Status")) %>'>  </asp:Label>
</ItemTemplate>
</asp:TemplateField>

How i can solve this issue? Thank you.

Was it helpful?

Solution

Use TryParse method like this

int y;
int.TryParse(status,out y);

TryParse method is return bool that indicate if input is convertible to integer or not. If it is then it will convert into int and assign it to y variable. If input can not be convert to int then value of y will be 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top