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! 🙂

Adding Assembly in to GAC-Progrmmatically

How To Add Assembly in GAC(Global Assembly Cache) Programmatically?

In my last project I need to ad Assembly to GAC…you will tell go to Microsoft .Net Configuration tool and Add Assembly to GAC….i also know this i am not that much dump…But i need to add it programmatically….now you also have to think na???? But i have its solution so don’t worry just follow this Post..
First i think u know that what it requires to add an assembly to GAC..What??????You don’t…….no worries yaar Main Hoon na…
For Adding an Assembly to GAC…your assembly should have strong name…what???you don’t know how to make an assembly which contains strong name….i am going to be a Mad now you don’t know anything… anyways i will guide you to do that so no worries.
Just Have to follow 3 Steps:
Step 1: Open .NET Framework SDK Command Prompt. and Type sn- k <keyname>.snk
Strong NAme01
This will generate a Key at current path. In Above’s examples(C:\).
Step 2. Now Add this Strong name to your solution by right click on solution and Add an Existing Item and locate Key File.
Strng Name 02
Step 3: Now Open AssemblyInfo.CS/Vb file and add [assembly: AssemblyKeyFile(“key.snk”)] this line.
Strong name03
That’s it now your Assembly have a Strong name and it is ready to be add in GAC..
Its easy na????
Now my main Aim comes tht how to add assembly to GAC programmatically.
It is also easy.Just follow me.
1. Add Reference to System.Enterpriseservices.
2. It has Publish Class make its object:
Publish publish = new Publish();
3. And now you can Install it into using this:
publish.GacInstall(<Path of your Assembly to Add in GAC>);
you can show a openFileDialog to user from it user can select an Assembly and based on FilePath Property you can add it into GAC..and Magic it is going to be added in GAC..user will Be happy!!![and that’s what a developer wants..am i right?].
Just by following 3 Steps your assembly is in to GAC..is it good na???
But there is one problem in it..can you guess what???No na..ok let me tell you.
See if Assembly dosen’t contains Strong Name than it will not show any error and not add it in to GAC Also..end user will not come in to know about it any time…so now what to do??? I have developed one Helper Class which will check that Assembly has strong name or not, user has permission to Add Assembly in to GAC etc.
So here is my Helper Class:
[sourcecode language=”csharp”]
<em><strong>public class Helper
{
public enum ErrorMessage
{
None, //NO ERROR
Unknown, //NOT IDENTIFIED ERROR OCCURED
AssemblyNotFound, //Assembly can’t be found
AssemblyLoadError, //Error during loading assembly
NotCLRAssembly, //Specified Assembly is Not a CLR Assembly
NotSignedAssembly, //NOT ASSIGNED STRONG NAME
NotAuthorized //Security Exception</strong></em>
<em><strong> } </strong></em>
<em><strong> public static ErrorMessage InstallAssembly(String assemblyPath)
{
if (String.IsNullOrEmpty(assemblyPath))
return ErrorMessage.AssemblyNotFound;
try
{</strong></em>
<em><strong>Assembly assembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath);
byte[] pkey = assembly.GetName().GetPublicKeyToken();
if (pkey.Length == 0) return ErrorMessage.NotSignedAssembly;</strong></em>
<em><strong>}
catch (BadImageFormatException)
{
return ErrorMessage.NotCLRAssembly;
}
catch (FileLoadException)
{
return ErrorMessage.AssemblyLoadError;
}
catch (FileNotFoundException)
{
return ErrorMessage.AssemblyNotFound;
}
catch (SecurityException)
{
return ErrorMessage.NotAuthorized;
}
Publish publish = new Publish();</strong></em>
<em><strong>try
{
publish.GacInstall(assemblyPath.Trim());
return ErrorMessage.None;
}
catch (SecurityException)
{
return ErrorMessage.NotAuthorized;
}
} </strong></em>
<span style="color: #ff00ff;"> }</span>
[/sourcecode]
To use it:
[sourcecode language=”csharp”]
<em><strong> private bool InstallAssembly()
{
String AssemblyPath = assemblyofd.FileName;</strong></em>
<em><strong> OpenFileDialog assemblyofd = new OpenFileDialog();
bool installed = false;
if (!string.IsNullOrEmpty(AssemblyPath))
{
Helper.ErrorMessage ErrMsg = Helper.InstallAssembly(AssemblyPath);
switch(ErrMsg)
{
case Helper.ErrorMessage.None:
//MessageBox.Show("The assembly you specify is successfully installed into the GAC", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
installed = true;
break;</strong></em>
<em><strong> case Helper.ErrorMessage.AssemblyLoadError:
MessageBox.Show("Some problem occured During Loading an Assembly,Please Try Later", "Cannot Add Assembly", MessageBoxButtons.OK, MessageBoxIcon.Error);
installed = false;
break;
case Helper.ErrorMessage.AssemblyNotFound:
MessageBox.Show("Sorry, Unable to Find Assembly", "Cannot Add Assembly", MessageBoxButtons.OK, MessageBoxIcon.Error);
installed = false;
break;
case Helper.ErrorMessage.NotAuthorized:
MessageBox.Show("You are not authorized to install Assembly, please Contact your Administrator", "Cannot Add Assembly", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
installed = false;
break;
case Helper.ErrorMessage.NotCLRAssembly:
MessageBox.Show("The assembly you specify is not a managed assembly", "Cannot Add Assembly", MessageBoxButtons.OK, MessageBoxIcon.Error);
installed = false;
break;
case Helper.ErrorMessage.NotSignedAssembly:
MessageBox.Show("Unable to add the selcted assembly. The assembly must have a strong name (name,versioan and public key).", "Cannot Add Assembly", MessageBoxButtons.OK, MessageBoxIcon.Error);
installed = false;
break;
case Helper.ErrorMessage.Unknown:
MessageBox.Show("Some problem occured during Installing Assembly", "Cannot Add Assembly", MessageBoxButtons.OK, MessageBoxIcon.Error);
installed = false;
break;
}
}
return installed;
}</strong></em>
[/sourcecode]
That’s it.
This will serve the Purpose.
Try it and let me know it works for you or not.
“If you do your best whatever happens will be for the best”.

There is no script engine for file extension “.vbs”

Challenge:

If you received an error saying There is no script engine for file extension “.vbs” then you are on right post 🙂

Solution:

I tried doing this : regsvr32 %systemroot%\system32\vbscript.dll

http://www.technologystory.net/2009/02/25/windows-vista-tips-there-is-no-script-engine-for-file-extension-vbs/

But like you — for me too no luck 🙁

But finally following trick worked for me 🙂

1. Locate the file %windir%\inf\wsh.inf (inf is a hidden
folder)

2.right click and select “Install”.

Happy Coding!!

IE innerHTML – “Unknown runtime error”

Setting value of <div id=”divhtml”> using js like this :

document.getElementById(“divhtml”).innerHTML = “<form><input type=”text”>…</form>”;

it was giving me error..

Solution:

oldDiv = document.getElementById(divhtml);
newDiv = document.createElement(oldDiv.tagName);
newDiv.id = oldDiv.id;
newDiv.className = oldDiv.className;
newDiv.innerHTML = “<form><input type=”text”>…</form>”;

oldDiv.parentNode.replaceChild(newDiv, oldDiv);

And it worked like a charm!!!!!!

Link : http://piecesofrakesh.blogspot.com/2007/02/ies-unknown-runtime-error-when-using.html