473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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.}"


Expand|Select|Wrap|Line Numbers
  1.   protected void bsin_Click(object sender, EventArgs e)
  2.     {
  3.         con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  4.         com = new SqlCommand("Select *From register Where loginname=@loginname And password=@password", con);
  5.         com.Parameters.Add("@loginname", SqlDbType.NVarChar).Value = txtun.Text;
  6.         com.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtpass.Text;
  7.         try
  8.         {
  9.             if (con.State == ConnectionState.Closed)
  10.                 con.Open();
  11.            dr = com.ExecuteReader();
  12.             if (dr.Read())
  13.             {
  14.                 Session["username"] = txtun.Text;
  15.                 Session["password"]=txtpass.Text;
  16.                 lplwronguserid.Visible = false;
  17.                 Response.Redirect("home.aspx");
  18.  
  19.  
  20.             }
  21.             else
  22.             {
  23.                 lplwronguserid.Text = "Wrong Password OR UserID*";
  24.                 //Response.Write("Wrong Password OR UserID");
  25.  
  26.  
  27.             }
  28.         }
  29.         catch (Exception exc)
  30.         {
  31.         }
  32.         finally
  33.         {
  34.             con.Close();
  35.         }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.     }
  42.  
Sep 19 '10 #1
16 12612
Frinavale
9,735 Expert Mod 8TB
You have:
Expand|Select|Wrap|Line Numbers
  1.  Select *From register Where loginname=@loginname And password=@password
But you need a space in between the * and the From...like this:
Expand|Select|Wrap|Line Numbers
  1. Select * From register Where loginname=@loginname And password=@password
-Frinny
Sep 20 '10 #2
I have changed that as well but still it is showing the same problem
Sep 20 '10 #3
ck9663
2,878 Expert 2GB
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
Sep 20 '10 #4
Frinavale
9,735 Expert Mod 8TB
It's C#, the variables are declared though...they are being assigned values from TextBoxes on the page here:
Expand|Select|Wrap|Line Numbers
  1.   com.Parameters.Add("@loginname", SqlDbType.NVarChar).Value = txtun.Text;
  2.   com.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtpass.Text;
Sep 20 '10 #5
hype261
207 100+
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.Redirect statement at line 17. Here is an MSDN article on the exception and how you can fix it.

http://support.microsoft.com/kb/312629/EN-US/
Sep 20 '10 #6
Frinavale
9,735 Expert Mod 8TB
I would never have guessed that it was a Response.Redirect issue (I myself have never experienced this particular error...usually the Response.Redirect("url") method throws "thread abortion error" instead of the error described)

But...change line 17 in the above code from this:
Expand|Select|Wrap|Line Numbers
  1. Response.Redirect("home.aspx");
To this:
Expand|Select|Wrap|Line Numbers
  1. Response.Redirect("home.aspx",true);

Providing "true" as the second parameter to the Response.Redirect() 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.Redirect away from the page!

It might be best to use a boolean to flag whether or not to Response.Redirect the user...like this:
Expand|Select|Wrap|Line Numbers
  1. protected void bsin_Click(object sender, EventArgs e)
  2. {   boolean redirectUser = false;
  3.         con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  4.         com = new SqlCommand("Select *From register Where loginname=@loginname And password=@password", con);
  5.         com.Parameters.Add("@loginname", SqlDbType.NVarChar).Value = txtun.Text;
  6.         com.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtpass.Text;
  7.         try
  8.         {
  9.             if (con.State == ConnectionState.Closed)
  10.                 con.Open();
  11.             dr = com.ExecuteReader();
  12.             if (dr.Read())
  13.             {
  14.                 Session["username"] = txtun.Text;
  15.                 Session["password"] = txtpass.Text;
  16.                 lplwronguserid.Visible = false;
  17.                 redirectUser = true;
  18.             }
  19.             else
  20.             {
  21.                 lplwronguserid.Text = "Wrong Password OR UserID*";
  22.  
  23.             }
  24.         }
  25.         catch (Exception exc)
  26.         {
  27.         }
  28.         finally
  29.         {
  30.             con.Close();
  31.         }
  32.         if(redirectUser)
  33.         {
  34.            Response.Redirect("home.aspx",true);
  35.         }
  36. }
-Frinny
Sep 20 '10 #7
thanks frinny ,
Now it is redirecting but exception is still there
Sep 21 '10 #8
hype261
207 100+
Jagdeep,

What line of code is the exception being thrown at?
Sep 21 '10 #9
Frinavale
9,735 Expert Mod 8TB
Where is it being thrown?

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

-Frinny
Sep 21 '10 #10
line no 34
n thanks for considering my request
Sep 22 '10 #11
Is this a ThreadAbortException? How are you seeing the exception?


ASP.NET will abort threads to handle control flow. If you do a Response.Redirect it calls Response.End which does the Thread.Abort(). This should not be an issue.
Sep 22 '10 #12
no sir the exception is same as written above by me
Sep 22 '10 #13
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.

http://www.west-wind.com/Weblog/posts/6931.aspx

http://bytes.com/topic/asp-net/answe...ate-expression

http://geekswithblogs.net/vadivelkum.../25/70703.aspx


Here is a KB article describing the issue.
http://support.microsoft.com/kb/312629/EN-US/

Good Luck
Sep 22 '10 #14
Frinavale
9,735 Expert Mod 8TB
jagdeep gupta,

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

-Frinny
Sep 22 '10 #15
Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using System.Data.SqlClient;
  13.  
  14. public partial class loginpage : System.Web.UI.Page
  15. {
  16.     SqlConnection con;
  17.     SqlCommand com;
  18.     SqlDataReader dr;
  19.     protected void Page_Load(object sender, EventArgs e)
  20.     {
  21.      //Response.Write("Welcome" + " " + Session["username"]+ " " + Session["lastname"]);
  22.  
  23.     }
  24.  
  25.     protected void lbnew_Click(object sender, EventArgs e)
  26.     {
  27.         Response.Redirect("registration.aspx");
  28.  
  29.     }
  30.     protected void bsin_Click(object sender, EventArgs e)
  31.     {
  32.         con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  33.         com = new SqlCommand("Select * From register Where loginname=@loginname And password=@password", con);
  34.         com.Parameters.Add("@loginname", SqlDbType.NVarChar).Value = txtun.Text;
  35.         com.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtpass.Text;
  36.         try
  37.         {
  38.             if (con.State == ConnectionState.Closed)
  39.                 con.Open();
  40.            dr = com.ExecuteReader();
  41.             if (dr.Read())
  42.             {
  43.                 Session["username"] = txtun.Text;
  44.                 Session["password"]=txtpass.Text;
  45.                 lplwronguserid.Visible = false;
  46.                 con.Close();
  47.                 Response.Redirect("home.aspx",true);
  48.  
  49.  
  50.             }
  51.             else
  52.             {
  53.                 lplwronguserid.Text = "Wrong Password OR UserID*";
  54.                 //Response.Write("Wrong Password OR UserID");
  55.  
  56.  
  57.             }
  58.         }
  59.         catch (Exception exc)
  60.         {
  61.         }
  62.         finally
  63.         {
  64.             con.Close();
  65.         }
  66.  
  67.  
  68.  
  69. }
  70.  
Sep 23 '10 #16
Frinavale
9,735 Expert Mod 8TB
Jagdeep Gupta,

I mentioned before that you should do your redirect after you close your connection.

I also mentioned that you should use Response.Redirect("url", true) to redirect the user so that you avoid problems with Thread Abortion exceptions.

Like This:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12.  
  13. public partial class loginpage : System.Web.UI.Page
  14. {
  15.     SqlConnection con;
  16.     SqlCommand com;
  17.     SqlDataReader dr;
  18.     protected void Page_Load(object sender, EventArgs e)
  19.     {   }
  20.  
  21.     protected void lbnew_Click(object sender, EventArgs e)
  22.     {
  23.         Response.Redirect("registration.aspx", true);
  24.     }
  25.     protected void bsin_Click(object sender, EventArgs e)
  26.     {
  27.         con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  28.         com = new SqlCommand("Select * From register Where loginname=@loginname And password=@password", con);
  29.         com.Parameters.Add("@loginname", SqlDbType.NVarChar).Value = txtun.Text;
  30.         com.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtpass.Text;
  31.         try
  32.         {
  33.             if (con.State == ConnectionState.Closed)
  34.                 con.Open();
  35.            dr = com.ExecuteReader();
  36.             if (dr.Read())
  37.             {
  38.                 Session["username"] = txtun.Text;
  39.                 Session["password"]=txtpass.Text;
  40.                 lplwronguserid.Visible = false;
  41.                 con.Close();
  42. //I removed the redirect from here
  43.             }
  44.             else
  45.             {
  46.                 lplwronguserid.Text = "Wrong Password OR UserID*";
  47.             }
  48.         }
  49.         catch (Exception exc)
  50.         {
  51.         }
  52.         finally
  53.         {
  54.             con.Close();
  55.         }
  56.  
  57. //I put it HERE...after closing the connection
  58.         if( Session["username"] != null)
  59.         {
  60.           Response.Redirect("home.aspx",true);
  61.         }
  62.   }
  63. }
-Frinny
Sep 23 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Zeng | last post by:
Hello, I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false ( ( 3 + 6 ) / 3 ) > ( ( 5 + 3 ) / 4 ) ...
1
by: mark | last post by:
In the sample code for Programming Microsoft Visual Basic.NET by Francesco Balena (Microsoft Press) there is a program (ExprEvaluator) that evaluates expressions. It has a bug in that when the...
0
by: bazzer | last post by:
hey, i am using visual basic.net 2003 and have an ASP.NET webform application thats accessing a microsoft access 2003 database. i kept getting the following error when i tried to run it: ERROR ...
0
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
2
by: steevehetu18 | last post by:
Hi, I'm doing a algorithm to calcule Earliest Start et Latest Start for a Graph with Nodes and Arcs. (like a PERT diagram) . Unfortunatly, i receive a wierd exception message for a specific...
0
by: Rick Little | last post by:
While stepping through code, I've started getting this message when tying to see the value of most variables. Cannot evaluate expression because a thread is stopped at a point where garbage...
0
by: Ajit Goel | last post by:
Hi; My project file property is set to use "Visual Studio Development Server". I am able to debug and see the corresponding values when I mouseover but as soon as I change to the project file...
4
by: Glenn | last post by:
OK, I've looked up this message but am not finding how to get rid of it: "Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because...
2
by: tbh | last post by:
in an error path in an aspx script under DotNet 2, IIS6, Win2003 Server I get the following error (which I don't understand) on Respone.End(): {Unable to evaluate expression because the code is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.