Search This Blog

2009-05-31

C# 3.0 Tutorial -2:Extension Methods

In the beginning – that is, in C# 1.0 – the methods belonging to a class were defined within the body of the class. C# 2.0 changed that somewhat. By introducing partial classes, the methods making up a class could be defined in more than one place. However, they were all collected together at compile time, so it was nothing particularly new. Anonymous methods were something new, however. Conceptually, anonymous methods are associated with no class at all. C# 3.0 continues the journey with extension methods.
Extension methods are associated with a class (either a specific one or they are generic and can work for any class). What makes them different from normal instance methods is that they are not defined within the class itself. Instead, they are defined in some other static class. This all sounds rather strange, so let’s take an example.
Imagine that we had got some third party class library for computing a serial number for a product from a product ID and some user details. The method may take the arguments ProductID, CustomerName, CustomerDateOfBirth, and CustomerCountry. In the class library, its implementation may look something like this:

public sealed class CodeGenerator
{
public long generateCode(int ProductID, string CustomerName, DateTime CustmerDOB, string CustomerCountry)
{
return (ProductID % 42) + CustomerName.GetHashCode() * (CustmerDOB.Ticks - CustomerCountry.GetHashCode());

}
}

In our application, however, we pass around user data in a structure.

struct UserInfo
{
public int UserID;
public string Name;
public DateTime DOB;
public string CountryCode;
}

Obj is used in two ways. The first is that it refers to the v-table. This can be used to look up the method to call (in the case of method overriding). The second use is that it is passed as the first parameter to the method. This parameter is taken and becomes the "this" variable – this is what the CLR does under the hood. So essentially, all of your instance methods have an implicit first parameter that the C# compiler inserts for you and provides access to through the "this" variable.

Second, there is an alternative way of thinking about object orientation: rather than having a class-based system, you can use multi-methods. Multi-methods are very similar to method overloading in C# - you can have methods with the same name but different signatures, and the right one is invoked depending on the parameters that are used in the call. Now imagine that the implicit first parameter that we just discussed is not implicit, but instead you place it at the start of every method's parameter list. The type of that first parameter can be a class name. When deciding which method to call, the full signature (including the type of the invocant – the first parameter) is taken into account. That means that you can write methods for a particular class anywhere you like in your program, or extend existing classes with your own methods.

Sometimes the object a method is being called on is referred to as the invocant. For example, in obj.method(), obj is the invocant.If you can get your head around those ideas, then you will find extension methods fairly straightforward. An extension method is a static method, but with the first parameter it takes being explicitly marked as receiving the object that the method was called on. Extension methods may only appear in static classes. Returning to our example, we can implement an extension method like this:

static class MyExtensions
{
public static long generateCode(this CodeGenerator CG, int ProductID, UserInfo User)
{
return 2 + 3;
}
public static long generateCode(this CodeGenerator CG)
{
return 2 + 3 + 4;
}

}

Now run the following code from your page load event
CodeGenerator CG = new CodeGenerator();
UserInfo User = new UserInfo();
User.UserID = 453;
User.Name = "Manab";
User.DOB = DateTime.Now;
User.CountryCode = "UK";


long Code = CG.generateCode(5183, "Manab", DateTime.Now, "UK");
long Code2 = CG.generateCode(5183, User);
long Code3 = CG.generateCode();
Response.Write(Code + "<br/>");
Response.Write(Code2 + "<br/>");
Response.Write(Code3 + "<br/>");

Output :
4605813284442487372
5
9

Another question that comes up is that of performance. Does the runtime have to locate the method to call at runtime, or is it worked out at compile time? The answer is that it is decided at compile time, so there is not any runtime dispatch overhead. In fact, the lookup doesn't even go through the v-table, so it may well be faster than some instance method calls. (For those who like to have the details, the .Net CLR provides an instruction, named "call", for calling a method without consulting the v-table, which the compiler can use instead of the standard one when it knows that it is safe to do so).

Perhaps the most important question, though, is when extension methods should be used. The C# designers themselves say that they are not something you want to be using all of the time, but rather in situations where instance methods are unable to provide what you need.

If you have a class and want to extend it with some special functionality that is specific to your usage of it, you now have two options.
Inherit from it and put the special functionality in the sub class
Write extension methods
The first option is preferable. Inheritance is well understood by other programmers, it is clear what method will be called and you won't have to repeatedly write the class name when writing the methods. So why the second option? I can think of a few cases.
If the class is sealed, you will be unable to inherit from it. In this case, you have no option but to use extension methods.
You may be using some kind of object factory that instantiates objects of a given class, but you do not have the ability to modify it so that your subclass is instantiated instead. Therefore extension methods are the only way to extend the functionality of these objects.
You want to implement a method that can be invoked on all classes implementing a given interface; before, we had no way to attach a method implementation to an interface.
You may want to add related things to a number of other classes, and from a software engineering point of view it may be better to collect those together in one place rather than spreading them amongst many classes.
If you find yourself writing extension methods every day, then that's probably a bad sign. It's a myth that every language feature is intended to be used equally often, both in real and programming languages.


A complete code is as follows.I have write this code in the aspx page of an Web application

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
public partial class CSharp3_ExtensionMethods : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CodeGenerator CG = new CodeGenerator();
UserInfo User = new UserInfo();
User.UserID = 453;
User.Name = "Manab";
User.DOB = DateTime.Now;
User.CountryCode = "UK";


long Code = CG.generateCode(5183, "Manab", DateTime.Now, "UK");
long Code2 = CG.generateCode(5183, User);
long Code3 = CG.generateCode();
Response.Write(Code + "<br/>");
Response.Write(Code2 + "<br/>");
Response.Write(Code3 + "<br/>");



A a1 = new A();
Response.Write("a1.useMe():" + a1.useMe() + "<br/>");

B b1 = new B();
Response.Write("b1.useMe():" + b1.useMe() + "<br/>");
Response.Write("b1.useMe(manab):" + b1.useMe("manab") + "<br/>");

A a2 = new A();
Response.Write("a2.useMe(manab) :" + a2.useMe("manab") + "<br/>");
}
}
struct UserInfo
{
public int UserID;
public string Name;
public DateTime DOB;
public string CountryCode;
}
public sealed class CodeGenerator
{
public long generateCode(int ProductID, string CustomerName, DateTime CustmerDOB, string CustomerCountry)
{
return (ProductID % 42) + CustomerName.GetHashCode() * (CustmerDOB.Ticks - CustomerCountry.GetHashCode());

}
}
static class MyExtensions
{
public static long generateCode(this CodeGenerator CG, int ProductID, UserInfo User)
{
return 2 + 3;
}
public static long generateCode(this CodeGenerator CG)
{
return 2 + 3 + 4;
}

}
//Create another example of extensions as belows
public class A
{
public string useMe()
{
return "I am in Class A ->useMe()";
}
}
public class B : A
{
/* public string useMe()
{
return "I am in Class B ->useMe()";
}*/
}
static class C
{
public static string useMe(this A a1, string str1)
{
return "I am in Class C ->useMe(this A a1,string str1)";
}
}
public class Jungle
{
public string Add(Monkey mn)
{
return mn.HelloMonkey();
}
}
public class Monkey
{
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public string HelloMonkey()
{
return Name;
}
}

Output is as follows:

4605813284442487372
5
9
a1.useMe():I am in Class A ->useMe()
b1.useMe():I am in Class A ->useMe()
b1.useMe(manab):I am in Class C ->useMe(this A a1,string str1)
a2.useMe(manab) :I am in Class C ->useMe(this A a1,string str1)


tags:what is Extension Methods in C# 3.0/3.5,how can we implement Extension Methods in c# 3.0/3.5

C# 3.0 Tutorial -1:Var Keyword

Var keyword

you can now declare a veriable with the "var" keyword where you dont need to mention the datatype

var Hello = "hello Programmer";
var my number = 42;

At first glance, it appears that you’re declaring a variable of type “var”. Actually, "var" is not a type, but rather a new keyword that means, “I want to declare a variable, but I’m too lazy to write out its type”. For cases where the type is "Dictionary<int, List<int>>" or similar, this saves quite a bit of clutter in the code:
// Before:
Dictionary<int, List<int>> Coeffs = new Dictionary<int, List<int>>();
// After:
var Coeffs = new Dictionary<int, List<int>>();

Following is a code snippent of Var keyword (code is done within an aspx page of a WebSite application)

using System;
using System.Data;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class CSharp3_VarKeyword : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/*var name = 22; name = "manab";//will throws error or var name;name="Manab"; will also throws error*/
var name = "Manab";
var answer = 42;
var prog = new DataSet();
var Friends = new List<string>();
var coeffs = new Dictionary<int, List<string>>();
var array = new[] {2,3,4,5,6,7 };
Response.Write("name is of type " + name.GetType().Name + "<br/>");
Response.Write("answer is of type " + answer.GetType().Name + "<br/>");
Response.Write("prog is of type " + prog.GetType().Name + "<br/>");
Response.Write("Friends is of type " + Friends.GetType().Name + "<br/>");
Response.Write("coeffs is of type " + coeffs.GetType().Name + "<br/>");
Response.Write("array is of type " + array.GetType().Name + "<br/>");
Response.Write("array element is of type " + array[2].GetType().Name + "<br/>");

bool condition = false;
var setCondionality =condition? new a():new b();
Response.Write("set Condionality is " + setCondionality.GetType().Name);
}

public class a { }
public class b :a{ }


}

And the output of the above prograp is as follows

name is of type String
answer is of type Int32
prog is of type DataSet
Friends is of type List`1
coeffs is of type Dictionary`2
array is of type Int32[]
array element is of type Int32
set Condionality is b


I consider that an improvement, both to developer productivity when writing code and to those reading the code too.

tags:what is var keyword in 3.0/3.5

2009-05-27

Call Master page event in Content page

A challenge developers may have using ASP.NET 2.0's master pages and content pages is the ability to notify content pages that something has happened in the master page that may be of interest inside the content page. For this example, let's assume our master page holds a list of buttons, and your content page needs to be notified that a new item is selected.
To solve this, we'll create a delegate.Add this outside of the code behind class to expose it to the content forms.Think of a delegate as a notification system that is avaiable for all to use.When the Master Page's button is clicked, its regular click event fires. Inside this event, the delegate is called to tell any subscribing entities that something great has happened, and they should react.

Code Snippet:
Here's how our master page is set up:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id="wrapper">
<div id="header">
Header Area
</div>
<div id="menuArea">
<asp:Button ID="btnMenu1" runat="server" Text="Menu 1" OnClick="btnMenu1_Click" />
<asp:Button ID="btnMenu2" runat="server" Text="Menu 2" OnClick="btnMenu2_Click" />
</div>
<div id="mainContent">
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
<div id="footer">
Footer Area
</div>
</div>
</form>
</body>
</html>

And the code behind class for the Master Page:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

#region Public Delegates

// Expose delegates for Master Page Events
public delegate void MasterPageMenuClickHandler(object sender, System.EventArgs e);

#endregion Public Delegates

public partial class MasterPage : System.Web.UI.MasterPage
{
#region Public Properties

private string _currentButton;

public string CurrentButton
{
get { return _currentButton; }
set { _currentButton = value; }
}

#endregion Public Properties

#region Public Events

public event MasterPageMenuClickHandler MenuButton;

#endregion Public Events

#region Page Events

///
/// Handles Page Load Event
///

///
///
protected void Page_Load(object sender, EventArgs e)
{
}

///
/// Handles Button 1 Click Event
///

///
///
protected void btnMenu1_Click(object sender, EventArgs e)
{
// Assign value to public property
_currentButton = "One";

// Fire event to existing delegates
OnMenuButton(e);
}

///
/// Handles Button 2 Click Event
///

///
///
protected void btnMenu2_Click(object sender, EventArgs e)
{
// Assign value to public property
_currentButton = "Two";

// Fire event to existing delegates
OnMenuButton(e);
}

#endregion Page Events

#region Virtual Methods

///
/// Invokes subscribed delegates to menu click event.
///

/// Click Event arguments
protected virtual void OnMenuButton(EventArgs e)
{
if (MenuButton != null)
{
//Invokes the delegates.
MenuButton(this, e);
}
}

#endregion Virtual Methods

}


Now, subscribing to this delegate is easy. In the Page Load of the content page, we simply add an event handler to the public event you created in the master page:

Master.MenuButton +=new MasterPageMenuClickHandler(Master_MenuButton);
One important item to note: to reference Master in this fashion, you'll have to add a MasterType directive to the top of the content page's aspx page.

<%@ MasterType virtualPath="~/MasterPage.master"%>

Delegates are so useful because they allow a class to notify others of an event, without being tied in any way to the subscribing forms. Here's the default.aspx content page code behind:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
/// <summary>
/// Handles Page Load Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
Master.MenuButton +=new MasterPageMenuClickHandler(Master_MenuButton);
}

/// <summary>
/// Reacts to Master Page menu click event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Master_MenuButton(object sender, EventArgs e)
{
lblOutput.Text = string.Format("Button {0} was pressed", Master.CurrentButton.ToString());
}
}


And that's how easy it is.

Parsing Querystring using Javascript

Following is the code snippent of javascript by which we can parse the Query string

Suppose we are going to pass the name and email from the parent or source page as the query string and the destination page supposed to be "parseQueryString.htm".
From the Parent page or the source page write the following code

<input type="text" name="txtName" id="txtName">Name<br>
<input type="text" name="txtEmail" id="txtEmail">Email<BR>
<input type="button" value="Submit" onclick="getURL(txtName.value, txtEmail.value);">
<script>
function getURL(sName, sEmail){
window.location ="parseQueryString.htm?name="+sName+"&email="+sEmail;
}
</script>

In the destination page "parseQueryString.htm" write the following code and call it from body onload as follows
<script>
function PageQuery(q) {
if(q.length > 1) this.q = q.substring(1, q.length);
else this.q = null;
this.keyValuePairs = new Array();
if(q) {
for(var i=0; i < this.q.split("&").length; i++) {
this.keyValuePairs[i] = this.q.split("&")[i];
}
}
this.getKeyValuePairs = function() { return this.keyValuePairs; }
this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length; j++) {
if(this.keyValuePairs[j].split("=")[0] == s)
return this.keyValuePairs[j].split("=")[1];
}
return false;
}
this.getParameters = function() {
var a = new Array(this.getLength());
for(var j=0; j < this.keyValuePairs.length; j++) {
a[j] = this.keyValuePairs[j].split("=")[0];
}
return a;
}
this.getLength = function() { return this.keyValuePairs.length; }
}
function queryString(key){
var page = new PageQuery(window.location.search);
return unescape(page.getValue(key));
}
function displayItem(key){
if(queryString(key)=='false')
{
result.innerHTML="you didn't enter a ?name=value querystring item.";
}else{
result.innerHTML+=queryString(key)+"<BR>";
}
}
</script>


<body onload="displayItem('name'); displayItem('email');">

<div id=result></div>

</body>

Tutorial 8-UML Diagram:Component and deployment diagrams

8.Component and deployment diagrams
A component is a code module. Component diagrams are physical analogs of class diagram. Deployment diagrams show the physical configurations of software and hardware.

The following deployment diagram shows the relationships among software and hardware components involved in real estate transactions.



The physical hardware is made up of nodes. Each component belongs on a node. Components are shown as rectangles with two tabs at the upper left.

Tutorial 7-UML Diagram:Activity diagrams

7.Activity diagrams
An activity diagram is essentially a fancy flowchart. Activity diagrams and statechart diagrams are related. While a statechart diagram focuses attention on an object undergoing a process (or on a process as an object), an activity diagram focuses on the flow of activities involved in a single process. The activity diagram shows the how those activities depend on one another.

For our example, we used the following process.

"Withdraw money from a bank account through an ATM."

The three involved classes (people, etc.) of the activity are Customer, ATM, and Bank. The process begins at the black start circle at the top and ends at the concentric white/black stop circles at the bottom. The activities are rounded rectangles.

Activity diagrams can be divided into object swimlanes that determine which object is responsible for which activity. A single transition comes out of each activity, connecting it to the next activity.

A transition may branch into two or more mutually exclusive transitions. Guard expressions (inside [ ]) label the transitions coming out of a branch. A branch and its subsequent merge marking the end of the branch appear in the diagram as hollow diamonds.

A transition may fork into two or more parallel activities. The fork and the subsequent join of the threads coming out of the fork appear in the diagram as solid bars.

Tutorial 6-UML Diagram:Statechart diagrams

6.Statechart diagrams

Objects have behaviors and state. The state of an object depends on its current activity or condition. A statechart diagram shows the possible states of the object and the transitions that cause a change in state.

Our example diagram models the login part of an online banking system. Logging in consists of entering a valid social security number and personal id number, then submitting the information for validation.

Logging in can be factored into four non-overlapping states: Getting SSN, Getting PIN, Validating, and Rejecting. From each state comes a complete set of transitions that determine the subsequent state.

States are rounded rectangles. Transitions are arrows from one state to another. Events or conditions that trigger transitions are written beside the arrows. Our diagram has two self-transition, one on Getting SSN and another on Getting PIN.

The initial state (black circle) is a dummy to start the action. Final states are also dummy states that terminate the action.

The action that occurs as a result of an event or condition is expressed in the form /action. While in its Validating state, the object does not wait for an outside event to trigger a transition. Instead, it performs an activity. The result of that activity determines its subsequent state.

Tutorial 5-UML Diagram:Collaboration diagrams

5.Collaboration diagrams
Collaboration diagrams are also interaction diagrams. They convey the same information as sequence diagrams, but they focus on object roles instead of the times that messages are sent. In a sequence diagram, object roles are the vertices and messages are the connecting links.

The object-role rectangles are labeled with either class or object names (or both). Class names are preceded by colons ( : ).

Each message in a collaboration diagram has a sequence number. The top-level message is numbered 1. Messages at the same level (sent during the same call) have the same decimal prefix but suffixes of 1, 2, etc. according to when they occur.

Tutorial 4-UML Diagram:Sequence diagrams

4.Sequence diagrams
Class and object diagrams are static model views. Interaction diagrams are dynamic. They describe how objects collaborate.

A sequence diagram is an interaction diagram that details how operations are carried out -- what messages are sent and when. Sequence diagrams are organized according to time. The time progresses as you go down the page. The objects involved in the operation are listed from left to right according to when they take part in the message sequence.

Below is a sequence diagram for making a hotel reservation. The object initiating the sequence of messages is a Reservation window.


The Reservation window sends a makeReservation() message to a HotelChain. The HotelChain then sends a makeReservation() message to a Hotel. If the Hotel has available rooms, then it makes a Reservation and a Confirmation.

Each vertical dotted line is a lifeline, representing the time that an object exists. Each arrow is a message call. An arrow goes from the sender to the top of the activation bar of the message on the receiver's lifeline. The activation bar represents the duration of execution of the message.

In our diagram, the Hotel issues a self call to determine if a room is available. If so, then the Hotel creates a Reservation and a Confirmation. The asterisk on the self call means iteration (to make sure there is available room for each day of the stay in the hotel). The expression in square brackets, [ ], is a condition.

The diagram has a clarifying note, which is text inside a dog-eared rectangle. Notes can be put into any kind of UML diagram.

Tutorial 3-UML Diagram:object diagrams

3.Packages and object diagrams
To simplify complex class diagrams, you can group classes into packages. A package is a collection of logically related UML elements. The diagram below is a business model in which the classes are grouped into packages.


Packages appear as rectangles with small tabs at the top. The package name is on the tab or inside the rectangle. The dotted arrows are dependencies. One package depends on another if changes in the other could possibly force changes in the first.

Object diagrams show instances instead of classes. They are useful for explaining small pieces with complicated relationships, especially recursive relationships.
This small class diagram shows that a university Department can contain lots of other Departments.



The object diagram below instantiates the class diagram, replacing it by a concrete example.



Each rectangle in the object diagram corresponds to a single instance. Instance names are underlined in UML diagrams. Class or instance names may be omitted from object diagrams as long as the diagram meaning is still clear.

Tutorial 2-UML Diagram:Class diagrams

2.Class Diagram
A Class diagram gives an overview of a system by showing its classes and the relationships among them. Class diagrams are static -- they display what interacts but not what happens when they do interact.
The class diagram below models a customer order from a retail catalog. The central class is the Order. Associated with it are the Customer making the purchase and the Payment. A Payment is one of three kinds: Cash, Check, or Credit. The order contains OrderDetails (line items), each with its associated Item.



UML class notation is a rectangle divided into three parts: class name, attributes, and operations. Names of abstract classes, such as Payment, are in italics. Relationships between classes are the connecting links.
Our class diagram has three kinds of relationships.
a)association -- a relationship between instances of the two classes. There is an association between two classes if an instance of one class must know about the other in order to perform its work. In a diagram, an association is a link connecting two classes.
b)aggregation -- an association in which one class belongs to a collection. An aggregation has a diamond end pointing to the part containing the whole. In our diagram, Order has a collection of OrderDetails.
c)generalization -- an inheritance link indicating one class is a superclass of the other. A generalization has a triangle pointing to the superclass. Payment is a superclass of Cash, Check, and Credit.
An association has two ends. An end may have a role name to clarify the nature of the association. For example, an OrderDetail is a line item of each Order.
A navigability arrow on an association shows which direction the association can be traversed or queried. An OrderDetail can be queried about its Item, but not the other way around. The arrow also lets you know who "owns" the association's implementation; in this case, OrderDetail has an Item. Associations with no navigability arrows are bi-directional.
The multiplicity of an association end is the number of possible instances of the class associated with a single instance of the other end. Multiplicities are single numbers or ranges of numbers. In our example, there can be only one Customer for each Order, but a Customer can have any number of Orders.

This table gives the most common multiplicities.

0..1 =zero or one instance. The notation n . . m indicates n to m instances.
0..* or * =no limit on the number of instances (including none).
1 =exactly one instance
1..*= at least one instance


Every class diagram has classes, associations, and multiplicities. Navigability and roles are optional .


Difference between Composition and Aggregation
Composition:

As we know, inheritance gives us an 'is-a' relationship. To make the understanding of composition easier, we can say that composition gives us a 'part-of' relationship. Composition is shown on a UML diagram as a filled diamond

If we were going to model a car, it would make sense to say that an engine is part-of a car. Within composition, the lifetime of the part (Engine) is managed by the whole (Car), in other words, when Car is destroyed, Engine is destroyed along with it. So how do we express this in C#?

public class Engine
{
. . .
}

public class Car

{

Engine e = new Engine();

.......

}

As you can see in the example code above, Car manages the lifetime of Engine.

Aggregation:
The CRM system has a database of customers and a separate database that holds all addresses within a geographic area. Aggregation would make sense in this situation, as a Customer 'has-a' Address. It wouldn't make sense to say that an Address is 'part-of' the Customer, because it isn't. Consider it this way, if the customer ceases to exist, does the address? I would argue that it does not cease to exist. Aggregation is shown on a UML diagram as an unfilled diamond


So how do we express the concept of aggregation in C#? Well, it's a little different to composition. Consider the following code:

public class Address
{
. . .
}

public class Person

{

private Address address;

public Person(Address address)

{

this.address = address;

}

. . .

}

Person would then be used as follows:

Address address = new Address();
Person person = new Person(address);

or

Person person = new Person( new Address() );

As you can see, Person does not manage the lifetime of Address. If Person is destroyed, the Address still exists. This scenario does map quite nicely to the real world.

Tutorial 1-UML Diagram:Use case

This tutorial provides a quick introduction to the Unified Modeling Language.
At the center of the UML are its nine kinds of modeling diagrams, which we describe here.
1.Use case diagrams
2.Class diagrams
3.Object diagrams
4.Sequence diagrams
5.Collaboration diagrams
6.Statechart diagrams
7.Activity diagrams
8.Component diagrams
9.Deployment diagrams

The UML is applicable to object-oriented problem solving. Anyone interested in learning UML must be familiar with the underlying tenet of object-oriented problem solving -- it all begins with the construction of a model.
A model is an abstraction of the underlying problem. The domain is the actual world from which the problem comes.
Models consist of objects that interact by sending each other messages. Think of an object as "alive." Objects have things they know (attributes) and things they can do (behaviors or operations). The values of an object's attributes determine its state.
Classes are the "blueprints" for objects. A class wraps attributes (data) and behaviors (methods or functions) into a single distinct entity. Objects are instances of classes.

1.Use case diagrams
Use case diagrams describe what a system does from the standpoint of an external observer. The emphasis is on what a system does rather than how.
Use case diagrams are closely connected to scenarios. A scenario is an example of what happens when someone interacts with the system. Here is a scenario for a medical clinic.

Scenario :"A patient calls the clinic to make an appointment for a yearly checkup. The receptionist finds the nearest empty time slot in the appointment book and schedules the appointment for that time slot. "
A use case is a summary of scenarios for a single task or goal. An actor is who or what initiates the events involved in that task. Actors are simply roles that people or objects play. The picture below is a Make Appointment use case for the medical clinic. The actor is a Patient. The connection between actor and use case is a communication association (or communication for short).



Actors are stick figures. Use cases are ovals. Communications are lines that link actors to use cases.

A use case diagram is a collection of actors, use cases, and their communications. We've put Make Appointment as part of a diagram with four actors and four use cases. Notice that a single use case can have multiple actors.

Cost Estimation using use case points

Quotation is one of the important aspect of software cycle. Any prediction less or more will affect the project a lot. Lets look at how basically day to day business manage there way of handling quotations.
Acronyms and Abbreviation:
Note: Have these acronyms in hand always as they are used through out the tutorial. Do not be tensed by the acronyms below they are for reference sake and as you proceed ahead with the tutorial you will have more clear picture what exactly they are.


Assumptions:
Readers have basic knowledge of how to write uses cases. This tutorial does not cover use cases and is only limited to estimation by "Use Cases".So if you are reading this article with out the cleared concept of Actor,Role,Scenarios.Its better not to read.
Use case structure varies from organization to organization which can seriously impact the Estimation. This tutorial has used Alistair Cockburn's template from Writing Effective Use Cases (**PRE-PUB. DRAFT#3**): Alistair Cockburn Humans and technology.

  • Scope:
    This tutorial is only till understanding Use Case points and does not get in to details of how to write use cases. This article will not concentrate on how to write uses cases.
  • Steps for UCP(Use Case Points) estimation
  • Step 1. Determine the UAW (Unadjusted Actor weight) : The first step is to classify all the actors in to following classification. Table 2.0 will give you clear idea of how to classify the actors. Second column is the litmus test for making decision which type of actor falls in which category. The last column provides the factor of complexity .



Step 2.Determine number of UUCW (Unadjusted Use case Weight): The second step is to count Use Cases and assign weights depending on number of scenarios and number of transactions.


Step 3.Determine Total UUCP (Unadjusted Use Case Point) : Total UUCP = Total UAW + Total UUCW

Step 4.Computing technical and environmental factor: Final step is to take in to account the technical complexity. All technical factor will be assigned a value from 0 to 5 depending on complexity.



Step 5.Equation for Tfactor = sum(T1....T13)

Step 6.TCF(Technical Complexity Factor) : TCF = 0.6 + (0.01 * Tfactor).

Step 7.EF(Environmental Factor): There are other factors like trained staff, motivation of programmers etc which has quiet a decent impact on the cost estimate.



Step 8. Efactor = SUM(e1...e8).

Step 9.Calculating Environmental Factor = EF = 1.4 + (-0.03 * Efactor)

Step 10.AUCP (Adjusted Use Case Points) : Finally calculating the Adjusted Use case points.
AUCP = UUCP * TCF * EF

Step 11.Multiplying by Man/Hours Factor : AUCP * Person/Hours.

Karner[13] proposed a factor of 20 staff hours per use case point for a project estimate.

While sharks states that field experience has shown that effort can range from 15 to 30 hours per use case point.

Schneider and winters proposed number of staff hours per use case point depends on the environmental factors. The number of factors in E1 through E6 that are below 3 are counted and added to the number of factors in E7 through E8 that are above 3. If the total is 2 or less, the general idea is to use twenty staff hours per UCP; if the total is 3 or 4, use twenty-eight staff hours per UCP.If the number exceeds 5, it is usually recommended that changes should be made to the project so the number can be adjusted because, in this case, the risk is unacceptably high. Another possibility is to increase the number of staff hours to thirty-six per use case point.

Sample project scope (Sample Data entry project):

  • Lets start with a sample fiction project.Heres the scope of the project. MRB company till now was using manual way of maintaining its customer database and there credit card information. Data entry operator manually validates credit card information from external payment gateway. They maintain Customer Code, Customer Name, Customer Address, Customer phone and validated Customer Credit card information in Customer registry. Customer Code is unique for a customer So MRB manually check for the validations and enters in the customer registry.MRB wants the data entry project to be automated.MRB is looking for the following automation:
  • Customer Code assigned should be checked for uniqueness automatically.
  • Customer Code should not exceed 8 length.
  • Credit card validation should be automatic for the current System.MRB has already given the API documentation of how to interact with the third party payment system.
  • Credit card length should not exceed more than 10 length.
  • Data entry operator should be able to add/update/Delete customer information.
  • The database will be in the MRB head office and only data entry operators will be allowed to use the Data entry Software.
  • Software should work on Windows platform. At this moment MRB has Windows 2000 client installed in all computers.
  • Writing use case for Sample Data Entry Project:
    I have used Alistair Cockburn's template for the "Use Case point" example. Use Case Template varies from person to person, project to project and organization to organization. I found Alistair's template to be complete so just took it. But there's no hard and fast rule that you have to follow this template. What will matter is what steps you write in the Use Case.
  • Use Case Transactions: It’s an atomic set of activities that are either performed entirely or not all. What is a use case transaction and what’s not just see if the transaction is adding any business value or else do not include it as a transaction. Example the user switches on the computer, user clicks on add button or any GUI is not valid business transaction step. But example the Customer Code is validated for credit card information is a valid business transaction. Use Case points are heavily affected by the way the Actors and its transactions are identified. So Use Case Document should be written by predefined guidelines, uniformly in a project. Just take a meeting with whole project team before starting writing Use Case. As the depth of the Use Case Document will affect estimation by 40%.



Applying Use Case Points:

Let Start Applying Use Case Points to the upper given document.

Determining Unadjusted Use Actor Weights (UAW): In this project we have identified only one actor “Data Entry Operator”. The upper Actor (Data entry operator) is complex as data entry operator will be interacting through GUI.So UAW=3 as per table Table:2.0.
Determine number of UUCW (Unadjusted Use case Weight): There are 12 transactions [Adding also the alternative flows] in Table 6.0 Use Case. So the above Use Case is complex according to Table:3.0.So referring Table:3.0 UUCW=15.
Now calculating the total UUCP = 15 + 3 = 18.
Determining the technical Factor



Depending on the table calculating the Technical Factor: TCF = 0.6 + (0.01 * Tfactor) = 0.6 + (0.01 * 19) = 0.79

Calculating environmental factor

According to [Kirsten Ribu Master of Science Thesis] Environmental factor play very important role in the estimation. A slight variation will increase the use case point by very very drastic amount. Even small adjustments of an environmental factor, for instance by half a point, can make a great difference to the estimate. Difference 3 to 2.5 increased the estimate by 4580 hours, from 10831 to 15411 hours, or 42.3 percent. This means that if the values for the environmental factors are not set correctly, there may be disastrous results -- Sources [Kirsten Ribu Master of Science Thesis] Do see links below.
  • Using formula for calculating EF = 1.4 + (-0.03 * Efactor) = 1.4 + (-0.03 * 23.5) = 0.695
  • Calculating AUCP = UUCP * TCF * EF = 18 X 0.79 X 0.695 = 9.88 approx = 10 Use Case Points.I have out the approximation as its only creates 3 to 4 hours of difference.
  • Calculating According to Karner i.e 20 staff hours per use case points = 10 X 20 = 200 hours for the total project.
  • If programmer works for 8 hours for a day then 200/8 = 25 days.
  • Calculating according to Schneider and winters from e1 to e6 there are only 3 properties that are below 3.and from e7 to e8 there are none value above 3.So the total is 3 so we use 28 staff hours.10 X 28 = 280 hours. If programmer works for 8 hours then 35 days. If this step is not understood look at the steps defined in theory of use case points.
  • If we apply sixth sense we will find karner approach is coming to round about figure. It really depends what you want to follow karner or Schneider approach. Best is that after 2 or 3 projects what’s ever is coming accurate from history take that approach. Best approach is to use excel and incorporate formulae's properly.

    Final quotation:

    So here the final quotation to the scope defined and the use case document.




    In this quotation I have taken karners value that’s 25 days. One programmer will sit on the project with around 1000 $ salary. So his 25 days salary comes to 840 dollars approx. The upper quotation format is in its simplest format. Every company has his quotation format accordingly. So no hard and fast rule of quotation template.But still if interested http://www.microsoft.com/mac/resources/templates.aspx?pid=templates has good collection of decent templates.

    Use-Case structure Matters :
    The structure of use-case matters a lot. According to (Bente Anda,Hege Dreiem,Dag I.K Sjoberg and Magne jorgensen) the following aspects of structure has an impact :
    The use of generalization between actors : If two actors have 80 percent in common put them in generalization relationship and count them only once. This will increase the accurate of the estimate.
    The use of included and extending use case :Karner recommends that included and extending use case should not be counted. But according to (Bente Anda,Hege Dreiem,Dag I.K Sjoberg and Magne Jorgensen) have a different opinion in there practical experience. In some use cases they found include and extended use cases as essential functionalities and reducing them will reduce steps and hence the estimation.
    The level of detail in the use case description. ( This is dependent on the guy who writes the use case )The level of detail and transaction in use case impact the use-case estimation a lot. If you see the upper use-case if I had written steps like user switches on the PC etc.The transaction would have increased and hence estimation. So if that transaction step is not adding business value do not add it as transaction step. This will also increase the estimation to a good level.Including the above recommendation by Karner and (Bente Anda,Hege Dreiem,Dag I.K Sjoberg and Magne jorgensen) here are also my inputs which can be followed to make a estimate better :-
    Use-Case Splitting and MergingSimple Use-Case masters matter a lot. Writing use-cases for example “Adding Country Master". User can write 3 different use cases for Add, Update, Delete or he can write one use-case and put the Update and delete in alternate scenarios. If the Update and delete do not have different business validations put them in one use-case. During my counting I had seen that accuracy increases if for simple master we put them in one use-case.
    Advantages of Using Use-Case Points
    Automation : Use Case document if structured properly for a company (Uniformly) we can use automation tools. In case of FP this is difficult.
    Disadvantages of Using Use-Case Points
    Can not be used during initial phase: Estimations are normally done at earlier stage of projects. When we say earlier means during the first and second meet with the client. Use case documents are mostly prepared after the project sign off. So during earlier stage this document will not be available. Preparing Use Case document during first and second meet with client means wasting your resources in case you do not get the project. For initial phase of project you can use “Function points”. For function points no formal document is needed. My old tutorial on function points http://www.geocities.com/shiv_koirala/fp/fp.html.
    No Standard Use Case Document:The document structure of use is not standard still. It varies not only from company to company but also from project to project. So the estimation has significant variance according to structure of Use Case documents. Even the writing matter to a large extent. And also how one does identifies use-case and the transaction associated with it. Free textual descriptions may lead ambiguous specification [AP98].
    Maintenance estimation problems:Function point [ http://www.geocities.com/shiv_koirala/fp/fp.html ] failed for maintenance projects. It will be difficult from Use Case Points to do maintenance estimation.
    Actor identification need technical details:In order that the actor be classified we need to know technical details like which protocol the actor will use. So estimation can be done by technical guys.FP is not technical dependent [http://www.geocities.com/shiv_koirala/fp/fp.html].
    Other General Practical Factors
    The below points are not related to Use as such but general while making estimation.
    Change of psychology :Estimator should not be biased. If you are employee of the company do not add extra charge or subtract extra charges. These all things will be handled at the negotiation tables between the software company director and the customer. A estimator job is to show the real cost of the software to the company. In short estimator should not be bothered about negotiation phase and will we get this project or not?. Leave that work to the companies director. And if you are the director of the company think that thing after estimation is over.
    Sixth Sense Approach:Any of the software measurement ways ( Use case, Function points, LOC etc) are evolving and under practice. After looking the figure try to give Sixth sense based on your past experience. Some time estimation will be fair if went the ADHOC way.
    Modular Estimation:In huge projects with 100 of modules its better to estimate modular wise. Example if a client is asking for a customer module, Supplier module and Accounts module. Estimate them differently so that on negotiation table with client you can show him the complete break up. Second this approach is very useful for phase wise estimation. Client can decide either to not take the module (Due to financial concerns) or move it to phases.
    Information Creep and Grey Areas:Estimation are normally done at the initial phase itself probably with one or two meets with client we have to give estimation. So but natural many of the areas there can be creep. The best way for such situation is to think the maximum possibility and estimate. Example if any customer says that he needs chat module and no clarification is made till what the depth it is, estimate to maximum possibility of how can chat application are made. Later during negotiation table show client the estimation basis. So according to the client financial budget changes will be made.
    Other Costing :Any of the Software estimation methodology do not give cost for non-software factors.
    If the software is using any third-party components example crystal reports etc estimate them in a ADHOC way.
    Example if in the project company is also providing web hosting, domain name, hard ware etc put them separately.
    Any training involved estimate them separately.
    Assumptions: As estimation is done at the initial stage there can be lot of creep and gray areas. Due to gray areas estimation has to be supported by proper assumptions.
    Review from Third Party: Before sending the costing to the client review it internally from third person who is not involved in the project.
    Iterations:Iterate it as many as times possible and at various phases. Example use function point to iterate during scoping phase that's initial phase. And Use case Point during the System requirement phase. This gives a good idea before implementing that is the costing proper
    Two teams Estimation:During estimation have two teams which will do the estimation. So that cross verification can be done on the error percent of the estimation.

    Resource:USE CASE Estimation Document written by Shivprasad Koirala

    2009-05-21

    How to know the IP Address of the user who visit my site

    we can track the IP address of the user if when they visit my site.

    Following is the code to know the user IP address

    HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    or
    HttpContext.Current.Request.UserHostAddress;

    To get the IP address of the machine and not the proxy use the following code
    HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    Gridview With Fixed Header

    Gridview can be scrollable if it is put under a div with the overflow property.But if we scroll down the div ,the grid are go down and headeare are also go upword i.e out of the visible portion of div.
    When you apply the following CSS the grid header is visble all the time i.e always in the top of the div.To do this follow the following steps.
    First Create a gridview within a div which will be scrollable.
    <div id="div1" runat="server"
    style="overflow: scroll; width: 100%; height: 200px;">

    <asp:GridView ID="GridView1" runat="server"></asp:GridView>

    </div>


    Create a CSS file "grid.css" within a folder "css" with a class "grid-header".The css folder is under the root directory

    .grid-header
    {
    font-weight: bold;
    font-family: Verdana;
    font-size: 11px;
    background-color: #7A9FCB;
    text-decoration: underline;
    color: #ffffff;
    text-align: left;
    position: relative;
    top:expression(this.parentNode.parentNode.parentNode.scrollTop-2);
    left:expression(this.parentNode.parentNode.parentNode.scrollLeft-1);
    right:1px;
    }


    Call this CSS File from your page which is contain your grid


    <link href="../css/grid.css" rel="stylesheet" type="text/css" />


    call this css from the HeaderStyle of your gridview

    <HeaderStyle CssClass="grid-header"></HeaderStyle>


    Now run your application ,scroll the div to the down,you can see that the grid header is fixed all time



    2009-05-20

    .NET and Flex Integration

    In This article I have tried to describe the integration of Flex Chart with the data that will come from .NET.
    This two platformm integration can be done both by HTTP Service and Web Service.I have used HTTP service in this example.
    To complete these walkthroughs you will need:
    Adobe Flex Builder V2 Beta3 (trial) or Flex SDK V2 Beta3 (free)
    Adobe Flash Player V9 Beta3 (free)
    Microsoft Visual Web Developer 2005 Express Edition or the .NET V2.0 SDK (both free)
    Microsoft Windows

    I have taken one dropdown where the user will select a value and a button which will show the report depending on the selected value of the dropdown.The output will be as follows.



    Step 1.Create a .NET web site
    Open the VS.NET IDE and create a new web site as follows



    Step 2.Create a folder "FlexChart" under the root of the web application

    Step 3.Add two new webform "Default5.aspx" and "ShowChart.aspx"


    Step 4.Modify the aspx page to generate xml output
    Open the Default5.aspx page and go to the HTML code.Remove all code except the first line i.e the page directive.add the following tage at the page directive.

    ContentType="text/xml"

    Now the entire HTML page of Default5.aspx will be as follows

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" ContentType="text/xml" %>

    Step 5.Write the server side code which will generate XML

    open the cs file of Default5.aspx and write the following code in the page load event.

    string strIndicator = (string)Session["Indicator"];
    Response.Expires = 0;
    if (strIndicator == "1900")
    {
    Response.Write("<Profit>");
    Response.Write("<Rate JAN='3.429' FEB='2.030' MAR='1.761'/>");
    Response.Write("<Rate JAN='3.248' FEB='1.975' MAR='1.891'/>");
    Response.Write("<Rate JAN='3.206' FEB='1.901' MAR='2.166'/>");
    Response.Write("<Rate JAN='3.186' FEB='1.703' MAR='2.014'/>");
    Response.Write("<Rate JAN='3.325' FEB='1.616' MAR='2.870'/>");
    Response.Write("<Rate JAN='3.431' FEB='1.310' MAR='2.451'/>");
    Response.Write("<Rate JAN='3.592' FEB='1.110' MAR='1.546'/>");
    Response.Write("<Rate JAN='3.645' FEB='1.274' MAR='0.972'/>");
    Response.Write("<Rate JAN='3.756' FEB='1.539' MAR='0.924'/>");
    Response.Write("</Profit>");
    }
    else
    {
    Response.Write("<Profit>");
    Response.Write("<Rate JAN='9' FEB='0' MAR='1'/>");
    Response.Write("<Rate JAN='8' FEB='5' MAR='1'/>");
    Response.Write("<Rate JAN='6' FEB='1' MAR='6'/>");
    Response.Write("<Rate JAN='6' FEB='3' MAR='4'/>");
    Response.Write("<Rate JAN='5' FEB='6' MAR='0'/>");
    Response.Write("<Rate JAN='1' FEB='0' MAR='1'/>");
    Response.Write("<Rate JAN='2' FEB='0' MAR='6'/>");
    Response.Write("<Rate JAN='5' FEB='4' MAR='2'/>");
    Response.Write("<Rate JAN='6' FEB='9' MAR='4'/>");
    Response.Write("</Profit>");
    }


    Step 6.Create the Flex Application

    Create the Flex application Test.mxml using either Flex Builder or Notepad. The HTTPService component retrieves the data from the .NET Web appliction(Default5.aspx) and the LineChart component uses the results of the HTTPService as the data series for the chart:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="white" backgroundImage="" creationComplete="this.MyService.send();">
    <mx:HTTPService id="MyService" url="../FlexChart/Default5.aspx" showBusyCursor="true" />
    <mx:LineChart id="myChart" showDataTips="true" dataProvider="{MyService.lastResult.Profit.Rate}" >
    <mx:series>
    <mx:LineSeries yField="JAN" displayName="JAN" />
    <mx:LineSeries yField="FEB" displayName="FEB" />
    <mx:LineSeries yField="MAR" displayName="MAR" />
    </mx:series>
    <mx:verticalAxis>
    <mx:LinearAxis baseAtZero="false"/>
    </mx:verticalAxis>
    </mx:LineChart>
    <mx:Legend dataProvider="{myChart}" direction="horizontal"/>
    </mx:Application>


    Step 7.Copy the swf file to the .NET application
    Now save your flex application by clicking CTRL+S and copy Test.swf from the flex_bin under root directory.
    Save Test.swf file in the FlexChart Folder of your web application

    Step 8.Design the page which will show the swf file and take the user input

    Open the HTML code of ShowChart.aspx and Modify the body portion as follows

    <body>
    <form id="form1" runat="server">
    <div>
    Select Year :
    <asp:DropDownList ID="ddlYear" runat="server">
    <asp:ListItem>1900</asp:ListItem>
    <asp:ListItem>2000</asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="btnOK" runat="server" Text="OK" OnClick="btnOK_Click" /></div>
    <table>
    <tr id="trGraph" runat="server">
    <td align="left" colspan="3">
    <fieldset class="fieldset" style="height: 300px; width: 640px;">
    <legend class="input-group-label">Report Details</legend>
    <div id="divImg1" runat="server" style="overflow: auto; width: 830px; height: 550px;">
    <object id="objLeftGraph" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase=" http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"
    width="830" height="550" align="middle">
    <param name="movie" value="../FlexChart/Test.swf" />
    <param name="allowScriptAccess" value="always" />
    <param name="quality" value="High">
    <param name="play" value="True">
    <param name="loop" value="False">
    <param name="menu" value="False">
    <param name="scale" value="Showall">
    <embed type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"
    align="middle" src="FlexChart/Test.swf" allowscriptaccess="always"></embed>
    </object>

    <script language="javascript" type="text/javascript" src="JScript.js">

    </script>

    </div>
    </fieldset>
    </td>
    </tr>
    </table>
    </form>
    </body>

    Step 9.Add EnableEventValidation="false" in the page directive which will show the report

    Go to the page directive of ShowChart.aspx and write EnableEventValidation="false" in this directive so that it will look like as follows

    <%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="ShowChart.aspx.cs"
    Inherits="ShowChart" %>

    Step 10.Now add a Javascript file "JScript.js" in the FlexChart Folder



    Step 11.Set the start page of your web application
    Right click on the Show chart .aspx and click on "set as start page"



    Step 12.Now run your web application by clicking F5.Click on the OK Button ,you can see the following output depending on the value of the dropdown.


    And that's how easy it is.

    2009-05-19

    Contact Me

    Mail ID:manab.it@gmail.com
    Mobile:##########
    Address:239,Arabinda Pally,PO-Konnagar,Dist-Hooghly,State-West Bengal,Pin-712235,Country-India

    please feel free to contact me at my mail id

    Javascript YES NO Confirm Dialog

    In few cases we need to show yes now popup instead of OK Cancel confirm dialog box.



    You can do it by the following code.

    Write the following code in the head section of your HTML Page

    <script language="javascript" type="text/javascript">
    /*@cc_on @*/
    /*@if (@_win32 && @_jscript_version>=5)
    function window.confirm(str)
    {
    execScript('n = msgbox("'+str+'","4132")', "vbscript");
    return(n == 6);
    }
    @end @*/
    </script>

    Call the function from the onClientClick event of your button

    <asp:Button ID="btnOk" runat="server" OnClientClick="javascript:return confirm('Are you sure to proceed?');" OnClick="btnOk_Click" Text="OK" />

    Write the btnOk_Click event in your server side code
    protected void btnOk_Click(object sender, EventArgs e)
    {
    Response.Write("User Click Yes");
    }

    Now run your application,click the "OK" button.You can see a Yes NO Dialog Box,if the user click yes the control will go to the server side method,if the user click no,the Server side code will not execute

    You can change the Dialog as OK Cancel by changing the value 4132 to 4129 in the statement execScript('n = msgbox("'+str+'","4132")', "vbscript");



    For Abort Rety Ignore change the value to 4130



    For yes no cancel change the value to 4131



    For Retry Cancel ,Change the value to 4133



    For OK Change the value to 4134


    The @cc_on statement activates conditional compilation in the scripting engine.
    It is strongly recommended that you use the @cc_on statement appear in a comment, so that browsers that do not support conditional compilation will accept your script as valid syntax:
    //@cc_on
    ...
    (remainder of script)
    Alternatively, an @if or @set statement outside of a comment also activates conditional compilation.

    2009-05-18

    Create SPList programmatically

    Creating a new list instance using the WSS object model :
    To Create the SPList programmatically,first take a console application "AccessList" and take a reference of Microsoft.Sharepoint.dll

    Write the following code and to create a Custom List "CustList2" which has a two column EmpID and EmpName.

    Please modify the SPSite according to your own SPSite

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;

    namespace AccessList
    {
    class Program
    {
    static void Main(string[] args)
    {
    using (SPSite site = new SPSite("http://manab:36069/sites/DevSite/"))
    {
    using (SPWeb web = site.OpenWeb())
    {
    string listName = "CustList2";
    SPList list = null;
    foreach (SPList currentList in web.Lists)
    {
    if (currentList.Title.Equals(listName,
    StringComparison.InvariantCultureIgnoreCase))
    {
    list = currentList;
    break;
    }
    }
    if (list == null)
    {
    Guid listID = web.Lists.Add(listName,
    "List for big news items",
    SPListTemplateType.GenericList);
    list = web.Lists[listID];
    list.OnQuickLaunch = true;
    list.Fields.Add("EmpID", SPFieldType.Text, true);
    list.Fields.Add("EmpName", SPFieldType.Text, false);
    list.Update();
    }
    // add a few news items
    SPListItem newItem;

    newItem = list.Items.Add();
    newItem["Title"] = "Title1";
    newItem["EmpID"] = "160040";
    newItem["EmpName"] = "Manab";
    newItem.Update();


    newItem = list.Items.Add();
    newItem["Title"] = "Title 2";
    newItem["EmpID"] = "160041";
    newItem["EmpName"] = "Ranjan";
    newItem.Update();
    }
    }
    }
    }
    }

    Now Run your console application and then go to your Site.Refresh Your Site.You will find your list "CustList2" which you have just created



    Click on your List to open it and then click on the New->New Item

    Now You will get the page where you can Put the values for your list.Click Ok To Save your Value

    2009-05-17

    Creating a Simple Webpart using usercontrol

    Sometimes you may have an existing Web page application that needs to be ported to SharePoint. It is relatively simple to do this by converting the pages to User Controls. Although User Controls aren’t recommended for all Web Part applications, they can be a quick way to get an ASP.NET application deployed into your SharePoint Web sites.
    We will need to create a Visual Studio Web Application project, create a Web User Control (ASCX), a class that can act as the Web Part interface to our ASCX Web User Control and a ASP.NET web page in which we can test and debug our components.

    Step 1.In Visual Studio, create a new ASP.NET Web Application (new web site will NOT work for this exercise). For the project name, enter MyWebpart.




    Step 2.Select the Microsoft.SharePoint.dll and set a reference to it in your project.



    If you are developing on a machine not having SharePoint or MOSS, you will need to copy this file along with Microsoft.SharePoint.Search.dll and Microsoft.SharePoint.Search.xml from the same directory to a directory on your local computer.

    Step 3.add a Web User Control file to your project and name it MyUserControl.ascx


    Step 4.The design of the user control will be as follows
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="MyWebpart.MyUserControl" %>
    <table>
    <tr>
    <td>
    Enter First Name :
    </td>
    <td>
    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    Enter Last Name :
    </td>
    <td>
    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Button ID="btnConcate" runat="server" Text="Concate" OnClick="btnConcate_Click" />
    </td>
    <td>
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    </td>
    </tr>
    </table>



    Step 5.Add the following code in the codebehind file of the user control
    protected void btnConcate_Click(object sender, EventArgs e)
    {
    txtName.Text = txtFirstName.Text + " " + txtLastName.Text;
    }

    Step 6.Next, add a class file to your project named MyClass.cs


    Step 7.Import the following namespace
    using Microsoft.SharePoint.WebPartPages;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    And Inherit Microsoft.SharePoint.WebPartPages.WebPart as follows

    public class MyClass : Microsoft.SharePoint.WebPartPages.WebPart

    Step 8.Creating the following class lavel veriable
    protected string _userControlPath = @"~/usercontrols/";
    protected string UserControlFileName = @"MyUserControl.ascx";
    private Control _control;
    private string _exceptions = "";
    Step 9.Next, the class will override the CreateChildControls method to load the Web Control. In this method, the control is loaded from the source file

    protected override void CreateChildControls()
    {
    try
    {
    _control = this.Page.LoadControl(_userControlPath + UserControlFileName);
    Controls.Add(_control);
    }
    catch (Exception CreateChildControls_Exception)
    {
    _exceptions += "CreateChildControls_Exception2: " + CreateChildControls_Exception.Message;
    }
    finally
    {
    base.CreateChildControls();

    }
    }

    Step 10.Next, we will override the RenderContents method which is specific to the WebPart class from which we inherit. This method was chosen because in the life cycle of SharePoint web pages, by the time this method is called all prerequisite processing will have taken place, including the creating and assignment of SharePoint variables and the CreateChildControls method. There is no need to call EnsureChildControls here since child controls will always exist when this method is called by the SharePoint ASP engine.

    protected override void RenderContents(HtmlTextWriter writer)
    {
    try
    {
    base.RenderContents(writer);
    }
    catch (Exception RenderContents_Exception)
    {
    _exceptions += "RenderContents_Exception: " + RenderContents_Exception.Message;
    }
    finally
    {
    if (_exceptions.Length > 0)
    {
    writer.WriteLine(_exceptions);
    }
    }
    }

    Step 11.Just to work with the Javascript function and another server side function from the ECB Menu, write the following function
    public override void CreateWebPartMenu()
    {
    SPWeb web = SPControl.GetContextWeb(this.Context);
    Microsoft.SharePoint.WebPartPages.MenuItem mnuClientSideMenu = new Microsoft.SharePoint.WebPartPages.MenuItem("Menu With Javascript Handler", "javascript:return alert('Client Side Menu Click');");
    Microsoft.SharePoint.WebPartPages.MenuItem mnuServerSideMenu = new Microsoft.SharePoint.WebPartPages.MenuItem("Menu With C# Handler", "MENUID", new EventHandler(mnuServerSideMenu_Click));
    this.WebPartMenu.MenuItems.Insert(0, mnuClientSideMenu);
    this.WebPartMenu.MenuItems.Insert(0, mnuServerSideMenu);
    }
    private void mnuServerSideMenu_Click(object sender, EventArgs e)
    {
    _exceptions += "Server Side Menu CLicked";
    }

    Step 12.We are not quite ready to build our class. Since we intend this WebPart and Web User Control to live in Microsoft Office SharePoint Server and the Global Assembly Cache, we will need to assign a Strong Name key and sign the control. In Solution Explorer, right-click the project node and select Properties. The Project Property Pages appear. Select the Signing tab from the choices on the left. Check the "Sign the assembly" box and select from the "Choose a strong name key file" drop down list.
    Enter "MySnk.snk" in the "Key file name" field. Uncheck the box marked "Protect my key file with a password"




    Step 13.First you will need to ensure that your target SharePoint web site has an UserControls directory. If not, create it. Then copy the ASCX file from your project directory to your SharePoint UserControls directory.




    Step 14.The Global Assembly Cache (GAC) is a special folder located at %WINDIR%\Assembly where %WINDIR% is the full path to your Windows folder (such as C:\Windows or C:\Winnt). Use Windows Explorer to copy your DLL into the GAC folder.

    Step 15.Open the web.config file of your Sharepoint Application from the C:\Inetpub\wwwroot\wss\VirtualDirectories\[port No\Web.config]



    Step 16.Add the following line within the SafeControls section.The following four part assemblies name will be varied for your application
    <SafeControl Assembly="MyWebpart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6f7e2cdc414a688d" Namespace="MyWebpart" TypeName="*" Safe="True" />

    you can use .NET Reflector by Lutz Roeder at http://www.aisto.com/roeder/dotnet/ to browse to your assembly's DLL file and read out the Assembly information and Public Key Token with no hassles and the ability to cut and paste.



    Step 17. Add the following line within the assemblies section.The following four part assemblies name will be varied for your application
    <add assembly="MyWebpart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6f7e2cdc414a688d" />

    Step 18.Next, we need to import (or upload) into SharePoint the WebPart file we created in the previous section. browse to your SharePoint site. Under "Site Actions" select Site Settings, Modify All Site Settings.



    Step 19.On the Site Settings page, under Galleries, click Web Parts


    Step 20.In the Web Part Gallery, click New:



    Step 21.Check the following webpart you have made



    Step 22.Click on Populate Gallery


    Step 23.Now back to your web part galerry and click on "Edit Document Properties" for your web part



    Step 24.Put the following values(all are optional) and click OK


    Step 25.To test or use your WebPart, you will need to add it to a SharePoint page just like you would any other WebPart. This will be very simple since we have added our WebPart to the Web Part Gallery. To summarize:

    Edit a SharePoint page
    Select a zone into which you want to place your WebPart
    Add the WebPart to the zone
    Save the page




    Step 26.Click on a zone and select "Add Web Part". The Add Web Part dialog will be displayed:



    Step 27.Scroll down and find your Web Part, select it and click "Add". Publish your page so everyone can see it. Your web part will now be fully functional on your SharePoint page.

    Step 28.Click on "Menu With C# Handler" and "Menu With Javascript Handler".You will get the followig output


    Complete Code of "MyClass.cs"


    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Microsoft.SharePoint.WebPartPages;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;

    namespace MyWebpart
    {
    public class MyClass : Microsoft.SharePoint.WebPartPages.WebPart
    {
    protected string _userControlPath = @"~/usercontrols/";
    protected string UserControlFileName = @"MyUserControl.ascx";
    private Control _control;
    private string _exceptions = "";
    protected override void CreateChildControls()
    {
    try
    {
    _control = this.Page.LoadControl(_userControlPath + UserControlFileName);
    Controls.Add(_control);
    }
    catch (Exception CreateChildControls_Exception)
    {
    _exceptions += "CreateChildControls_Exception2: " + CreateChildControls_Exception.Message;
    }
    finally
    {
    base.CreateChildControls();
    }
    }
    protected override void RenderContents(HtmlTextWriter writer)
    {
    try
    {
    base.RenderContents(writer);
    }
    catch (Exception RenderContents_Exception)
    {
    _exceptions += "RenderContents_Exception: " + RenderContents_Exception.Message;
    }
    finally
    {
    if (_exceptions.Length > 0)
    {
    writer.WriteLine(_exceptions);
    }
    }
    }
    public override void CreateWebPartMenu()
    {
    SPWeb web = SPControl.GetContextWeb(this.Context);
    Microsoft.SharePoint.WebPartPages.MenuItem mnuClientSideMenu = new Microsoft.SharePoint.WebPartPages.MenuItem("Menu With Javascript Handler", "javascript:return alert('Client Side Menu Click');");
    Microsoft.SharePoint.WebPartPages.MenuItem mnuServerSideMenu = new Microsoft.SharePoint.WebPartPages.MenuItem("Menu With C# Handler", "MENUID", new EventHandler(mnuServerSideMenu_Click));
    this.WebPartMenu.MenuItems.Insert(0, mnuClientSideMenu);
    this.WebPartMenu.MenuItems.Insert(0, mnuServerSideMenu);
    }
    private void mnuServerSideMenu_Click(object sender, EventArgs e)
    {
    _exceptions += "Server Side Menu CLicked";
    }
    }
    }

    Happy Programming!!!