Abhijeet
Abhijeet

Reputation: 13856

Response.Redirect Inside Try/Catch Block

While trying to convert strting into enum in Asp.NET webApplication.

Code -

enum MyEnum
{
    field1,
    field2,
    field3
}

string strField1 = "field1";
MyEnum parsedEnum = (MyEnum)Enum.Parse(typeof(MyEnum), strField1);

I encounter following error -

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

What am I missing.

EDIT: Updated Code:

I have been using this enum to redirect user to other page, by validating CommandArgument of the button. I obtain this error while debugging the solution, other-wise the code works fine.

<form id="form1" runat="server">
    <div>
        <asp:Button Text="Redirect" ID="btnRedirect" OnClick="btnRedirect_Click" CommandName="field1" runat="server" />
    </div>
    </form>



    protected void btnRedirect_Click(object sender, EventArgs e)
    {            
        var btn = sender as Button;
        var cmdName = btn.CommandName; //field1
        MyEnum parsedEnum = (MyEnum)Enum.Parse(typeof(MyEnum), cmdName);

        try
        {
            switch (parsedEnum)
            {
                case MyEnum.field1:
                    Response.Redirect("WebForm1.aspx");
                    break;
                case MyEnum.field2:
                    Response.Redirect("WebForm2.aspx");
                    break;
                case MyEnum.field3:
                    Response.Redirect("WebForm3.aspx");
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var err = ex.Message;                
        }
    }

Upvotes: 1

Views: 1882

Answers (2)

Abhijeet
Abhijeet

Reputation: 13856

Issue lied in trying to redirect to other page, inside try catch block.

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

Above exception had been caused, inside catch block, the current page thread was being aborted.

Upvotes: 0

Joey Gennari
Joey Gennari

Reputation: 2361

All references to that error message point to adding a second, false parameter to Request.Redirect. Is the enum code redirecting the user?

Upvotes: 1

Related Questions