Tuesday, January 27, 2009

XAML in .net : A Step Ahead Series

XAML : stands for eXtensible Application Markup Language and pronounced as zammel".

XAML combined with the next-gen Windows graphics kernel (code-named Avalon) is an all-in-one markup language for hypertext (HTML), vector graphics (SVG), animations (Flash,SMIL), print documents (PDF,XSL-FO), forms (XForms,XUL), and much more.

The best of both worlds: In the XAML world there's no longer a difference between a web application and a windows application. Windows is the browser kernel and Internet Explorer is just a shell.

The best of all worlds: A single programming model for media, documents and applications.

A typical XAML example:


<Window Text="Xul Challenge 2004">
<FlowPanel>
<SimpleText>Counter Sample</SimpleText>
<TextBox ID="ValueTextBox" HorizontalAlignment="Center" IsReadOnly="True"/>
<FlowPanel HorizontalAlignment="Center" Width="100%">
<Button Click="Dec">Dec (-)</Button>
<Button Click="Clear">Clear</Button>
<Button Click="Inc">Inc (+)</Button>
</FlowPanel>
</FlowPanel>
</Window>



<b>C# vs. XAML</b>

1. XAML-Example
<TextPanel
Background="BlanchedAlmond"
FontFamily="Comic sans MS"
FontSize="36pt"
HorizontalAlignment="Center">
Hello, world!
</TextPanel>

2. C#-Example

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

class HelloWorldApp : Application
{
static void Main()
{
HelloWorldApp app = new HelloWorldApp();
app.Run();
}

protected override void OnStartingUp( StartingUpCancelEventArgs args )
{
Window win = new Window();
TextPanel tp = new TextPanel();

tp.Background = Brushes.BlanchedAlmond;
tp.FontFamily = "Comic sans MS";
tp.FontSize = new FontSize( 36f, FontSizeType.Point);
tp.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
tp.TextRange.Text = "Hello, world";

win.Children.Add(tp);
win.Show();
}
}



For more details, please refer to:

1. http://msdn.microsoft.com/en-us/library/ms752059.aspx
2. http://xaml.sourceforge.net/talk/dotnet-dec-2004/slides.html

Sunday, January 25, 2009

State Management

The  State or Cache Management is nothing but the way to storing the data in Client-Side and in Server-Side using preity small memory.


There are two major category of the above :
I. Server-Side State Management

Session State: Its nothing but defined as a period of time shared between the web application and user. Every user has individual session. Items/Objects can be placed into the Session which would only define these object for that user. Session contains key variables which help to identify the related values. This can be thought of as a hash table. Each user would represent a different key node in the hash identifying unique values. The Session variables will be clear by the application which can clear it, as well as through the timeout property in the web config file. Usually the timeout is 20 minutes by default.

Session Variables are stored on the server, can hold any type of data including references, they are similar to global variables in a windows application and use HTTP cookies to store a key with which to locate user's session variables.

The collection of session variables is indexed by the name of the variable or by an integer index. Session variables are created by referring to the session variable by name. You do not have to declare a session variable or explicitly add it to the collection.

Lets get it cleared from following example:

Session[“firstName”] = “Gaurav” //User’s first name
Session[“lastName”] = “Arora” //User’s last name

// Clear the session variable 
Session[“FirstName”] = null; 

//Clear all Session variables 
Session.Abandon();  


InProc—Stores Session state in the same process as the ASP.NET process [aspnet_wp.exe].

StateServer—Stores Session state in a Windows NT process, which is distinct from the ASP.NET process[aspnet_state.exe].

SQLServer—Stores Session state in a SQL Server database.

Both in StateServer and SQLServer options, we need to ensure that the objects we cache are serializable as data storages are out-of-process systems. Both these options have impact on the application performance as data retrieval and saving operations take more time when compared to the InProc option. So based on our application requirement we should choose the option that best suits our requirement.


Note:
By default, ASP.NET session state is enabled for all ASP.NET applications.

ASP.NET application object

ASP.NET provides an object called Application object to store data that is accessible to all user requests. The life span of this object is tied to the application and it is re-created every time the application starts. Unlike ASP.NETSession object this object is accessible to all user requests. Since this storage is created and maintained in an application domain space, this should not be used for data storage in a web farm scenario. This option is very useful to store data like the application metadata (CONFIG files data) that can be loaded to the Application object during application start up and can be used during the life of the application without reloading it every time for each user request. But if there is a requirement to invalidate the cached data whenever there is any change to the CONFIG files while the application is running, this option should not be used as it doesn't provide any feature to expire the cached data. So in this case other options like the ASP.NET Cache object should be used, which is explained below.

Types of Cache –Dependencies

When anyone add an item to cache, he/she can define the dependency relationships that can force that item to be removed from the cache under specific activities of dependencies.

For example: If the cache is dependent on file and when the file data changes you want the cache object to be updated.

Following are the difference dependencies:

(a) File Dependency :Allows invalidating a specific cache item when a disk based file or files change.

object errorData;
 //Load errorData from errors.xml 

 CacheDependency fileDependency = 
     new CacheDependency(Server.MapPath("errors.xml")); 
 Cache.Insert("ERROR_INFO", errorData, fileDependency);


(b) Time based expiration :Allows to invalidate a specific cache item depending on predefined time.


//Absolute Expiration

Cache.Insert("EMP_NAME", "Shubhabrata", null, 
             DateTime.Now.AddDays(1), Cache.NoSlidingExpiration);

//Sliding Expiration

Cache.Insert("EMP_NAME", "Shubhabrata", null, 
             Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(60));


(c) Key dependency :Allows to invalidate a specific cache item depending when another cache item changes.

string[] relatedKeys = new string[1]; 
relatedKeys[0] = "EMP_NUM";
CacheDependency keyDependency = new CacheDependency(null, relatedKeys); 
Cache["EMP_NUM"] = 5435;
Cache.Insert("EMP_NAME", "Shubhabrata", keyDependency);
Cache.Insert("EMP_ADDR", "Bhubaneswar", keyDependency);
Cache.Insert("EMP_SAL", "5555USD", keyDependency);


.NET remoting
You might be thinking how .NET remoting can be used for data caching? The same question came to my mind when I heard about it for the first time. As you know the .NET remoting singleton object shares the same instance with multiple clients so singleton objects can be used to store and share data between different client invocations. Since .NET remoting can be used outside the process and machine, this option is very useful when we want to cache data and share it across servers and users particularly in a web farm scenario. In this approach we can store the data as member variables of singleton remoting object and provide methods to read and save data. But while implementing this we need to ensure that the remoting object used as cache is not destroyed by the garbage collector. For that we will have to set the remoting cache object's lease period to infinite so that the lease period never times out. We can do that by overriding the InitializeLifetimeService method of MarshalByRefObject and return null from this method. But the only issue with this approach is performance. As the analysis says the performance is very poor in this approach when compared to other approaches. Anyway it is up to the application designers/developers to analyze different options and use the best one that suits the requirement.

Memory-mapped files
You all know what a memory-mapped file is. It is basically about mapping a file on disk to a specific range of addresses in the application's process address space. This option allows different processes to use the same data by increasing the application performance. As using memory-mapped file is not very popular among .NET developers, I would personally not suggest this approach as implementing this involves a lot of complexities and also .NET Framework doesn't support this. But if anyone is very much interested in using this approach then they will have to develop their own custom solution as per their own requirement.

Static variables
We use static variables for storing data or objects globally so that it can be accessed during the life of the application. Similarly, in ASP.NET we can use static objects for caching data and we can also provide methods to retrieve and save data to the cache. As static variables are stored in the process area, performance wise it is faster. But since it is very difficult to implement expiration policies and dependencies incase of static variables, I generally prefer ASP.NET cache object over this option. Another problem is that the custom static cache object has to be thread-safe which has to be implemented carefully.

Example:
public class CustomCache
{
    //Synchronized to implement thread-safe

    static Hashtable _myCache = 
             Hashtable.Synchronized(new Hashtable());
    
    public static object GetData(object key)
    {
        return _myCache[key];
    }
    
    public static void SetData(object key, object val)
    {
        _myCache[key] = val;
    }
}



II. Client-Side State Management

Cookies
Cookie is a very familiar term in web development environment. Cookie is a client-side storage that is sent to the server for each request and also received as response back from the server. Because of its size limitation (4096 bytes) it should be used for storing small amount of data. Expiration policies can be set for cookies to invalidate the items after a certain period of time. 

The following example shows how Cookie can be used in an ASP.NET application:

if (this.Request.Cookies["MY_NAME"] == null) 
{ 
    this.Response.Cookies.Add(new HttpCookie("MY_NAME", 
                                       "Shubhabrata Mohanty")); 
} 
else 
{ 
    this.Response.Write(this.Request.Cookies["MY_NAME"].Value); 
}


ViewState

ASP.NET ViewState is a new concept. Here the data related to the pages and controls are stored in ViewState which retains the values across multiple requests to the server. If you remember correctly, in VB-ASP applications we used to store data across multiple requests using hidden fields. ViewState is actually implemented internally as hidden fields in ASP.NET but here the data is hashed to improve security as against hidden fields. To see how ViewState is implemented, you can view the source of the page in which ViewState is used in the Internet browser. ViewState should not be used to store large amounts of data as it is passed to the server for each request

protected void Page_Load(object sender, EventArgs e) 

{
    if (this.ViewState["MY_NAME"] == null) 
    { 
        this.ViewState["MY_NAME"] = "Shuby";
    } 
    
    //txtName is a TextBox control 

    this.txtName.Text = this.ViewState["MY_NAME"].ToString(); 
}

Hidden fields

Hidden fields are very popular among VB-ASP web developers. Hidden field is similar to any other control in a page but the visible state of this control is always false. Like ViewState we should not use it for storing large amounts of data. 
Note:Similarly hidden frames can be used to cache data in the client side. But please note that hidden frames are not supported by all Internet browsers.

<!--In ASP.NET-->
<asp:hiddenfield id="myHiddenField" value="Shuby" runat="server" />
<!--In HTML-->
<input id="myHiddenField" type="hidden" value="Shuby">
</asp:hiddenfield>

Query Strings

Query strings provide a simple but limited way of maintaining some state information.You can easily pass information from one page to another, But most browsers and client devices impose a 255-character limit on the length of the URL. In addition, the query values are exposed to the Internet via the URL so in some cases security may be an issue. 

http://www.msdotnetheaven.com/?authorid=4

Example:
string authorid;
authorid=Request.Params[“authorid”];

Note: You can only use HTTP-Get method to post the web page, or you will never get the value from query strings.

Difference between cache, session and viewstate

Cache: Its nothing but a thought kind of memory. In respect to asp.net it’s the memory of the machine/server from where the source-code is running. It is the one way which allow to store complex data for reusability.
Now think a criteria where clients access an ASP.NET page, there are basically two ways to provide them with the information they need:
  • The ASP.NET page can either obtain information from server resources, such as  from data that has been persisted to a database, or
  • The ASP.NET page can obtain information from within the application.
Retrieving information from a resource outside the application will require more processing steps, and will therefore require more time and resources on the server than if the information can be obtained from within the application space.

Now, suppose the information’s which sent to browser’s have already been prepared then how faster the process of web-page.

The ASP.NET 3.5 Framework supports the following types of caching:
Page Output Caching :Page Output Caching caches an entire page.          

Partial Page Caching :Partial Page Caching enables you to get around this problem by enabling you to cache only particular regions of a page.

DataSource Caching:You use DataSource Caching with the different ASP.NET DataSource controls such as the SqlDataSource and ObjectDataSource controls. When you enable caching with a DataSource control, the DataSource control caches the data that it represents.
 
Data Caching :Finally, Data Caching is the fundamental caching mechanism. Behind the scenes, all the other types of caching use Data Caching. You can use Data Caching to cache arbitrary objects in memory. For example, you can use Data Caching to cache a DataSet across multiple pages in a web application.
 
Note: 
The Cache object can also have an expiration which would allow us to reinstitute data into the memory in intervals. Using the same example as above, we can make the cache expire every two hours, and repopulate the data. It would do this every 2 hours throughout the day, allowing the most up to date data to be fetched. Below is an example of how something can be put into the cache: 

Referenced from msdn 
 
public void AddItemToCache(Object sender, EventArgs e) 
{ 
    itemRemoved = false; 
    onRemove = new CacheItemRemovedCallback(this.RemovedCallback); 
    if (Cache["Key1"] == null) 
        Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60),TimeSpan.Zero, CacheItemPriority.High, onRemove); 
} 
 
 
public void RemoveItemFromCache(Object sender, EventArgs e) 
{ 
    if(Cache["Key1"] != null) 
        Cache.Remove("Key1"); 
}
 
 
Session: Its nothing but defined as a period of time shared between the web application and user. Every user has individual session. Items/Objects can be placed into the Session which would only define these object for that user. Session contains key variables which help to identify the related values. This can be thought of as a hash table. Each user would represent a different key node in the hash identifying unique values. The Session variables will be clear by the application which can clear it, as well as through the timeout property in the web config file. Usually the timeout is 20 minutes by default.
 
Session Variables are stored on the server, can hold any type of data including references, they are similar to global variables in a windows application and use HTTP cookies to store a key with which to locate user's session variables.
 
The collection of session variables is indexed by the name of the variable or by an integer index. Session variables are created by referring to the session variable by name. You do not have to declare a session variable or explicitly add it to the collection.
 
Lets get it cleared from following example:
 
Session[“firstName”] = “Gaurav”    //User’s first name
Session[“lastName”] = “Arora”        //User’s last name
 
// Clear the session variable 
Session[“FirstName”] = null; 
 
//Clear all Session variables 
Session.Abandon();  
 
 
Note:By default, ASP.NET session state is enabled for all ASP.NET applications.
 
 
ViewState: Its nothing but it’s a hidden data which is kept by asp.net pages in “_VIEWSTATE” as hidden form field. They track the changes to a web site during post backs.
 
The ViewState of a page can be seen if you view the source code of the running page(Right Click --> View Source). It will be listed under the following tags:
Having a large ViewState will cause a page to download slowly from the users side. When a user clicks on a button and a post back occurs, all the view state information has to be posted back to the server which causes a slower request. Most times this is insignificant, but imagine a person using a dial up connection. If requests, and page loading is taking a couple of minutes, most likely the user will get frustrated, and leave.

Note:
  1. By default, View State is enabled for every control in the ASP.NET Framework.
  2. ViewState does not hold the controls, rather it holds the values of the form controls and their corresponding ID's that would otherwise be lost due to a post back because they do not post with the form.
  3. ViewState is not used to hold session data or to transmit data between pages.
  4. ViewState does not recreate the dynamically created controls of a page.
  5. It does not restore the values to the controls after a post back operation.

Friday, January 23, 2009

Capturing CTRL+Keys using JavaScript

This is the practical scenario where sometime one wants to disallow Ctl+keys combinitions on web-pages.

Following Code-snippet tells the whole story:


<html>
<head>
<script language="JavaScript">
function testCtrlKeys(e)
{
//Create an array for all possible keys
var arrCtrlKeys = new Array('a', 'n', 'c', 'x', 'v', 'j');
var key;
var isCtrl;

if(window.event)
{
key = window.event.keyCode; //Valid only for Internet Explorer
if(window.event.ctrlKey)
isCtrl = true;
else
isCtrl = false;
}

else
{
key = e.which; //this is other than IE
if(e.ctrlKey)
isCtrl = true;
else
isCtrl = false;
}


//if ctrl is pressed check if other key is in forbidenKeys array
if(isCtrl)
{
for(i=0; i<arrCtrlKeys .length; i++)
{
//case-insensitive comparation
if(arrCtrlKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
{
alert('You have pressed CTRL + '
+String.fromCharCode(key)
+'.');

return false;
}
}
}
return true;
}
</script>

</head>
<body>
<form method="">
<div>
<h3>Test Key Combinition [Ctrl+A, Ctrl+N, Ctrl+C, Ctrl+X, Ctrl+V,Ctrl+J]</h3>
<input type="text" name="mytext"
onKeyPress="return testCtrlKeys(event);"
onKeyDown="return testCtrlKeys(event);" />
</div>
</body>
</html>


Note:
1. Copy/paste above code in text file and save it as "nameddoc.html" and run it into Explorer
2. Check the magic of capturing javascript keys.

Capturing MouseEvents Using JavaScript

Capturing Mouse Events Using JavaScript :


Sometime there is a need to disable MouseEvents for specific task(s) in Web-Pages.

The following code-snippet shows how to achieve the same :


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Capturing Mouse Events Using JavaScript</title>
</head>
<body>
<div>
<b> Testing with Link Button : </b>
<a href="javascript:void(null)"
onmousedown="return captureButton(event)" onmouseup="return preventOperation(event)"
onclick="return preventOperation(event)" ondblclick="return preventOperation(event)"
oncontextmenu="return preventOperation(event)"
>Click here with various mouse buttons to test</a>
</div>
<br/>
<div>
<b> Testing wuth Submit Button : </b>
<input name=btnG type=submit onmousedown="return captureButton(event)" onmouseup="return preventOperation(event)"
onclick="return preventOperation(event)" ondblclick="return preventOperation(event)"
oncontextmenu="return preventOperation(event)">
</div>
<br/>
<div>
<b> Testing with Input Text : </b>
<input name=btnG type=text onmousedown="return captureButton(event)" onmouseup="return preventOperation(event)"
onclick="return preventOperation(event)" ondblclick="return preventOperation(event)"
oncontextmenu="return preventOperation(event)">
</div>

<script language="Javascript">
function captureButton(event)
{
var button;
if (event.which == null)
button= (event.button < 2) ? "MOUSE BUTTON : LEFT" :
((event.button == 4) ? "MOUSE BUTTON : MIDDLE" : "MOUSE BUTTON : RIGHT");
else
button= (event.which < 2) ? "MOUSE BUTTON : LEFT" :
((event.which == 2) ? "MOUSE BUTTON : MIDDLE" : "MOUSE BUTTON : RIGHT");
alert(button);
preventOperation(event);
}
function preventOperation(event)
{
if (event.preventDefault)
event.preventDefault();
else
event.returnValue= false;
return false;
}
</script>

</body>
</html>



Copy paste above code in a text document and named it as MouseEvents.html
Now test the same.

Get MicroSoft SDK's under single roof

Now MicroSoft SDK's are available in a single page.

One can get download required SDK's from the page MicroSOft SDK's.

The are under heads :

1. Azure Services Platrform
2. Office
3. Windows Desktop
4. Devices
5. Games
6. Server Technologies
7. Others

If need any kind of assistant, please fell free to contact me.

Thursday, January 15, 2009

MCP/MCTS : Welcome Kit

Today I have got my Welcome kit for MCTS. I have successfully achieved the same with 96.4% marks.

I got this news from my wife Shuby Arora.

Want to share my joy with all of you.

Thursday, January 1, 2009

Writing CLR Stored Proc : Using Visual Studio

Overview & History


First of all let me say my thanks to
Mr. Joymon whose question inspired me to write this resource.

The present post is showing the another method to Write a CLR Proc apart from the method in my earlier post
Writting a CLR Stored Proc.

Step:Followings are the steps to start the above in Visual Studion



1. Start Visual Studio2005 or higher
2. Add new file from File -> New -> Project
3. Select Database Project [provide the name]
4. Create the project, it requires Database Reference [I used EmployeeDB here]
5. In Solution Explorer Right Click on the project name and Click Add
6. Select Stored Procedure
7. Add new File with name 'myTestStoredProcedure.cs'
8. The added file will look like :


using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class StoredProcedures
{

[Microsoft.SqlServer.Server.SqlProcedure]
public static void myTestStoredProcedure()
{
//Right some code here

}
};



I am writing a code for following three Stored Proc:

1. myTestStoredProcedure - Simply prints a message
2. spGetRolesList - Display the rows from table
3. spGetEmployeeList - Display the rows from table for specific Age group.

The following code tells how to create a CLR stored proc with the use of Visual Studio:


/************************************************
* Topic : How to Create CLR Proc using VS
* Author : Gaurav Arora
* Reference : A Step Ahead Series - SQL2005
* Visit : http://msdotnetheaven.com
* **********************************************/

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;


public partial class StoredProcedures
{
///
/// Prints a Message
///

[Microsoft.SqlServer.Server.SqlProcedure]
public static void myTestStoredProcedure()
{
//Simple proc
SqlPipe objSqlPipe = SqlContext.Pipe;
objSqlPipe.Send("Hi! I am simple CLR PROC");

}
///
/// Proc to Show Rows of [EmployeeDB]..[Roles] table
///

[Microsoft.SqlServer.Server.SqlProcedure]
public static void spGetRolesList()
{
//It returns rows from Roles table
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Context Connection=true";

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = @"Select * from [dbo].[Roles] Order By HireDate";
conn.Open();

SqlDataReader sqldr = cmd.ExecuteReader();
SqlContext.Pipe.Send(sqldr);

sqldr.Close();
conn.Close();


}
///
/// It shows rows from Employee table on basis of supplied age
///
/// a specified age
[Microsoft.SqlServer.Server.SqlProcedure]
public static void spGetEmployeeList(Int32 intAge)
{

//It returns rows from Employee table on basis of supplied age
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Context Connection=true";


SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "Select * from [dbo].[Employees] Where Age >=@intAge Order By Age";

SqlParameter paramAge = new SqlParameter();
paramAge.Value = intAge;
paramAge.Direction = ParameterDirection.Input;
paramAge.DbType = DbType.Int32;
paramAge.ParameterName = "@intAge";

cmd.Parameters.Add(paramAge);

SqlDataReader sqldr = cmd.ExecuteReader();
SqlContext.Pipe.Send(sqldr);

sqldr.Close();
conn.Close();

}
};


Build and Deploy the assembly :



1. Press ctrl+shift+B or Select Build option Under Build
2. If project successfully Build, now its time to deploy the assembly in SQLServer Directory.
3. Right click on Project name in SOlution Explorer
4. Click on Deploy.
5. Check the Status Bar for further.
6. If It deployed successfully, you can check the CLR proc from Server Explorer
7. Expand EmployeeDB node.
8. Expand Assemblynode [ you can find 'AStepAheadProcVisual' CLR Assembly]

Executing CLR Stored Proc :



Using Server Explorer of VS

1. Expand the AStepAheadProcVisual node
2. Here you can see a class file and Assembly.info file including three procs.
3. Right click on anyone Stored Proc [option availale :Open, Execute, Step Into Stored Procedure ].
(a) Open : Directs to a specific proc.
(b) Execute : Executes the selected proc and result is available in OutPut window.
(c) Step Into Stored Procedure : It debugs the application following selected proc.


Using SQLServer Management Studio

1. Open your SQLServer Management Studio[if not opened earlier]
2. Type following code in Query Window



Use [EmployeeDB]
Go

DECLARE @Role int
SET @Role = 28
Exec [dbo].[spGetEmployeeList] @Role
Go



3. The above code will display the EmployeeList with Employees having age 28Yrs or more.
4. In same manner execute other procs



Use [EmployeeDB]
Go

Exec [dbo].[myTestStoredProcedure]
Go

Exec [dbo].[spGetRolesList]
Go

DECLARE @Role int
SET @Role = 28
Exec [dbo].[spGetEmployeeList] @Role
Go



Scripts of Databases:



Followings are the scripts of Table(s) used in given example:



USE [master]
GO
/****** Object: Database [EmployeeDB] Script Date: 01/01/2009 23:04:12 ******/
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'EmployeeDB')
BEGIN
DROP DATABASE [EmployeeDB]
CREATE DATABASE [EmployeeDB]
END

USE [EmployeeDB]
GO
/****** Object: Table [dbo].[employees] Script Date: 01/01/2009 23:05:24 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[employees]') AND type in (N'U'))
BEGIN
DROP TABLE [dbo].[employees]

CREATE TABLE [dbo].[employees](
[id] [nvarchar](4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[firstname] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL DEFAULT ('First Name'),
[lastname] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL DEFAULT ('Last Name'),
[age] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL DEFAULT ('19'),
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END

GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Roles]') AND type in (N'U'))
BEGIN
DROP TABLE [dbo].[Roles]
CREATE TABLE [dbo].[Roles](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Role] [nvarchar](300) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[IsRetiree] [bit] NOT NULL,
[HireDate] [datetime] NOT NULL,
CONSTRAINT [PK_Roles] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END

GO



IMPORTANT NOTE:


1. If get error : Execution of user code in the .NET Framework is disabled. Enable "clr enabled" configuration option.
, While executing the above using SQLSERVER2005/2008, run following code in Query analyzer:



sp_configure 'clr enabled', 1
go
reconfigure
go



2. You can also Debug the Produced code within VS: by starting Debugging from DEBUG MENU


Hope the represented article is worthful for you.

Monday, December 22, 2008

MicroSoft in OpenSource : PHP

As the technologies grow, Microsoft is also lightning it.
Open-source initiatives at Microsoft are important to the open-source community because they give developers greater exposure for their products through access to a broadly adopted platform….The (open-source development and interoperability) initiatives are important because they break down barriers between proprietary and open-source developers allowing them to benefit from each other’s work.

Take a round to an interview with the PHP Classes blog.

Microsoft engineers and contractors have made contributions to the PHP run-time engine and to PHP application projects. And communication between Microsoft; commercial open-source-based companies including Zend, OmniTI, and iBuildings; and open-source developers has broadened significantly.

In other words, both the PHP community and Microsoft benefit from this interoperability development.
For more info: http://news.cnet.com/8301-13505_3-10126079-16.html

Open-Source Blog : Launched by MicroSoft

MicroSoft launched an Open-Source blogging platform
However, the software maker was quick to underline that the product is aimed at developers and not intended to directly compete with popular blogging software such as WordPress or Movable Type.
The same has been available at : oxite
One need MicroSoft Public licence from Open-Source License.
Read about first public release at : http://www.codeplex.com/oxite/Release/ProjectReleases.aspx?ReleaseId=20210
Oxite is a standards-compliant, extensible content-management system designed to support either blogs or larger Web sites, Microsoft said. The platform includes support for features such as pingbacks, trackbacks, anonymous or authenticated commenting, gravatars (globally recognized avatars), and RSS feeds at any page level, the company said.
For more details: http://news.cnet.com/8301-1001_3-10119017-92.html?subj=news&tag=2547-1_3-0-20&part=sphere

Sunday, December 14, 2008

Redirect to Error Page Using JavaScript

My readers enquired that how to rehuffle the yellow page on web.
The Yellow page appears when Web-Site got any runtime error.

I always suggested to supercede the Yellow-Page. Try the following code :


namespace AStepAhead.redirectErrorPage
{
public class redirectErrorPage
{
public redirectErrorPage()
{
String strDomainName = “MsDotnetHeaven.com”;
try
{
//Write you coding here
}
catch (Exception ex)
{
SystemFramework.ApplicationLog.WriteError(”Error : ” + ex.ToString());
//Redircet you main page to error page
Response.Write(”\n”);
Response.End();
}
}
}

}


Now, in error page, read ErrorString and show to the users.

Step(s): Followings are the steps to perform above:

1. Create an Error Page and named it as errorpage.aspx.
2. Read Error string at Page_load
3. Write above code in your Web Page.

After above, whenever any runtime exception occured, your user will see a maintained / designed error page.

Sunday, November 30, 2008

Writting a CLR Stored Proc

With the launch of SqlServer2005, we got the ability to write CLR from any .Net language.

Overview:


The following code-snippet tells how to write a simple CLR Stored Proc with the use

of C#.

Step:

1. Start Visual Studio2005 or higher
2. Add new file from File -> New -> File
3. Select Class with C#
4. Mark the name as AStepAheadProc.cs
5. Write the following lines:


/* This Example is a part of different
* examples shown in Book:
* C#2008 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : Gaurav Arora */

using System.Data.SqlServer;
using System.Data.SqlTypes;

public class AStepAheadProc
{
public static void FirstProcProc()
{
//Client output buffer

SqlPipe sqlPipe = SqlContext.GetPipe();

//send the output

sqlPipe.Send("This is demo Store Proc Assembly");

}
}


Explanation :

The sqlContext provides The GetPipe() method returns a SqlPipe that we can use
to send results back to the client.

Steps of Use/Working

1. Compile the above code into assembly


csc /target:library c:\AStepAheadProc.cs /r:"D:\Program Files\Microsoft SQL Server\
MSSQL.1\MSSQL\Binn\sqlaccess.dll"


2. Load the created assembly in SQL with the use of CREATE PROCEDURE


create procedure AStepAheadProc
as external name AStepAheadProc.AStepAheadProc.FirstProcProc


3. Execute the Created stored procedure in Query Analyzer


exec AStepAheadProc


Note:

The above code-snippet is just describe how to create a CLT Stored Proc.

Thursday, November 27, 2008

Get Client System's Drive List : JavaScript

The following code tells how to get client systems Drive List.


<HTML>

<HEAD>



<SCRIPT language=JavaScript>



//Following function returns the drive list from client system

function getClientDrives() {

var objfs, s, n, e, x;

objfs = new ActiveXObject("Scripting.FileSystemObject");

e = new Enumerator(objfs.Drives);

s = "";

do {

x = e.item();

s = s + x.DriveLetter;

s += ": ";

if (x.DriveType == 3) n = x.ShareName;

else if (x.IsReady) n = x.VolumeName;

else n = "<font color=red>[Other Drive-not intialized]</font>";

s += n + "<br>";

e.moveNext();

} while (!e.atEnd());



return (s);

}



</SCRIPT>

</HEAD>



<BODY>

<P>

<h1>The following is the list of available drives on Client system:</h1>

<SCRIPT language=JavaScript> document.write(getClientDrives());</SCRIPT>

</P>

</BODY>

</HTML>

Sunday, November 23, 2008

ref - out : at a glance

The ref


The ref keyword causes argument passed by reference. The effect is that any chnages is made to the parameter in the method will be reflected in that variable when control passes back to the calling method. To use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword.

An argument passed to to a ref parameter must first be initialized. his differs it from out whose argument need not be explicitly initialize before being passed.

Both ref and out are treated differently at runtime, but treated the same at compilation. Therefore, methods can't be overloaded if one method takes a ref keyword and the other takes an out argument.

Exaple:

class refpara
{
static void refMath(ref String STR)
{
STR = "Hi!";
}
static void Main()
{
String str = "Hello!";
refMath(ref str);
}
}


The out



The out keyword also causes argument to be passed by reference. There no need to initialize the out variable as it requires in case of ref variable.

Exaple:

class refpara
{
static void refMath(out String STR)
{
STR = "Hi!";
}
static void Main()
{
String str = "Hello!";
refMath(out str);
}
}

Sunday, November 16, 2008

ASP.NET MVC – A Step Ahead Series: Part-II

ASP.NET MVC – A Step Ahead Series: Part-II


This article is only for learning purpose and to enhance the ASP.Net MVC skills. The A Step Ahead Series containing few chapters to describe the same.

History


In Part-I we have learnt few basics of the ASP.Net MVC, including features and containing the creation of a sample application. In this entire tutorial we will continue the same application with other concepts.

Introduction


This part of tutorial expands the previously created project. Let’s remind the last tutorial in following steps:

  1. The MVC pattern separates the components of an MVC web application, which allows the more controls of the individual parts of that application.

  2. MVC pattern separates objects into Model, View and Controllers

  3. The MVC pattern helps you create applications that separate the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements. The pattern specifies where each kind of logic should be located in the application. The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model. This separation helps you manage complexity when you build an application, because it enables you to focus on one aspect of the implementation at a time. For example, you can focus on the view without depending on the business logic.

  4. Steps to create an ASP.Net MVC application :

    • Start your Visual Studio2008 : Start ->Programs -> Visual Studio2008

    • On the File menu, click New Project.

    • In the upper-right corner, make sure that .NET Framework 3.5 is selected.

    • Under Project types, expand either Visual Basic or Visual C#, and then click Web [I have opted C#]

    • Under Visual Studio installed templates, select ASP.NET MVC Web Application.

    • In the Name box, enter MVCApplicationStepAhead.

    • In the Location box, enter a name for the project folder.

    • If you want the name of the solution to differ from the project name, enter a name for the solution in the Solution Name box.

    • Select Create directory for solution and click OK



MVCApplicationStepAhead – At a glance


The folder structure of an MVC project differs from that of an ASP.NET Web site project. The MVC project contains the following folders:

  • Content Folder : This folder contains the style sheets. Basically this folder having responsible files for page layout, design etc.

  • Controller Folder : As ot pronounced from name this folder contains the controller files like AccountController.cs and HomeController.cs.

  • Models Folder : Contains Data Model files.

  • Script Folder : Contains Script files.

  • View Folder : For view page files. This folder contains three subfolders: Account, Home, and Shared. The Account folder contains views that are used as UI for logging in and changing passwords. The Home folder contains an Index view (the default starting page for the application) and an About page view. The Shared folder contains the master page view for the application.



Creating the Routes


One of the powerful new features that ASP.NET MVC brings to the table is the ability to customize the URLs that access your application. The URL routing feature explicitly breaks the connection between physical files on disk and the URL that is used to access a given bit of functionality. This is important for Search Engine Optimization as well as general usability of the website.
This is done by creating a route table in the global.asax file in the MVC Application. Luckily for us, the defaults included in the template work perfectly this application.

Finally, we are ready to start creating application:

Scope :


Scope of our Test application is

  • Creation of an Interface for employees to:

  • Defining their roles

  • Getting Roles

  • Checking the availability of an Employee [whether retiree or not]


Requirement :


We need for above

  • A Storage where we put records – The Model

  • An Interface where user can interact – The Viewer

  • A media through which all activities could be inspected – The Controller


Selecting Application :


We have assigned tasks for .Net and we need MVC, so, the best selection of language is ASP.NET MVC.
Now, let’s start creating above one-by-one [I assume that there is no predefined file exists]:

Creation of Model


First o f all as have discussed we want a Container where we store our data. Here What I mean of Container , ofcourse, a Database.
Here are three options for you to choose container of this application:

  1. Go with the existing database file EmployeeDB.mdf under App_data folder of application.

  2. Use the available script EmployeeDB.sql available under App_data folder of application to create above database

  3. Finally, follow bellow mentioned steps to create a new database from / within the environment :

  4. Select App_data folder Choose New From File -> New -> File option , or

  5. Right Click the App_data to add new database file.

  6. Create a Database with name EmployeeDB

  7. Create a Table ERoles following is the query for this :



/*******************************************
*This script creates
****** database EmployeeDB
****** Table Roles
********************************************/

IF EXISTS (SELECT name FROM sys.databases WHERE name = N'EmployeeDB')
DROP DATABASE [EmployeeDB]
Go
CREATE DATABASE [EmployeeDB]
GO

USE [EmployeeDB]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Roles]') AND type in (N'U'))
DROP TABLE [dbo].[Roles]
GO

/********************************************************************
******This script is a part of ASP.NET MVC-A Step Ahead Series
****** Written by : Gaurav Arora
****** Reach author at : webmaster@msdotnetheaven.com
****** Anyone can use this for educational purposes.
**********************************************************************/

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Roles](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Role] [nvarchar](300) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[IsRetiree] [bit] NOT NULL,
[HireDate] [datetime] NOT NULL,
CONSTRAINT [PK_Roles] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO


  • Fill some initial values into new created table either from Environment or Query analyzer :


  • USE [EmployeeDB]
    GO

    INSERT INTO [dbo].[Roles]([Role],[IsRetiree],[HireDate])
    VALUES('Manager - Resource',0,'01/01/2005')
    GO
    INSERT INTO [dbo].[Roles]([Role],[IsRetiree],[HireDate])
    VALUES('Manager - Technical',0,'01/11/1999')
    GO
    INSERT INTO [dbo].[Roles]([Role],[IsRetiree],[HireDate])
    VALUES('Member - Technical Staff',0,'01/11/2007')
    GO
    INSERT INTO [dbo].[Roles]([Role],[IsRetiree],[HireDate])
    VALUES('Sr. Member - Technical Staff',0,'11/11/2007')
    GO
    INSERT INTO [dbo].[Roles]([Role],[IsRetiree],[HireDate])
    VALUES('Lead Member - Technical Staff',1,'11/11/1986')
    GO

    Alternatively, you can also used available stored procedures. I am not discussing about Stored Procedure here.
    Finally, you have a database with table.
    Note: You can change the name of above objects as per your feasibility.
    With the invention of LINQ, its very easy to solve the smut problems in applications. Here to defining a Model.
    There are many tools to generate this task like :

    1. LINQ To SQL

    2. nHibernet

    3. Entity Framework



    I prefer to use LINQ to SQL, although above three are likely to be same but I always use LINQ to SQL.

    To do the same :

    Right click on Models folder and select Add New Item -> LINQ To SQL -> EmpRole.dbml

    Now, just pick and drop your object of Container i.e. ERoles table on Method Pane.
    Alternatively, you can create a new class from tool box by defining all the elements.
    Finally, click on ‘Save’ when done.

    Many author says that the next step should be adding the logics but my opinion is you should have at-least one interface to interact user then may go for logics.

    So, lets start to create a UI:

    Creation of Views


    Right Click on Home folder under Views folder to add new Views.
    Select Add -> View Create.aspx, Home.aspx these are the two views we need here.

    Now Double click on Index.aspx and put following lines there:

    First include namespace:


    <%@ Import Namespace="MVCApplicationStepAhead.Models" %>


    Now, rewrite the above with following code:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Employee Roles</title>
    </head>
    <body>
    <div>
    <h1>
    Employee Roles</h1>
    <ul>
    <%foreach (RoleE roleE in (IEnumerable)ViewData.Model)
    {%>
    <li>
    <%if (roleE.IsRetiree)
    { %>
    <font color="red">
    <%= roleE.HireDate.ToShortDateString() %>
    --
    <%= roleE.Role1%>
    </font>
    <%}
    else
    { %>
    <a href="/Home/Complete/<%=roleE.Id.ToString() %>" >Change Role as Retiree</a>
    <%= roleE.HireDate.ToShortDateString() %>
    --
    <%= roleE.Role1 %>
    <% } %>
    </li>
    <% } %>
    </ul>
    <br /><br />
    <a href="/Home/Create">Add New Task</a>
    </div>
    </body>
    </html>


    The above piece of code – just defining the activities of getting Available roles of Employee, adding new roles with Employee, changing Employee current roles.
    In Create.aspx page we pull some new roles, use following code:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Create.aspx.cs" Inherits="MVCApplicationStepAhead.Views.Home.Create" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <div>
    <h1>
    Add New Role</h1>
    <form method="post" action="/Home/CreateNew">
    <label for="role">
    Role: </label>
    <input type="text" name="role" />
    <br />
    <input type="submit" value="Add Task" />
    </form>
    </div>
    </body>
    </html>



    As you can check here we are using postmethod.
    Do not confuse with both above two pages, very soon you will get all the answers of your queries:

    Creation of Controllers:


    Click on Controllers folder and add new MVC controller, named as HomeController.cs

    Put the following lines there:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;
    using MVCApplicationStepAhead.Models;

    namespace MVCApplicationStepAhead.Controllers
    {
    public class HomeController : Controller
    {
    private EmpRoleDataContext dbEmpRole = new EmpRoleDataContext();

    //Display a list of Roles
    public ActionResult Index()
    {
    var roles = from r in dbEmpRole.RoleEs orderby r.HireDate descending select r;

    //return View();
    return View(roles.ToList());
    }

    //New Role form
    public ActionResult Create()
    {
    return View();
    }

    //Adding new Roles

    public ActionResult CreateNew(string roleDescription)
    {
    //New role to database
    RoleE newRole = new RoleE();
    newRole.Role1 = roleDescription;
    newRole.IsRetiree = false;
    newRole.HireDate=DateTime.Now;

    dbEmpRole.RoleEs.InsertOnSubmit(newRole);

    return RedirectToAction("Index");
    }

    //Mark that Role has been Completed
    public ActionResult Complete(int Id)
    {
    //database tasks here
    var roles = from r in dbEmpRole.RoleEs where r.Id == Id select r;

    foreach (RoleE match in roles)
    match.IsRetiree = true;

    dbEmpRole.SubmitChanges();

    return RedirectToAction("Index");
    }

    }
    }

    Finally, we reach to conclusion:
    Now you can build application or run application:
    Start the above application by pressing F5
    Add some new roles, change roles to retiree etc.
    Conclusion

    The ASP.NET MVC provides us a huge availability to represent our application in of MVC manner:
    The folder structure of an MVC project differs from that of an ASP.NET Web site project. The MVC project contains the following folders:

    • Content Folder : This folder contains the style sheets. Basically this folder having responsible files for page layout, design etc.

    • Controller Folder : As ot pronounced from name this folder contains the controller files like AccountController.cs and HomeController.cs.

    • Models Folder : Contains Data Model files.

    • Script Folder : Contains Script files.

    • View Folder : For view page files. This folder contains three subfolders: Account, Home, and Shared. The Account folder contains views that are used as UI for logging in and changing passwords. The Home folder contains an Index view (the default starting page for the application) and an About page view. The Shared folder contains the master page view for the application.


    This all above for Tutorial Part – II.
    We will include some advance topics in our next tutorials.