Monday, March 23, 2009

Get IPADDRESS

Method 1

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


Method 2

string strHostName = System.Net.Dns.GetHostName();

string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

HttpContext.Current.Request.UserHostAddress;

Method 3

Use the below code to get the IP address of the machine and not the proxy use the following code

HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_For]

Get Common Records From Two Tables Without Using Join

If you are using SQL Server 2005, then you can use Intersect Key word, which gives you common records.

SELECT column1
FROM table1
INTERSECT
SELECT
column1
FROM table2

If you want in the output both column1 and column2 from table1 which has common columns1 in both tables.

SELECT column1, column2
FROM table1
WHERE column1 IN
(
SELECT column1
FROM table1
INTERSECT
SELECT
column1
FROM table2
)

Monday, March 16, 2009

Search String in Stored Procedure -Sqlserver 2000 & 2005

SQL Server 2000

USE JobPortal
GO
Method-1
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE ‘%Jobseekers%’
GO
Method 2
SELECT DISTINCT o.name ,o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE ‘%Jobseekers%’
GO



SQL Server 2005

USE JobPortal
GO
–Searching for Jobseekers table
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE ‘%Jobseekers%’
GO

Connection Strings For .NET and Sqlserver 2005

Standard Security

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Trusted Connection

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

Connecting to an SQL Server instance

Server=myServerName\theInstanceName;Database=myDataBase;
Trusted_Connection=True;

Connecting to an SQL Server instance via an IP address

Data Source=192.168.1.80,Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

Sql-List All Stored Procedure Modified in Sqlserver

In SqlServer 2000

SELECT ROUTINE_NAME,CREATED,LAST_ALTERED,ROUTINE_TYPE
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE LIKE '%Proc%'
ORDER BY LAST_ALTERED DESC

ROUTINE_TYPE can be "Function","Procedure"


In Sqlserver 2oo5

SELECT name,create_date, modify_date
FROM sys.objects
WHERE type = ‘P’
AND DATEDIFF(D,modify_date, GETDATE()) < 7


This will return a list of all user defined stored procedures in a database along with their name, creation date & last modified date. This will only work if the ALTER command was used to update the sprocs, if the sprocs were dropped and re-created only the create_date will contain any useful information.

Friday, March 13, 2009

Javascript-Working with dates

JavaScript Date Object Methods to get date parts:

getDate()
getDate
function returns the current date from 1 to 31 according to the corresponding month.

getDay()
getDay
function returns the current Day of the week from 0 to 6 where 0 for Sunday, 1 for Monday and so on.

getMonth()
getMonth
function returns the current month from 0 to 11 where 0 for January, 1 for February and so on.

getFullYear()
getFullYear
function returns the current year e.g. 2008


Methods to retrieve date values

The following methods all extract information from the Date object:

Method Description Example
getFullYear () Retrieves the year component of the date as a 4-digit number. y = x.getFullYear ()



getMonth() Retrieves the month component of the date as a number from 0 to 11 (0=January, 1=February, etc) y = x.getMonth()



getDate() Retrieves the day-of-month component of the date as a number from 1 to 31 y = x.getDate()



getDay() Retrieves the day of the week component of the date as a number from 0 to 6 (0=Sunday, 1=Monday, etc) y = x.getDay()



getHours() Retrieves the hours component of the date as a number from 0 to 23 y = x.getHours()



getMinutes() Retrieves the minutes component of the date as a number from 0 to 59 y = x.getMinutes()



getSeconds() Retrieves the seconds component of the date as a number from 0 to 59 y = x.getSeconds()



getMilliseconds() Retrieves the milliseconds component of the date as a number from 0 to 999 y = x.getMilliseconds()



getTimezoneOffset() Retrieves the time difference, in minutes, between the computer's local time and GMT y = x.getTimezoneOffset()

The following methods are similar to the previous ones, but they work with UTC (Universal Time Coordinated) time, or GMT, rather than the computer's local time.

About UTC

UTC is a timezone-independent way of storing time values, based on milliseconds since midnight, January 1, 1970 in the GMT timezone. All dates and times are stored internally in JavaScript using UTC. When you use the above, non-UTC functions to get dates and times, JavaScript converts the time back into the local computer's time locale first. However, the UTC functions below will return times and dates in UTC format instead.

Method Description Example
getUTCFullYear () Retrieves the year component of the date as a 4-digit number. y = x.getUTCFullYear ()



getUTCMonth() Retrieves the month component of the date as a number from 0 to 11 (0=January, 1=February, etc) y = x.getUTCMonth()



getUTCDate() Retrieves the day-of-month component of the date as a number from 1 to 31 y = x.getUTCDate()



getUTCDay() Retrieves the day of the week component of the date as a number from 0 to 6 (0=Sunday, 1=Monday, etc) y = x.getUTCDay()



getUTCHours() Retrieves the hours component of the date as a number from 0 to 23 y = x.getUTCHours()



getUTCMinutes() Retrieves the minutes component of the date as a number from 0 to 59 y = x.getUTCMinutes()



getUTCSeconds() Retrieves the seconds component of the date as a number from 0 to 59 y = x.getUTCSeconds()



getUTCMilliseconds() Retrieves the milliseconds component of the date as a number from 0 to 999 y = x.getUTCMilliseconds()



Methods to set date values

The following methods allow you to set components of the Date object:

Method Description Example
setFullYear() Sets the year component of the date using a 4-digit number. x.setFullYear(2002)



setMonth() Sets the month component of the date using a number from 0 to 11 (0=January, 1=February, etc) x.setMonth(11)



setDate() Sets the day-of-month component of the date using a number from 1 to 31 x.setDate(17)



setMinutes() Sets the minutes component of the date using a number from 0 to 59 x.setMinutes(45)



setSeconds() Sets the seconds component of the date using a number from 0 to 59 x.setSeconds(0)



setMilliseconds() Sets the milliseconds component of the date using a number from 0 to 999 x.setMilliseconds(500)

And again, these methods set date components using UTC, rather than the computer's local time:

Method Description Example
setUTCFullYear() Sets the year component of the date using a 4-digit number. x.setUTCFullYear(2002)



setUTCMonth() Sets the month component of the date using a number from 0 to 11 (0=January, 1=February, etc) x.setUTCMonth(11)



setUTCDate() Sets the day-of-month component of the date using a number from 1 to 31 x.setUTCDate(17)



setUTCHours() Sets the hours component of the date using a number from 0 to 23 x.setUTCHours(20)



setUTCMinutes() Sets the minutes component of the date using a number from 0 to 59 x.setUTCMinutes(45)



setUTCSeconds() Sets the seconds component of the date using a number from 0 to 59 x.setUTCSeconds(0)



setUTCMilliseconds() Sets the milliseconds component of the date using a number from 0 to 999 x.setUTCMilliseconds(500)



Other Date methods

There is a handful of further methods that can be used on Date objects:

Method Description Example
parse() Converts a date text string to the "milliseconds since midnight Jan 1, 1970" format. You call it using Date.parse() rather than object.parse(). x = Date.parse
("January 6, 1972")



UTC(y, m, d, h, m, s) Converts the specified date to the "milliseconds since midnight Jan 1, 1970" format. You call it using Date.UTC() rather than object.UTC(). x = Date.UTC(1972, 0, 6)

x = Date.UTC
(1972, 0, 6, 16, 5, 0)



toString() Outputs the value of the Date object as a date text string. y = x.toString()



toUTCString() Outputs the value of the Date object as a date text string, using UTC. y = x.toUTCString()



toLocaleString() Outputs the value of the Date object as a date text string, using the computer's locale conventions (e.g. D/M/Y for UK computers). y = x.toLocaleString()



toDateString() Outputs the "date" portion of the Date object as a text string. y = x.toDateString()



toTimeString() Outputs the "time" portion of the Date object as a text string. y = x.toTimeString()

Wednesday, March 11, 2009

Using Foreach with an Enumeration

enum Polygon
{
Triangle = 3,
Quadrilateral,
Pentagon,
Hexagon
}


static void Main(string[] args)
{
foreach (Polygon p in Enum.GetValues(typeof(Polygon)))
{
Console.WriteLine("{0} ({1})", p, (int)p);
}
}

/* OUTPUT

Triangle (3)
Quadrilateral (4)
Pentagon (5)
Hexagon (6)

*/

Numeric Types, MaxValue and MinValue

All of the basic numeric data types include two useful fields named MaxValue and MinValue


Console.WriteLine("byte\t{0} to {1}", byte.MinValue, byte.MaxValue);
Console.WriteLine("sbyte\t{0} to {1}", sbyte.MinValue, sbyte.MaxValue);
Console.WriteLine("short\t{0} to {1}", short.MinValue, short.MaxValue);
Console.WriteLine("ushort\t{0} to {1}", ushort.MinValue, ushort.MaxValue);
Console.WriteLine("int\t{0} to {1}", int.MinValue, int.MaxValue);
Console.WriteLine("uint\t{0} to {1}", uint.MinValue, uint.MaxValue);
Console.WriteLine("long\t{0} to {1}", long.MinValue, long.MaxValue);
Console.WriteLine("ulong\t{0} to {1}", ulong.MinValue, ulong.MaxValue);
Console.WriteLine("float\t{0} to {1}", float.MinValue, float.MaxValue);
Console.WriteLine("double\t{0} to {1}", double.MinValue, double.MaxValue);
Console.WriteLine("decimal\t{0} to {1}", decimal.MinValue, decimal.MaxValue);

/* OUTPUT

byte 0 to 255
sbyte -128 to 127
short -32768 to 32767
ushort 0 to 65535
int -2147483648 to 2147483647
uint 0 to 4294967295
long -9223372036854775808 to 9223372036854775807
ulong 0 to 18446744073709551615
float -3.402823E+38 to 3.402823E+38
double -1.79769313486232E+308 to 1.79769313486232E+308
decimal -79228162514264337593543950335 to 79228162514264337593543950335

*/

.NET- Convert a String to Title Case

Namespace : using System.Globalization;

Code: CultureInfo.CurrentCulture.TextInfo.ToTitleCase("The quick brown fox JUMPS over the lazy dog.")

* Outputs "The Quick Brown Fox JUMPS Over The Lazy Dog."

C#- The Null Coalescing Operator

The null coalescing operator is a new operator introduced as a part of the .NET framework version 2.0
The operator's symbol is a double question mark (??).

The null coalescing operator tests the value of a variable to check if it is null. If the value is not null then the variable's value is returned unaffected. If the variable is null however, a substitute value is provided as a second operand.

nt? a = 10;
int? n = null;
int result;

result = a ?? -1; // result = 10
result = n ?? -1; // result = -1

Sunday, March 8, 2009

SQL SERVER - DECLARE Multiple Variables in One Statement

5 days ago. one of my friend asking me that can be declare multiple variable in one statement.
i found that its possible and thought to post this to my blog.

Method 1: Multiple statements for declaring multiple variables

DECLARE @Var1 INT
DECLARE
@Var2 INT
SET
@Var1 = 1
SET @Var2 = 2
SELECT @Var1 ‘Var1′, @Var2 ‘Var2′

Method 2: Single statements for declaring multiple variables

DECLARE @Var1 INT, @Var2 INT
SET
@Var1 = 1
SET @Var2 = 2
SELECT @Var1 ‘Var1′, @Var2 ‘Var2′
GO


From above example its clear that we can declare multiple variables in one statements.

Friday, March 6, 2009

Difference Between COALESCE And ISNULL

  1. COALESCE correctly promotes its arguments to the highest data type in the expression list, ISNULL does not
  2. ISNULL can only work with 2 values while COALESCE can take a lot more
  3. The alternate value takes the length of the first value with ISNULL, with COALESCE this doesn't happen

Scroll wheel in Firfox See the Magic

There are many shortcuts in Firefox we generally don't know.Today let us explore the power of mouse scroll wheel.

1. Moving Back & Forward

Hold the shift key and move the mouse scroll while up and down to move back and forward of the browser history respectively.

2. Close a tab page without even displaying it

Popup ads of yesterday are new tab page ads of today. Few annoying web sites keeps opening popup adds as new tab pages. At times we don’t even want to see what is there in the unwanted tab and close it. Mouse scroll wheels come handy to deal with this situation. Just hover the mouse on the unwanted tab and click using mouse scroll wheel. The tab will be closed immediately.

3. Opening links in a new tab

To open any link on a new tab page just click it using the mouse scroll wheel. In the background Firefox silently opens the link in a new tab page.

4. Scroll line by line while reading a web page

This is the very useful functionality for those who read lot of stuff online. Hold the Alt key while scrolling the page using mouse scroll wheel to move it line by line. This come very handy while you need to scan text line by line.

5. Zoom In and Out images/text

Use CTRL + Mouse Scroll wheel to zoom in and out a web page

6. Scrolling though the tabs

If you open many tabs then Firefox adds a tab scroll as it runs out of space to show all the tabs. The general way of scrolling through the tabs is to use the arrows Firefox displays on the edges of tab area. The quickest way to scroll through the tabs is to use mouse scroll. Place the mouse scroll on any tab and move it up and down to scroll through the pages

7. Open a folder of bookmarks with a single click

Bookmark toolbar of Firefox is very handy to create bookmarks of the favourite web sites that we browse often. The bookmark tool bar also allows us to create folder and group the bookmarks under them. If you click on a folder with mouse scroll wheel, Firefox opens all the bookmarks of the folder in tab pages.

8. Move Up and Down

Click on the web page using mouse scroll wheel to use mouse to move the page up and down