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

Here we will see how to solve one of the most common runtime error ““Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.””

This error may occur in the following scenario’s

1) Response.End
2) Response.Redirect
3) Server.Transfer

Reason for this exception occurrence is because page execution is stopped and sent to the Application_EndRequest event in the application’s event pipeline.

When we call Response.End method it stops the page execution and raises the EndRequest event and thus the line of code after Response.End is not executed. Same thing happens when we use Response.Redirect and Server.Transfer methods because both they both call Response.End internally.

When a call is made to destroy a thread, the CLR internally throws a ThreadAbortException. This is a exception that can be caught and handled explicitly.

Use a try-catch statement to catch the exception as below

 try  
 {  
 }  
 catch (System.Threading.ThreadAbortException)  
 {  
     //do nothing  
 }  

While using Response.Redirect we can use an overload of the same

 Response.Redirect(String url, bool endResponse);  

It passes false for endResponse parameter to suppress the internal call to Response.End

Use Server.Execute instead of Server.Transfer as the execution of code happens on the new page.

Advertisements

Page.IsValid And Page.Validate in Asp.net

Here we will see the use of Page.IsValid and Page.Validate in Asp.net.

Suppose we have a login form with two text box(username and password) and a submit button and we have to validate the user input before submitting to server so that only valid data goes to server.

We can achieve this by applying client side validations but always remember that the client side validations are not secure as a hacker can disable the javascript and bypass our validations. Here is where the Page.Validate() method and Page.IsValid property comes into play.

The Page.Validate() method is fired automatically by controls that have the CausesValidation property set to true. Note that the Button control’s CausesValidation property is true by default.

The code is as follows: Continue reading Page.IsValid And Page.Validate in Asp.net

Difference Between DataSet And DataTable In Asp.net

Here we will see some differences between dataset and datatable.

1) A DataTable is an in-memory representation of a single database table which has collection of rows and columns whereas a DataSet is an in-memory representation of a database-like structure which has collection of DataTables. Whenever you want to fetch data from database, it connects indirectly to the database and create a virtual database in local system and then disconnected from database.
OR
A DataTable is an in-memory representation of a single database table. You can think of it as having columns and rows in the same way. A dataset is an in-memory representation of a database-like structure. It can have one or more DataTables and define relations between them, key or any fields etc.

2) DataTable object is lighter than DataSet object since it contains data from single table whereas DataSet is heavier object that can contain data from multiple tables.

3) DataTable fetches only one TableRow at a time whereas DataSet can fetch multiple TableRows at a time

4) As DataTable is a single database table, so there is no DataRelation object in it whereas In DataSet, DataTable objects can be related to each other with DataRelation objects.

5) In DataTable, there is no Unique Constraint and Foreign Key Constraint objects available
But In DataSet, data integrity is enforced by using the Unique Constraint and Foreign Key Constraint objects.

6) In DataTable, DataSource cannot be serialized. But DataSet is serialized DataSource .That is why web services can always returns DataSet as the result but not the DataTables.

7) We can load a single database table into a DataTable and manipulate the data in memory. If we are going to fetch data from a single database table then DataTable is better choice. While DataSet on the other hand can define DataRelations – which define the relationship between DataTables, much like a foreign key relationship can be set up between tables in a database. DataSets, themselves DOES NOT contain any Data. DataSets contain DataTables (which is where any data actually resides), DataRelations, etc, but no data.

8) Dataset is a collection of tables, which is used in disconnected architecture. Generally to fill DataSet we use fill method of SqlDataAdapter. It can be used for manipulating the data remotely and finally updating the database with the modified data. This way it enables disconnected means of working with data. This improves performance in terms of reducing the number of times a database is accessed for data manipulations.

DataTable Example:
SqlDataAdapter adp = new SqlDataAdapter(“select * from SampleTable”, con);
DataTable dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

DataSet Example:
SqlDataAdapter adp= new SqlDataAdapter(“select * from SampleTable”, con);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

Original Source : http://www.webcodeexpert.com/2013/03/difference-between-dataset-and.html

Populate Dropdownlist With Time With Time Interval Dynamically In Asp.net.

Here we will see how to populate your dropdownlist with a time interval of 30 mins dynamically. For this we use TimeSpan class. We can also provide the time interval in hours, minutes and seconds.

Code is as follows: Continue reading Populate Dropdownlist With Time With Time Interval Dynamically In Asp.net.

Set Default Button For Enter Key Press In Asp.net

Here we will see how to set a button as the default button for enter key press. For this you have use Panel. For this example we have created a simple page where you will find two textbox and two button out of which the submit button has been set as the default button for the enter key. We have done this by setting the DefaultButton property of panel.

Code is as follows: Continue reading Set Default Button For Enter Key Press In Asp.net

Calling Asp.net C# Method (Web Method) Using JavaScript.

Here we will see how to avoid annoying post-backs on ASP.NET Web Pages. For this example we have created a simple page where you will be able to check the username availability. For this we need to take the advantage of PageMethod and to setup PageMethod we need instance of Script Manager on web page.

PageMethod an easier and faster approach for ASP.NET AJAX.PageMethod is a way through which we can expose server side page’s method in JavaScript. This brings so many opportunities, we can perform lots of operations without using slow and annoying post backs.

Code is as follows: Continue reading Calling Asp.net C# Method (Web Method) Using JavaScript.

Get Desired Html Color By Using System.Drawing.Color In Asp.net.

Here we will see how to get the html color in back end through the System.Drawing.Color Class.

From the bellow code you can get the Html Color by either its name or color code.

Code is as follows: Continue reading Get Desired Html Color By Using System.Drawing.Color In Asp.net.