Thursday, January 28, 2010

Replace Newline to br tag

function nl2br_js(myString) {
var regX = /\n/gi ;

s = new String(myString);
s = s.replace(regX, "
\n");
return s;
}

Tuesday, August 18, 2009

SQL SERVER – Find Stored Procedure Related to Table in Database – Search in All Stored Procedure

Following code will help to find all the Stored Procedures (SP) which are related to one or more specific tables. sp_help and sp_depends does not always return accurate results.

----Option 1
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%tablename%'
----Option 2
SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'

Reference : Pinal Dave (http://www.SQLAuthority.com)

Thursday, April 16, 2009

Try Catch Error Handling in Sqlserver2005

BEGIN TRY
– Generate a divide-by-zero error.
SELECT 1/0;
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
GO

Here:-
  • ERROR_NUMBER: returns the error number, and is the same value of @@ERROR.
  • ERROR_SEVERITY: returns the severity level of the error that invoked the CATCH block.
  • ERROR_STATE: returns the state number of the error.
  • ERROR_LINE: returns the line number where the error occurred.
  • ERROR_PROCEDURE: returns the name of the stored procedure or trigger for which the error occurred.
  • ERROR_MESSAGE: returns the full message text of the error. The text includes the values supplied for any substitutable parameters, such as lengths, object names, or times.

Tuesday, April 7, 2009

Get QueryStringvalues using Javascript

function GetQueryStringValueFor(key)
{
var valueToReturn="";
var query = window.location.search.substring(1);
var parms = query.split('&');
for(var i=0 to parms.length)

{ var pos = parms[i].indexOf('=');
if (pos > 0)
{
if(key==parms[i].substring(0,pos))
{
valueToReturn = parms[i].substring(pos+1);
}
}
}
return valueToReturn;
}

Validate Date in Javascript

Format -: mm/dd/yyyy

function checkdate(input){
var validformat=/^\d{2}\/\d{2}\/\d{4}$/
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correctand submit again.")
else{
var monthfield=input.value.split("/")[0]
var dayfield=input.value.split("/")[1]
var yearfield=input.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||dayobj.getDate()!=dayfield)||dayobj.getFullYear()!=yearfield))
alert("Invalid Day, Month,or Year rangedetected. Please correctand submit again.")
else
returnval=true
}
if(returnval==false)
input.select()
return returnval
}

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
)