My shared items

 

Saturday, October 6, 2007

Friday, October 5, 2007

"CURSOR" in SQL Server

Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time. For example, you can use cursor to include a list of all user databases and make multiple operations against each database by passing each database name as a variable.

Cursors are the SLOWEST way to access data inside SQL Server

Example:

USE Northwind
DECLARE @EmployeeID varchar(10), @Counter int

CREATE TABLE #tempTbl
(
EmployeeID int,
OrderCount int
)

SET @Counter = 0
DECLARE Employees CURSOR
FOR
SELECT DISTINCT
(EmployeeID) FROM Employees

OPEN Employees
BEGIN
WHILE( @Counter < @@CURSOR_ROWS)

BEGIN

FETCH NEXT FROM Employees INTO @EmployeeID

INSERT INTO #tempTbl(EmployeeID, OrderCount)
SELECT @EmployeeID AS EmployeeID, COUNT(O.EmployeeID) AS OrderCount FROM Employees E, Orders O WHERE O.EmployeeID = @EmployeeID AND E.EmployeeID = O.EmployeeID

SET @Counter = @Counter + 1
END

SELECT * FROM #tempTbl

DROP TABLE #tempTbl

CLOSE Employees

DEALLOCATE Employees

END


Output:

EmployeeID OrderCount

3 127
4 156
8 104
1 123
2 96
6 67
7 72
5 42
9 43


Monday, September 10, 2007

Embeding Images into Application in C#.

 

These Methods are used To Embed Images into Applications.......

///By Ramesh.R

private Bitmap EmbedImage(string imagePath)
{
try
{
return new Bitmap(typeof(Form1), imagePath);
}
catch (Exception ex)
{
throw ex;
}
}

///(or)


public static Bitmap EmbedImage1(string imagePath)
{
try
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream;
stream = null;
Bitmap bmp = null;
stream = assembly.GetManifestResourceStream(imagePath);
stream.Position = 0;
bmp = (Bitmap)Bitmap.FromStream(stream);
return bmp;
}
catch (Exception ex)
{
throw ex;
}
}


Here..,

imagePath --> Path where the Image Placed..

Both Methods returns a Bitmap instance of the Image.

For this Method to work make sure the Images Property

"BuildAction : Embedded Resource"

Saturday, September 8, 2007

Master Pages Demo






Master Page Design:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Master Pages Demotitle>
head>

<body leftmargin="0" topmargin="0" scroll="no">
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%">
<tr style="height: 3%">
<td valign="top" align="right" background="Images/banner.gif">
<table width="100%" style="text-align: right">
<tr>
<td width="38%" valign="bottom">
td>
<td valign="bottom" align="right">

<%--Script_Menu--%>

td>
tr>
table>
td>
tr>
<tr height="97%">
<td align="center">
<table width="99%" height="100%">
<tr style="height: 3%">
<td align="left" bgcolor="#BDB14B" valign="top" style="font: Bold; font-family: Verdana;">

<asp:Label ID="Label1" runat="server" Text="">asp:Label>
td>
tr>
<tr style="height: 94%">
<td>
<asp:ContentPlaceHolder ID="ScreenContent" runat="server">
asp:ContentPlaceHolder>
<asp:Label ID="Label2" runat="server" Text="">asp:Label>
<asp:Label ID="Label3" runat="server" Text="">asp:Label>
td>
tr>
<tr style="height: 3%" bgcolor="#BDB14B">
<td align="center" style="font-family: Verdana; font-size: 12;">
CopyRights

td>
tr>
table>
td>
tr>
table>
div>
form>
body>
html>



Master Page Code-Behind File:

public partial class MasterPage : System.Web.UI.MasterPage
{
string pageHeadingTitle = "Header";

///
/// Property return a string
///
public string PageHeadingTitle
{
get
{
return pageHeadingTitle;
}
set
{
pageHeadingTitle = value;
}
}

///
/// Property returns a Label Object
///
public Label MasterPageLabel
{
get
{
return Label2;
}
set
{
Label2 = value;
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Label1.Text = PageHeadingTitle;
}
}
}

Content Page Design:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Default1" %>
<%@ MasterType VirtualPath="~/MasterPage.master" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ScreenContent" Runat="Server">
asp:Content>



Content Page Code Behind File:

///Using FindControl()
this.MasterPageFile = "MasterPage.master";
MasterPage mp = (MasterPage)this.Master;

Label lbl = mp.FindControl("Label2") as Label;
if (lbl != null)
lbl.Text = "Label2";

///(Or)

(Master.FindControl("Label3") as Label).Text = "Label3";

///Using Properties Or Controls in Master Page
Master.PageHeadingTitle = "Content Page Header";
Master.MasterPageLabel.Font.Size = 25;
Master.MasterPageLabel.Text = "Label2";



Saturday, September 1, 2007

How to Get AM PM Formatted DateTime in SQL Server

The Following Function is used to Gets the AM Or PM Formatted DateTime for the Given Date


CREATE FUNCTION GetAMPMFormat(@inputDate DATETIME)
RETURNS char(25)
AS
BEGIN

DECLARE @time varchar(5)
SET @time = CONVERT(char(10), @inputDate, 108)

DECLARE @date varchar(10)
SET @date = CONVERT(char(10), @inputDate, 101)

DECLARE @hour varchar(2)
DECLARE @minute varchar(2)

SET @hour = LEFT(@time,2)
SET @minute = SUBSTRING(@time,4,2)

DECLARE @amOrPm char(2)
SET @amOrPm = 'AM'

IF CAST(@hour as int) = 0 BEGIN
SET @hour = 12
SET @amOrPm = 'AM'
END

ELSE IF CAST(@hour as int) = 12 BEGIN
SET @amOrPm = 'PM'
END

ELSE IF CAST(@hour as int) > 12 BEGIN
SET @hour = @hour - 12
SET @amOrPm = 'PM'
END

DECLARE @outputDate char(25)
SET @outputDate = @date + ' ' + @hour + ':' + @minute + ' ' + @amOrPm

RETURN @outputDate
END


For Example you maye use like

PRINT dbo.GetAMPMFormattedDateTime('01-08-2007 20:10')

it will prints
01/08/2007 8:10 PM

SQL Server DateTime Related...

CONVERT

CONVERT function explicitly converts an expression of one data type to another. CAST and CONVERT provide similar functionality.

Syntax

CAST ( expression AS data_type )

CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

Arguments

expression

Is any valid Microsoft SQL Server expression.

data_type

Is the target system-supplied data type, including bigint and sql_variant. User-defined data types cannot be used.

length

Is an optional parameter of nchar, nvarchar, char, varchar, binary, or varbinary data types.

style

Is the style of date format used to convert datetime or smalldatetime data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types), or the string format when converting float, real, money, or smallmoney data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types).


yy yyyy
Standard

Input/Output
- 0 or 100
Default mon dd yyyy hh:miAM (or PM)
1 101 USA mm/dd/yy
2 102 ANSI yy.mm.dd
3 103 British/French dd/mm/yy
4 104 German dd.mm.yy
5 105 Italian dd-mm-yy
6 106 - dd mon yy
7 107 - Mon dd, yy
8 108 - hh:mm:ss
- 9 or 109
Default + milliseconds mon dd yyyy hh:mi:ss:mmmAM (or PM)
10 110 USA mm-dd-yy
11 111 JAPAN yy/mm/dd
12 112 ISO yymmdd
- 13 or 113
Europe default + milliseconds dd mon yyyy hh:mm:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 20 or 120
ODBC canonical yyyy-mm-dd hh:mi:ss(24h)
- 21 or 121
ODBC canonical (with milliseconds) yyyy-mm-dd hh:mi:ss.mmm(24h)
- 126 ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(no spaces)
- 130 Kuwaiti dd mon yyyy hh:mi:ss:mmmAM
- 131 Kuwaiti dd/mm/yy hh:mi:ss:mmmAM

Some Other Date Related Functions...

Dateadd: Returns a new datetime value based on adding an interval to the specified date.

Syntax: DATEADD ( datepart, number, date )

Datediff: Returns the number of date and time boundaries crossed between two specified dates.

Syntax: DATEDIFF ( datepart, startdate, enddate )

Datename: Returns a character string representing the specified datepart of the specified date.

Syntax: DATENAME ( datepart, date )

Datepart: Returns an integer representing the specified datepart of the specified date.

Syntax: DATEPART ( datepart, date )

Day: Returns an integer representing the day datepart of the specified date.

Syntax: DAY ( date )

Getdate: Returns the current system date and time in the Microsoft® SQL Server™ standard internal format for datetime values.

Syntax: GETDATE ( )

Month: Returns an integer that represents the month part of a specified date.

Syntax: MONTH ( date )

Year: Returns an integer that represents the year part of a specified date.

Syntax: YEAR ( date )

Here,

datepart--> may be mm, dd, yy, yyyy, mi..etc...


Example:


PRINT DATEADD ( dd, 60, 'Sep 1 2007 10:46AM')


will prints
Oct 31 2007 10:46AM

Thursday, August 30, 2007

"Add To Favorites" Dialog in JavaScript

JavaScript to launch the browser Favorites window to bookmark the current page.

function bookmarkThisSite(url, bookmarkName)
{
if (sidebar)
{
sidebar.addPanel(bookmarkName, url,"");
}
else if( document.all )
{
external.AddFavorite(url, bookmarkName);
}
else if( window.opera && window.print )
{
return true;
}
else
{
return true;
}
}

The JavaScript Programming Language

Tuesday, August 28, 2007

How Do I Get Paths and URL fragments from the HttpRequest object?

Visit...

http://www.cambiaresearch.com/c4/dfdb20ae-b335-48ae-a201-f2a5a8329342/How-Do-I-Get-Paths-and-URL-fragments-from-the-HttpRequest-object.aspx

DLL Register And UnRegister in Mouse Context Menu


Normal Case:
For register DLL or OCX:
rundll32 yourdll.dll,DLLRegisterServer
rundll32 yourocx.ocx,DLLRegisterServer

For unregister DLL or OCX:
rundll32 yourdll.dll,DLLUnregisterServer
rundll32 yourocx.ocx,DLLUnregisterServer

For Mouse Context Menu:

If do you want make a "Right mouse-click" option, enter in the REGEDIT and make the options:

For DLL Register:

HKEY_CLASSES_ROOT\dllfile
Make a new key "Shell":

HKEY_CLASSES_ROOT\dllfile\shell
Make a new key "Register":

HKEY_CLASSES_ROOT\dllfile\shell\register
In the "Default" value, type: "DLL Register"
And make a new key "Command":

HKEY_CLASSES_ROOT\dllfile\shell\register\command
In the "Default" value, type:
rundll32.exe "%1",DllRegisterServer

For DLL Unregister:

HKEY_CLASSES_ROOT\dllfile
Make a new key "Shell":

HKEY_CLASSES_ROOT\dllfile\shell
Make a new key "Unregister":

HKEY_CLASSES_ROOT\dllfile\shell\unregister
In the "Default" value, type: "DLL Unregister"
And make a new key "Command":

HKEY_CLASSES_ROOT\dllfile\shell\unregister\command
In the "Default" value, type:
rundll32.exe "%1",DllUnregisterServer


For OCX Register:

HKEY_CLASSES_ROOT\ocxfile
Make a new key "Shell":

HKEY_CLASSES_ROOT\ocxfile\shell
Make a new key "Register":

HKEY_CLASSES_ROOT\ocxfile\shell\register
In the "Default" value, type: "OCX Register"
And make a new key "Command":

HKEY_CLASSES_ROOT\ocxfile\shell\register\command
In the "Default" value, type:
rundll32.exe "%1",DllRegisterServer

For OCX Unregister:

HKEY_CLASSES_ROOT\ocxlfile
Make a new key "Shell":

HKEY_CLASSES_ROOT\ocxfile\shell
Make a new key "Unregister":

HKEY_CLASSES_ROOT\ocxfile\shell\unregister
In the "Default" value, type: "OCX Unregister"
And make a new key "Command":

HKEY_CLASSES_ROOT\ocxfile\shell\unregister\command
In the "Default" value, type:
rundll32.exe "%1",DllUnregisterServer

Client-Side JavaScript Reference

Client-Side JavaScript Reference:

"Client-Side JavaScript Reference This book is a reference manual for the JavaScript language, including both core and client-side JavaScript for version 1.3. JavaScript is Netscape's cross-platform, object-based scripting language for client and server applications."

http://docs.sun.com/source/816-6408-10/contents.htm

Saturday, August 25, 2007

Delegates and Events in C# / .NET

Overview

All of us have been exposed to event driven programming of some sort or the other. C# adds on value to the often mentioned world of event driven programming by adding support through events and delegates. The emphasis of this article would be to identify what exactly happens when you add an event handler to your common UI controls. A simple simulation of what could possibly be going on behind the scenes when the AddOnClick or any similar event is added to the Button class will be explained. This will help you understand better the nature of event handling using multi cast delegates.

Delegates

A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.


Events

The Event model in C# finds its roots in the event programming model that is popular in asynchronous programming. The basic foundation behind this programming model is the idea of "publisher and subscribers." In this model, you have publishers who will do some logic and publish an "event." Publishers will then send out their event only to subscribers who have subscribed to receive the specific event.


More Information:

http://www.akadia.com/services/dotnet_delegates_and_events.html

Thursday, August 9, 2007

enum at C# Online.NET (CSharp-Online.NET)

enum at C# Online.NET (CSharp-Online.NET)

enum

The Basics

An enum is a value type with a set of related named constants often referred to as an enumerator list. They allow code to look a lot cleaner and easier to read by getting rid of "magic numbers", that is to say, they get rid of numbers which have a purpose within a module of code, but make the code harder to read. If a single number needs a definition to make it easier to read then use a constant as shown below.

public const int North = 0;

However, if more than one related number needs a definition, then use an enumeration.


Declaration

An enum is declared as follows:

[attributes] [modifiers] enum identifier [:base-type]
{
enumerator-list [,]
}

The attributes is optional and is used to hold additional declarative information.

The modifier is optional. The allowed modifiers are new, public, protected, internal and private.

The keyword enum must be followed by an identifier that names the enum.

The base-type of an enumeration may be one of the following; byte, sbyte, short, ushort, int, uint, long or ulong. If no base-type is declared, than the default of int is used.

The enumerator-list contains the identifiers which are separated by commas (optionally including a value assignment).

The first enumerator begins at zero by default and each enumerator following is increased by 1. This can be overridden so that each member of the enumerator-list contains its own unrelated value (see second example below). Two items in an enumeration can hold the same value, however, this will cause problems if you use an automated switch statement where all elements are added.


Example 1 – A simple enumeration

An application may need to work with directions. Instead of using four numbers to represent the major directions, an enumeration works better. The numbers then become irrespective as the directions are named.

enum Direction
{
North,
East,
South,
West
}


Example 2 – Number specific

Now consider the same enumeration, but with each direction holding the value clockwise that they are from North.

enum Direction
{
North = 0,
East = 90,
South = 180,
West = 270
}

We would use the enumeration in the same manner, but the values have different meanings.


Casting

To get the underlying value of an item in an enumerator-list, you need to have an explicit cast to convert it from the enum type to its integral type. The following code displays this and makes use of the enum in example 2.

static void Main(string[] args)
{
Console.WriteLine("-------------------------------");
Console.WriteLine("Direction Enum Members by Name:");
Console.WriteLine("-------------------------------");

// get a list of member names from Direction enum,
// figure out the numeric value, and display
foreach (string direction in Enum.GetNames(typeof(Direction)))
{
Console.WriteLine("Direction Member: {0}\n Value: {1}",
direction, (int)Enum.Parse(typeof(Direction), direction));
}
}

And here is the output

-------------------------------
Direction Enum Members by Name:
-------------------------------
Direction Member: North
Value: 0
Direction Member: East
Value: 90
Direction Member: South
Value: 180
Direction Member: West
Value: 270


Some useful methods

  • Enum.GetName - retrieves the name of the constant in the enumeration that has a specific value.
  • Enum.GetNames - retrieves an array of the names of the constants in the enumeration.
  • Enum.GetValues - retrieves an array of the values of the constants in the enumeration.
  • Enum.IsDefined - returns an indication whether a constant with a specified value exists in a specified enumeration.

MSDN references

Tuesday, August 7, 2007

RegExLib.com Regular Expression Cheat Sheet (.NET Framework)

RegExLib.com Regular Expression Cheat Sheet (.NET Framework): "RegExLib.com Regular Expression Cheat Sheet (.NET)"

RegExLib.com Regular Expression Cheat Sheet (.NET)

Metacharacters Defined
MCharDefinition
^Start of a string.
$End of a string.
.Any character (except \n newline)
|Alternation.
{...}Explicit quantifier notation.
[...]Explicit set of characters to match.
(...)Logical grouping of part of an expression.
*0 or more of previous expression.
+1 or more of previous expression.
?0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string.
\Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below.
Metacharacter Examples
PatternSample Matches
^abcabc, abcdefg, abc123, ...
abc$abc, endsinabc, 123abc, ...
a.cabc, aac, acc, adc, aec, ...
bill|tedted, bill
ab{2}cabbc
a[bB]cabc, aBc
(abc){2}abcabc
ab*cac, abc, abbc, abbbc, ...
ab+cabc, abbc, abbbc, ...
ab?cac, abc
a\sca c
Character Escapes http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconcharacterescapes.asp
Escaped CharDescription
ordinary charactersCharacters other than . $ ^ { [ ( | ) ] } * + ? \ match themselves.
\aMatches a bell (alarm) \u0007.
\bMatches a backspace \u0008 if in a []; otherwise matches a word boundary (between \w and \W characters).
\tMatches a tab \u0009.
\rMatches a carriage return \u000D.
\vMatches a vertical tab \u000B.
\fMatches a form feed \u000C.
\nMatches a new line \u000A.
\eMatches an escape \u001B.
\040Matches an ASCII character as octal (up to three digits); numbers with no leading zero are backreferences if they have only one digit or if they correspond to a capturing group number. (For more information, see Backreferences.) For example, the character \040 represents a space.
\x20Matches an ASCII character using hexadecimal representation (exactly two digits).
\cCMatches an ASCII control character; for example \cC is control-C.
\u0020Matches a Unicode character using a hexadecimal representation (exactly four digits).
\*When followed by a character that is not recognized as an escaped character, matches that character. For example, \* is the same as \x2A.
Character Classes http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconcharacterclasses.asp
Char ClassDescription
.Matches any character except \n. If modified by the Singleline option, a period character matches any character. For more information, see Regular Expression Options.
[aeiou]Matches any single character included in the specified set of characters.
[^aeiou]Matches any single character not in the specified set of characters.
[0-9a-fA-F]Use of a hyphen (–) allows specification of contiguous character ranges.
\p{name}Matches any character in the named character class specified by {name}. Supported names are Unicode groups and block ranges. For example, Ll, Nd, Z, IsGreek, IsBoxDrawing.
\P{name}Matches text not included in groups and block ranges specified in {name}.
\wMatches any word character. Equivalent to the Unicode character categories [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \w is equivalent to [a-zA-Z_0-9].
\WMatches any nonword character. Equivalent to the Unicode categories [^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \W is equivalent to [^a-zA-Z_0-9].
\sMatches any white-space character. Equivalent to the Unicode character categories [\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \s is equivalent to [ \f\n\r\t\v].
\SMatches any non-white-space character. Equivalent to the Unicode character categories [^\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \S is equivalent to [^ \f\n\r\t\v].
\dMatches any decimal digit. Equivalent to \p{Nd} for Unicode and [0-9] for non-Unicode, ECMAScript behavior.
\DMatches any nondigit. Equivalent to \P{Nd} for Unicode and [^0-9] for non-Unicode, ECMAScript behavior.