Friday, February 25, 2011

Identity and scope Identity

Scope_Identity()
This function returns Last generated Id in the same scope. A scope may be store Procedure,Trigger,Function or Batch.

IDENT_CURRENT('table_name')
Returns the last identity value generated for a specified table or view. The last identity value generated can be for any scope.

@@IDENTITY
@@IDENTITY is a global variable. And returns the last identity value generated for any table across all scopes.

We have two tables Employee1 and Employee2

Employee1 Structure
Id int Not NULL IDENTITY(1,1),
Name varchar(100) NULL,
Salary int


Employee2 Structure
Id int Not NULL IDENTITY(1,1),
Name varchar(100) NULL

You can create the duplicate copy of Employee1 as

Select * into Employee2 from Employee1

And a procedure is used to insert values in first table.

Crate Procedure sp_InsertEmp(@Name varchar(50),@Salary int)
as
Begin
insert into Employee(Name,Salary) values(@Name,@Salary)
Select @@Identity
End

And a trigger is used to insert values in second table.

Create Trigger InsertInEmplyee2 On Employee1 For Insert
As
Begin
Declare @Name varchar(100),
Select @Name = [Name] from Inserted
Insert into Employee2(Name) values (@Name).
End

Here you can not use Identity it will give you last generated Id that will be the Id of Employee2. Bu here we want last generated Id of Employee1. So in this scenario we can use Scope_Identity() or Ident_Current(‘Employee1’). If you are using two tables in the same procedure and you want id generated by a specific table than better is use Ident_Current(‘Table_Name’) instead of Scope_Identity().

When you create an Identity column there are two parameters First is seed and second one is Increment.

Ex. Id int IDENTITY(1,2).. It will start from 1 and per row 2 will be increment number.

You can download the sample sp and trigger :- http://www.box.net/shared/ydez5c49jx

Friday, February 18, 2011

Start with Selector JQuery

Today I have stuck in a problem where I have to select an item start with a specific character in a select box. I found the quiet short and interesting solution for this problem. I want to share this experience with you people.

There is a start with selector in jquery in this you can find out the item start with a specific character or string.

Start with Selector :- We use ^[Power sign] for this in jquery

Suppose you want item start with character C.You can find the value of that item like :

var Char='C';
var sValue = $("#SelectBoxId option[text^='"+ Char + "']").val();


Now select that specific item

$("#SelectBoxId option[value='"+ sValue + "']").attr("selected","selected");

I have found some more selector that may be useful for you.

End with Selector :- Use $ for end with selector

Suppose you have to find out the item which end with a specific character

var ='C';
var sValue = $("#SelectBoxId option[text$='"+ Char + "']").val();

Contains with selector :- Use * for contains with selector

Suppose you have to find out the item which contains a specific string like "rose".

var sValue = $("#SelectBoxId option[text*='rose']").val();

Friday, February 11, 2011

Handlers

Whenever Client do the request to the Server it always goes through the Modules and HttpHandlers.After the Handler it again pass through the modules. A request can go through multiple Modules at a time but pass through only one handler at a time.

If you want to modify request in the middle than you can use HTTP Handlers for that.

To service incoming Http requests Asp.Net use Http Handlers. All handlers implement the IHttpHandler interface, which is located in the System.Web namespace. Handlers are somewhat analogous to Internet Server Application Programming Interface (ISAPI) extensions.

Any class that implements the IHttpHandler interface can act as a target for the incoming HTTP requests. HTTP handlers are somewhat similar to ISAPI extensions. One similarity between HTTP handlers and ISAPI extensions is that HTTP handlers can be called directly by using their file name in the URL, similar to ISAPI extensions.

The following are the methods in IHttpHandler interface

ProcessRequest : Used to call Http Requests.
IsReusable : To check the re-usability of same instance handler with a new request of same type.

We can use <httpHandlers> tag for adding HTTP handlers to our Web applications like..

<httpHandlers>
<add verb="supported http verbs" path="path" type="namespace.classname, assemblyname" />
<httpHandlers>

Attributes

verb = The verb attribute defines allowed HTTP request methods for this handler.If the handler supports all of the HTTP verbs, simply use "*", otherwise list the supported verbs in a comma separated list. So if your handler supports only HTTP GET and POST, then verb attribute will be "GET,POST". Possible values are head, get, post etc*.

path = The path attribute specifies the path or wild card specification of the files for which this handler will be invoked. For example, if you want your handler to be called only when test.xyz file is requested, then the path attribute will contain "test.xyz"; similarly if you want your handler called for any file having .xyz extension, the path attribute will contain "*.xyz".

type =The type attribute specifies the actual type of the handler or handler factory in the form of a combination of namespace, class name and assembly name. ASP.NET runtime first searches the assembly DLL in the application's bin directory and then searches in the Global Assembly Cache (GAC). In type you can give your Handler name also if you have added it through ASP .Net new Items.

The <add> directives are processed in a top-down sequential order. If two or more <add> sub elements specify the same verb/path combination, the final <add> overrides all others.

When we use HttpHanlers?

If your ASP.NET or Sharepoint web application is storing binary data (images, PDF’s) in a database, And you want to pick those images and display to user as if it were normal files on the server (www.example.com/name1.jpg). A clever way to do that is using the IHttpHandler interface.

Even ASP.Net itself use Handlers for its internal processing. You can see in the Machine.config file
<httpHandlers>
<add verb="*" path="trace.axd" type="System.Web.Handlers.TraceHandler"/>

<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory"/>

<add verb="*" path="*.ashx" type="System.Web.UI.SimpleHandlerFactory"/>

<add verb="*" path="*.config" type="System.Web.HttpForbiddenHandler"/>

<add verb="GET,HEAD" path="*" type="System.Web.StaticFileHandler"/>

. . . . . .
. . . . . .
</httpHandlers>

Friday, February 4, 2011

Move a Window

You can move a window at any place. All you have to do get the handle of that window which you want to move. There are two API functions that can be used to move a window.

To get the handle of the window.

[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);

There are two parameter
lpClassName :- Name of the process.
lpWindowName :- Title of the Window

In case of a blank notepad window which has a title Untitle parameres

For example for a new unsaved notepad window

lpClassName :- Notepad
lpWindowName :- Untitle

To move the the window.

[DllImport("User32.dll", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern bool MoveWindow(IntPtr hWnd, int xPos, int yPos, int Width, int Height, bool repaint);

Parameters Detail.
hWnd :- Handle of the window
xPos :- xPosition where you have to move
yPos :- yPosition where you have to move
Width :- Width of the Window
Height :- Height of the window
repaint :- Do you want to repaint the window.

Followers

Link