Session values lost when any exception occurs

Challenge

If you’ve stored some values in session and your session values getting lost when any exception occurs. Then this article may provide you hint to solve it.

Solution

In my case the reason was as below:
Basically I was creating a directory under my web root directory and when any exception occurred I was deleting that directory. And that was the main reason for losing my session values.
So, Now I am creating/deleting directory outside my web root directory and problem is solved!
And deepest reason is Application Restart – Whenever any directory gets created/deleted under web root ASP.NET restarts the application pool. To read more about ASP.NET Application restart read my blog post.
Happy Coding! 🙂

directory

 

Could not load type 'System.Web.UI.ScriptReferenceBase' from assembly 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

Challenge

If you are getting following error:
Could not load type ‘System.Web.UI.ScriptReferenceBase’ from assembly ‘System.Web.Extensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35′.
Then you are on the right article

Solution

It’s highly likely that you compiled your code using .NET 3.5 Service Pack 1 but deployed to a system that has .NET 3.5 without Service Pack 1.
ScriptReferenceBase is a new class for Service Pack 1, clearly a refactoring of ScriptReference to allow common code for the new CompositeScriptReference class. If you compile your code using SP1, you will include a reference to this class, which does not exist in .NET 3.5 pre-SP1. When the non-SP1 runtime tries to load the class, you will receive the exception above.
Solutions are:

  • Compile using .NET 3.5 without SP1.

OR

  • Install SP1 on the target system

 

Create PerfMon WebPage

Challenge

If you’ve used PerfMon – tool from microsoft for performance monitor. [if you haven’t then it is really good to use tool for monitoring your application’s performance once It is deployed on server. Try it today!].
While monitoring performance we need to run perfmon tool [START | RUN | perfmon]. But what if admins need to view it from web browser and it can be from anywhere.

Solution

Here’s the solution for that. We can configure perfmon to run inside a webpage [But you need IE!].
Step by step guide is here: http://www.myitforum.com/inc/arts/9435MMC%20to%20webpage.doc
Reference : http://www.myitforum.com/articles/15/view.asp?id=7407
Happy Performance Monitoring! 🙂

Merging two XML files in to one dataset

Challenge:

Since so long back one of my best buddy Devang asked me that how can we merge two Datasets in one. In short, suppose you’ve two XML Files and you want to read them in different dataset. So, obviously they both will belong to two different data sets. But you would like to have both of them in a single dataset. How can you do it?
Before you see the solution code. Let’s see the problem by code
[sourcecode language=”csharp”]
private const string FILE1_PATH = "~/Books1.xml";
private const string FILE2_PATH = "~/Books2.xml";
protected void Page_Load(object sender, EventArgs e)
{
// Read First Xml
DataSet dataSet1 = new DataSet();
dataSet1.ReadXml(Server.MapPath(FILE1_PATH));
// Read Second Xml
DataSet dataSet2 = new DataSet();
dataSet2.ReadXml(Server.MapPath(FILE2_PATH));
// Merge — Will not work
dataSet1.Merge(dataSet2);
// Read in Table — Will not work
dataSet1.Tables[0].ReadXml(Server.MapPath((FILE1_PATH)));
dataSet2.Tables[0].ReadXml(Server.MapPath((FILE2_PATH)));
// Merge DataSet2.Table in first dataset — WILL NOT WORK — DataTable already belongs to another DataSet.
dataSet1.Tables.Add(dataSet2.Tables[0]);
}
[/sourcecode]

Solution

When we try to use following line.
[sourcecode language=”csharp”]
// Merge DataSet2.Table in first dataset — WILL NOT WORK — DataTable already belongs to another DataSet.
dataSet1.Tables.Add(dataSet2.Tables[0]);
[/sourcecode]
It will throw following error:
DataTable already belongs to another DataSet.
System.ArgumentException was unhandled by user code
Message=”DataTable already belongs to another DataSet.”
Source=”System.Data”
StackTrace:
at System.Data.DataTableCollection.BaseAdd(DataTable table)
at System.Data.DataTableCollection.Add(DataTable table)
at _Default.Page_Load(Object sender, EventArgs e) in c:\TestHarness\MergeTwoXmlFiles\Default.aspx.cs:line 35
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
Reason fort an error is dataset2’s Table number 0 can’t be added to dataset1’s tables collection as it is already in dataset2’s tables collection — Recall Reference types?
So, final solution is as below:
[sourcecode language=”csharp”]</pre>
// Remove table from second dataset
DataTable dt = dataSet2.Tables[0];
dataSet2.Tables.Remove(dt);
// and add it to first one
dataSet1.Tables.Add(dt);
[/sourcecode]
Happy Dataset Merging! 🙂