Error: Unable to evaluate expression because the code is optimized . . .

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jagdeep gupta
    New Member
    • Aug 2010
    • 98

    Error: Unable to evaluate expression because the code is optimized . . .

    The code was executing fine but now an exception is generated: "exc = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}"


    Code:
      protected void bsin_Click(object sender, EventArgs e)
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            com = new SqlCommand("Select *From register Where loginname=@loginname And password=@password", con);
            com.Parameters.Add("@loginname", SqlDbType.NVarChar).Value = txtun.Text;
            com.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtpass.Text;
            try
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
               dr = com.ExecuteReader();
                if (dr.Read())
                {
                    Session["username"] = txtun.Text;
                    Session["password"]=txtpass.Text;
                    lplwronguserid.Visible = false;
                    Response.Redirect("home.aspx");
    
    
                }
                else
                {
                    lplwronguserid.Text = "Wrong Password OR UserID*";
                    //Response.Write("Wrong Password OR UserID");
    
    
                }
            }
            catch (Exception exc)
            {
            }
            finally
            {
                con.Close();
            }
    
    
    
    
    
        }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You have:
    Code:
     Select *From register Where loginname=@loginname And password=@password
    But you need a space in between the * and the From...like this:
    Code:
    Select * From register Where loginname=@loginname And password=@password
    -Frinny

    Comment

    • jagdeep gupta
      New Member
      • Aug 2010
      • 98

      #3
      I have changed that as well but still it is showing the same problem

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        This looks like a java/C/C+/C# code. In SQL Server, a "@" sign means it's a variable. In this case those variables are undeclared. That could be the reason for your error. As to some programming languages, it may be unassigned, but it can not be undeclared.

        Good Luck!!!

        ~~ CK

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          It's C#, the variables are declared though...they are being assigned values from TextBoxes on the page here:
          Code:
            com.Parameters.Add("@loginname", SqlDbType.NVarChar).Value = txtun.Text;
            com.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtpass.Text;

          Comment

          • hype261
            New Member
            • Apr 2010
            • 207

            #6
            It is often helpful if you tell people which statement is throwing the exception. From looking on google it appears that this isn't a SQL Server problem at all.

            I believe the exception is being thrown at Response.Redire ct statement at line 17. Here is an MSDN article on the exception and how you can fix it.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              I would never have guessed that it was a Response.Redire ct issue (I myself have never experienced this particular error...usually the Response.Redire ct("url") method throws "thread abortion error" instead of the error described)

              But...change line 17 in the above code from this:
              Code:
              Response.Redirect("home.aspx");
              To this:
              Code:
              Response.Redirect("home.aspx",true);

              Providing "true" as the second parameter to the Response.Redire ct() method will prevent the rest of the page from being executed. This will avoid "thread abortion" errors.


              Be Sure to Close your connection Before you Response.Redire ct away from the page!

              It might be best to use a boolean to flag whether or not to Response.Redire ct the user...like this:
              Code:
              protected void bsin_Click(object sender, EventArgs e)
              {   boolean redirectUser = false;
                      con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                      com = new SqlCommand("Select *From register Where loginname=@loginname And password=@password", con);
                      com.Parameters.Add("@loginname", SqlDbType.NVarChar).Value = txtun.Text;
                      com.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtpass.Text;
                      try
                      {
                          if (con.State == ConnectionState.Closed)
                              con.Open();
                          dr = com.ExecuteReader();
                          if (dr.Read())
                          {
                              Session["username"] = txtun.Text;
                              Session["password"] = txtpass.Text;
                              lplwronguserid.Visible = false;
                              redirectUser = true;
                          }
                          else
                          {
                              lplwronguserid.Text = "Wrong Password OR UserID*";
              
                          }
                      }
                      catch (Exception exc)
                      {
                      }
                      finally
                      {
                          con.Close();
                      }
                      if(redirectUser)
                      {
                         Response.Redirect("home.aspx",true);
                      }
              }
              -Frinny
              Last edited by Frinavale; Sep 21 '10, 04:56 PM.

              Comment

              • jagdeep gupta
                New Member
                • Aug 2010
                • 98

                #8
                thanks frinny ,
                Now it is redirecting but exception is still there

                Comment

                • hype261
                  New Member
                  • Apr 2010
                  • 207

                  #9
                  Jagdeep,

                  What line of code is the exception being thrown at?

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Where is it being thrown?

                    You have a Try-Catch block that catches all Exceptions and does nothing with them....

                    -Frinny

                    Comment

                    • jagdeep gupta
                      New Member
                      • Aug 2010
                      • 98

                      #11
                      line no 34
                      n thanks for considering my request

                      Comment

                      • ZiadElmalki
                        New Member
                        • Sep 2010
                        • 43

                        #12
                        Is this a ThreadAbortExce ption? How are you seeing the exception?


                        ASP.NET will abort threads to handle control flow. If you do a Response.Redire ct it calls Response.End which does the Thread.Abort(). This should not be an issue.

                        Comment

                        • jagdeep gupta
                          New Member
                          • Aug 2010
                          • 98

                          #13
                          no sir the exception is same as written above by me

                          Comment

                          • ZiadElmalki
                            New Member
                            • Sep 2010
                            • 43

                            #14
                            exc = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}"


                            That is not an exception. Thats an error message from the debugger.

                            The issue is due to the thread being aborted. Here are some posts fro people with the same issue.








                            Here is a KB article describing the issue.


                            Good Luck

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              jagdeep gupta,

                              Could you please post your updated code so that we can see what is going on?

                              -Frinny

                              Comment

                              Working...