More servicesWindows Live
HomeHotmailSpacesOneCare
 
MSN
Sign in
 
 
Spaces home  .NET Demon's SpaceProfileFriendsBlogMore Tools Explore the Spaces community

.NET Demon's Space

Naveed Akhtar's [MCPD] Coding Blog

Naveed Akhtar

View spaceSend a message
Occupation:
Age:
Location:
Interests:
I am Working as a Software Developer Since 2004. I have done Masters in Computer Science & Microsoft certification for Professional Developer.
No list items have been added yet.
July 09

Converting Data Table / Dataset Into JSON String

JSON (Java Script Object Notation), is a light weight, easily understandable to read and write string. It is also easily parse-able by machine.

JSON is introduced on two structues

A collection (key/value pair)

And ordered list of values.

I have not covered this topic in detail. Detailed analysis is stated on http://www.json.org/.

I am presenting a helper function (in C#) for developers for fast parsing on datatable / dataset into JSON String, and access it on client-side.

public static string GetJSONString(DataTable Dt)

{

string[] StrDc = new string[Dt.Columns.Count];

string HeadStr = string.Empty;

for (int i = 0; i < Dt.Columns.Count; i++)

{

StrDc[i] = Dt.Columns[i].Caption;

HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";

}

HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);

StringBuilder Sb = new StringBuilder();

Sb.Append("{\"" + Dt.TableName + "\" : [");

for (int i = 0; i < Dt.Rows.Count; i++)

{

string TempStr = HeadStr;

Sb.Append("{");

for (int j = 0; j < Dt.Columns.Count; j++)

{

TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "¾", Dt.Rows[i][j].ToString());

}

Sb.Append(TempStr + "},");

}

Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));

Sb.Append("]};");

return Sb.ToString();

}

Here, Dt is the datatable, and it returns JSON formatted string.

For detailed porcedure on how to access this string on client side, please refer to this link Exposing Webservices to Client-Side because I don't like to do task repetition.

Please donot forget to convert to JSON string to JSON Object using

var JObject = eval('(' + JSONString + ')');

in Javascript JObject have all characteristics of JSON object, through which you can Use JObject by iterrating or what so ever.

e.g., you can use your JObject as

for(var i = 0; i < JObject .Employees.length; i++)

{

var val1 = JObject.Employees[i].EmployeeID;

var val2 = JObject.Employees[i].NationalIDNumber;

var val3 = JObject.Employees[i].Title;

var val4 = JObject.Employees[i].BirthDate;

var val5 = JObject .Employees[i].HireDate ;

}

Please note that I am querieng data from AdventurWorksDB SQL Sample Database (Table: Employee).

I hope this article will be helpful for you.

Any Questions / Queries ??

Regards,

Naveed Akhtar

July 03

ASP.Net Compilation and Merge Tool (Part 2 of 2)

ASP NET Merge tool allows user to manage and combine assemblies that are created using ASP NET Compilation tool. It works only on assemblies that have been created by using ASP.NET 2.0 or higher.This tool creates one assembly for each folder in target application or it creates each assembly for each file.This tool gives you additional functionality for management and deployment. It allows user to do the following operations.Create single assembly for just single web application UI elements such as controls and pagesCreate assembly for whole web applicationCreate assembly for each web application and affix to assembly name

Syntax

aspnet_merge [-?] // -? displays command syntax

applicationPath

[-keyfile filename [-delaysign]]

[-o assemblyname | -w assemblyname | -prefix prefix]

[-copyattrs [assemblyfile]]

[-debug]

[-nologo]

[-errorstack]

[-r]

[-xmldocs]

[-a]

[-logfile logfile]

[-allowattrs textfile]

Example

The command stated below merges the assemblies of the precompiled site in the C:\PreCompSite directory.

Aspnet_merge C:\ PreCompSite
The command stated below merges assemblies of a precompiled web application in the C:\ PreCompSite directory and signs the merged assemblies by using the KFile.snk file. The merged application will have one assembly for each precompiled application folder.
Aspnet_merge C:\ PreCompSite -keyfile KFile.snk
The command stated below merges all assemblies of the precompiled site in the C:\ PreCompSite directory into a single assembly and names the resulting assembly MyWebApplication.dll. The merged site will have one assembly for all Web site UI content.
Aspnet_merge C:\ PreCompSite -w MyWebApplication.dll
The command stated below merges all assemblies of the precompiled site in the C:\ PreCompSite directory into a single assembly and names the resulting assembly MyWebApplication.dll.
Aspnet_merge C:\ PreCompSite -o MyWebApplication.dll
Why to Use Aspnet_merge tool

Please refer to my previous post ASP.Net Compilation and Merge Tool (Part 1 of 2) for advantages.

ASP.Net Compilation and Merge Tool (Part 1 of 2)

ASP.NET Compilation tool is used to compile an ASP.NET Web application either in same machine or to deploy on a target machine such as a production server. It helps application performance because end users do not encounter a delay on the first request to the application while the application is compiled on back-end.

Compilation for deployment can be in one or two ways

  • That removes all files such as code-behind and markup files
  • That retains markup files.
Syntax

aspnet_compiler [-?]

[-m metabasePath | -v virtualPath [-p physicalPath]]

[[-u] [-f] [-d] [-fixednames] targetDir]

[-c]

[-errorstack]

[-nologo]

[[-keyfile file | -keycontainer container ] [-aptca] [-delaysign]]

Example

Aspnet_compiler -v /WebApp1 -p "c:\MyProjects\WebApp1" -keyfile "c:\MyProjects\KeyWebApp1.sn" -aptca c:\applicationTarget

Why to use ASP.Net Compilation Tool

People always ask, why I use ASP.Net Compilation tool. The answer is “precompiled ASP.Net 2.0 Web Application”. Following are the advantages to use ASP.Net Compilation and Merge Tool. According to MSDN

  • Security
  • Performance
  • Stability
Security

Pre-compilation of ASP.Net Application slow down reverse engineering process, because it lacks the abstraction and readability of a high-level language.

Performance

Compiled code faster than other scripting languages such as VBScript or ECMAScript because it is a closer to machine code and does not need additional parsing.

Stability

Code is checked on compilation for syntax, type safety and other sort of problems by caching error at build process and then you can eliminate these errors.

June 09

How to use Javascript using Server-side scripting

Hi.
This article is interesting for those who loves to use javascript, but with little server postbacks. I had some times issues related to validation or client redirection or other sorts. So i got this solution and i would like to share it with you.
Here I am presenting a scenario. I have a button, and OnClick of this button, i want to call java script function.
 
On Server Side (.aspx.cs)
--------------------------
Write this code under Button_Click event or in any server event.

ClientScript.RegisterStartupScript(this.GetType(), "key", "SubmitConfirm();",true);

You can also pass server control, properties to the "SubmitConfirm();" function.
On Client Side (.aspx)
--------------------------
write within javascript tag

function SubmitConfirm()
{
  alert('Form Submitted'); window.location='TestPage.aspx';
}
-------------------------------------
 
I hope this will be helpful for you.
Any Comments / Suggestion...