Monday, March 22, 2010

Sorting a LIST (LINQ)

<ListName>.Sort((x, y) => string.Compare(x.<ColumnName>, y.<ColumnName>));

Thursday, April 9, 2009

Give permission to all stored procedures for a SQL user

use < DatabaseName>
Declare @userName varchar(50)
set @userName = < UserName>

declare cur cursor FAST_FORWARD for
select name from sysobjects where xtype = 'P' and category <> 2

declare @spName varchar(255)

open cur
fetch next from cur into @spName

WHILE @@FETCH_STATUS = 0
BEGIN
exec('GRANT EXECUTE ON [dbo].[' + @spName + '] TO ' + @userName + '')
fetch next from cur into @spName
end

close cur
deallocate cur


SELECT 'Given exec permision to all procedures for the user ' + @userName

--

Friday, March 13, 2009

Web application reviewer need to concentrate

While reviewing the code... Some of the points we need to have it in mind, which I have listed below.

1. Checking for un-used reference
2. Removing unused namespace
3. Have logged all the errors
4. Error page for web application
5. If there is any static variable, note the purpose of it
6. If you find replicated code, try to move it to a separate function
7. Coding-Standard (Camel case – Pascal case)
8. Not to have multiple class in a single file
9. Use built-in C# data type (avoid using System.String instead of string)
10. NameSpace need to be as the Project folder structure
11. Better to implement the design pattern as much as possible
12. Code alignment
13. Covered test case
14. Commented wherever there is some complexity logic
15. Avoid in-line style (use only css classes)
16. Avoid specifying the javascript in the page level, instead of that use js (javascript) file

Some tools to check coding standard and test case.
1. StyleCop (To check the coding standard in the Microsoft standard)
2. Re-sharper (Not free-ware. There is a trail version)
3. N-Unit or MSTest for checking unit test cases
4. FX-Cop for coding standard

Some tools which are all useful for web application:
1. Fiddler (HTTP debugging)
2. FireBug (Analyze a web-page)
3. Codesmith (Not free-ware) – to generate database code automatically with the table schema and also database script.

Tuesday, July 15, 2008

Unicode to Codepage

Unicode to Codepage in SSIS package:



In SSIS package, for converting from Unicode to Codepage, follow the following steps:



1. Source table (Contains Unicode data)
2. Script component
3. Data Conversion
2. Destination table (Contains Codepage data)










For converting Unicode to Codepage


Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
Dim codePageNumber As Integer = System.Text.Encoding.GetEncoding(Row.charset).CodePage
Dim unicodeBytes As Byte() = Row.unicodecolumn.GetBlobData(0, CInt(Row.unicodecolumn.Length))
Dim codepagebytes As Byte() = System.Text.Encoding.Convert(System.Text.Encoding.Unicode, System.Text.Encoding.GetEncoding(codePageNumber), unicodeBytes)

Row.codepagecolumn.AddBlobData(codepagebytes)
End Sub

Note:
UnicodeColumn - Unicode data which will be in DT_NTEXT
CodepageColumn - Codepage data which need to be in DT_TEXT

Declare CodePageColumn as output parameter and set the datatype as DT_TEXT.

Then map it to Destination table where codepage need to be stored.

If you want to converrt it again to DT_NTEXT, you can do this using DATA Conversion.

Hope this will help you.

Wednesday, December 5, 2007

Start/Stop Windows Services from ASP.Net 2.0

Need to add the reference System.ServiceProcess
Imports System.ServiceProcess

Dim sc As New ServiceController()
sc.ServiceName = "W3SVC"
Console.WriteLine("The Alerter service status is currently set to {0}", sc.Status)
If sc.Status.Equals(ServiceControllerStatus.Stopped) Then
sc.Start()
Else
sc.Stop()
End If

We can do all related to the service controller

Tuesday, November 27, 2007

This Code is Used to Open a New Window using JavaScript

function onload(url, pageName)
{
var iHeight = 350;
var iWidth = 600;
var str = "scrollbars=No,resizable=No,width=" + iWidth + ",height=" + iHeight + ",menubar=No,toolbar=No,top=" + ( ( screen.availHeight / 2) - ( iHeight / 2 ) )+ " ,left=" + ( ( screen.availWidth / 2) - ( iWidth / 2 ) );
window.open( url, pageName, str );
}

Notes: If you want to open a new window from a href, set href="javascript:void(0);"

JavaScript : Validation (Checking for all controls in that Page)

// Code to pop up a warning if user changes anything in the page and leaves the page
// Picked up from http://www.codestore.org.
// Known to work only on IE 5+ (most of the time)
var isDocBeingSubmitted = false;

function isFormChanged()
{
var frm = document.forms[0];
var ele = frm.elements;
for ( i=0; i < ele.length; i++ )
{
if ( ele[i].type.length > 0 )
{
//if ( isElementChanged( ele, i ) )
if ( isElementChanged( ele[i]) )
{
return true;
}
}
}
return false;
}
function isElementChanged( elem )
{
var isValidationEnabled = true; //validate all controls by default

//to avoid some controls to be checked for changes
if (elem.Validate)
{
if (elem.Validate == "false")
isValidationEnabled = false;
}


if (isValidationEnabled)
{
switch ( elem.type )
{
case "text" :
if ( elem.value != elem.defaultValue ) return true;
break;

case "textarea" :
if ( elem.value != elem.defaultValue ) return true;
break;
case "radio" :
if ( elem.checked != elem.defaultChecked ) return true;
break;
case "select-one" :
for ( var x =0 ; x <elem.length; x++ )
{

if ( elem.options[ x ].selected != elem.options[ x ].defaultSelected )
{
//to avoid facility and department drop down change throwing the alert box
if(elem.name != "FacilityDropDownList" && elem.name != "DepartmentDropDownList")
return true;
}
//if you didnt set the selected to default, and user clicks cancel, dropdown will
//show the last changed value. If user saves the data, it will lead to error.
//So, we are changing the value back to default value irrespective of user click(ok/cancel).
if( elem.name == "FacilityDropDownList" || elem.name == "DepartmentDropDownList")
elem.options[ x ].selected = elem.options[ x ].defaultSelected;
}
break;
case "select-multiple" :
for ( var x =0 ; x <elem.length; x++ ) {
if ( elem.options[ x ].selected != elem.options[ x ].defaultSelected ) return true;
}
break;
case "checkbox" :
if ( elem.checked != elem.defaultChecked ) return true;

default:
break;
} //switch
}// if (isValidationEnabled)
return false;
}