Tuesday, December 30, 2008

C# Questions

Hi
Here is the basic question which may get confused at some times

++a it increments the value and then it will assign where as a++ it will assign the value and then it increments

int a = 2;
int b = ++a;
Response.Write(b+","+a);

Result:
b : 3
a : 3

int a=2 ;
int b =a++;
Response.write(b+","+a);

Result:
b : 2
a : 3

?? is the operator chks for the null if both c&f are null it results as -1 or else it results first non null value
int? c = 20;
int? f = null ;
int d = c ?? f ?? -1;
Response.Write(d);

Result:
d : 20

int? c = null;
int? f = null;
int d = c ?? f ?? -1;
Response.write(d);

Result:
d : -1;

Convert to binary and left shift bit by 4 digits and then conver to numeric number.Shortcut method is 1*4*4.
int i = 1;
int j = i <<4
Response.Write(j);

Result:
j : 16


Convert to binary and right shift by 2 digits and convert to numeric number .Shortcut Method is i =8 then 8/2 = 4/2 = 2
int i = 8;
int j = i >> 2;
Response.Write(j);

Result:
j : 2


Monday, December 29, 2008

WebCustom Control VS WebUser Control

Here is the steps to create the custom control.To implement this create two projects

To create Custom Control
1. On the File menu, point to New, and then click Project.The New Project dialog box appears.
2. In the ProjectTypes pane, choose either Visual Basic Projects or Visual C# Projects. Select Web Control Library in the Templates pane.
3. Change the Name to CustomLabel and click OK. The new project is created, and WebCustomControl1 opens in the Code Editor. Code for a custom Label control is included in the Web Control Library template by default.
4. On the Build menu, click Build CustomLabel to compile the control.The control is compiled as CustomLabel.dll. By default, it is created in the Bin folder inside the CustomLabel project folder.
5. Save the file.Now the Default Label is created

To create WebForm
1)On the File menu, point to Add Project, and then click New Project.The Add New Project dialog box appears.
2) In the Project Types pane, choose either Visual Basic Projects or Visual C# Projects. Select ASP.NET Web Application in the Templates pan
3)Click OK.
The new project is created

Adding Control to the Toolbox:
1)Right click on the ToolBox,Select Choose Item
2)You will find the .netFrameWorkComponent tab, select Browse and map the webusercontrol dll
3)Click Ok
4)Now You can find the WebuserControl in the tool box .
5)Double Click on the control or register the control with the following Tag
<%@ Register Assembly="CustomLabel " Namespace="CustomLabel " TagPrefix="cc1" %>
6)Now Run Your Project

Web User Controls vs. Web Custom Controls
Web User Control:
1)Easier to Create.
2)Limited support for consumers who use a visual design tool.
3)A separate copy of the control is required in each application.
4)Cannot be added to the Toolbox in Visual Studio.
5)Good for static layout.

Web Custom Controls
1)Harder to create.
2)Full visual design tool support for consumers.
3)Only a single copy of the control is required, in the global assembly cache.
4)Can be added to the Toolbox in Visual Studio.
5)Good for dynamic layout.

Sunday, December 28, 2008

How to get the value between from date and to date in sql?

How to get the value between from date and to date in sql?

Consider the Table Employee has the following fields such as EId,Name,FromDate,ToDate
Suppose If u want to calculate the employee working days the follwing query results

Select * from Emp Where FromDate Between '2008-04-15 16:40:00' and '2008-04-15 18:47:0' and TODate Between '2008-04-15 00:00:00 and '2008-04-15 00:00:00'

Sunday, December 21, 2008

How to know how many tables contains same column in a database

How to know how many tables contains same column in a database?
Select Count (*) as counter from Syscolumn Where(name ='ColumnName')

Thursday, December 18, 2008

Difference between ISNULL and COALESCE in SqlServer2005

COALESCE() and ISNULL() is more or less but the difference is

1)Coalesce() accepts more than 2 parameter and Isnull() accepts only two parameter

ex: Select ISNULL('test',null, 'test1')
Select COALESCE('test',null,'test1')

Results:
IsNull() returns error as The isnull function requires 2 argument(s).
COALESCE() returns test

2)COALESCE() Comapres with any type of datatype Where ISNUL() expression should be of same DataType

ex: Declare @test nvarchar(5)
Declare @test1 int
Set @test= null
Set @test1 =1234567890
Select Isnull(@test,@test1)as nullcolumn
Select COALESCE(@test,@test1,'testingvalue')as coalescecolumn

Results:
ISNULL() returns error as Arithmetic overflow error converting expression to data type nvarchar.
COALESCE() returns result as 1234567890

3)Eventhough if the ISNULL() is of same datatype it takes the first datatype.Let Me explain Clearly

ex: Declare @test nvarchar(5)
Declare @test1 nvarchar(10)
set @test = null
set @test1 = '0123456789'
Select Isnull(@test,@test1)as nullcolumn
Select COALESCE(@test,@test1,'testingvalue')as coalescecolumn

Results:
ISNULL() return as 01234
COALESCE() return as 0123456789

4)A relatively scarce difference is the ability to apply constraints to computed columns that use COALESCE() or ISNULL(). SQL Server views a column created by COALESCE() as nullable, whereas one using ISNULL() is not.

CREATE TABLE dbo.Try
(
col1 INT,
col2 AS COALESCE(col1, 0)
PRIMARY KEY
)
GO

Results:
Cannot define PRIMARY KEY constraint on nullable column in table 'Try'. Could not create constraint. See previous errors.



Wednesday, December 17, 2008

Install Assembly in GAC

Hi,
Here is the steps to install Your assembly in GAC

1)Open Visual studio command tool and map the path of the dll that you want to install in GAC

2)Then Create the stongName Key by using sn -k key.snk .It generates the key for the assembly
For ex:
D:/MyProject/bin/debug sn-k Key.snk. key will be generated in the specified path.

3) Now Open Your Project of the corresponding dll , Right Click on the
project -> properties->Application->Assembly Information-> check the option of Make Assembly COM Visible.

4)Next Step
Project->Properties->Signing->select sign the assembly and browse the key from the project. Save the option.

5)Rebulid Your Project.

6)Now Register your assembly add
regasm dllname, to install the assembly in global assembly
Assembly will be registered

7)To install assembly in the
gacutil -i dllname.Now Your dll will be installed in Gac . Check in windows/assembly

Difference Between Aspx and ashx files

Hi,
Here is the details about ashx file When some one new to this topic

Ashx File is nothing but just like an aspx page.They're equivalent to custom handlers written in C Sharp or Visual Basic.NET in that they contain classes that fully implement IHttpHandler. They're convenient in the same way ASPX files are convenient. You simply surf to them and they're compiled automatically.

When WebForms(aspx)to be used
Simple Html Pages
Asp.net Custom Controls
Simple Dyanamic Pages

When Handlers(ashx) to be used
Binary files
Dynamic Image Views
Performance critical web pages
xml files
Minimal Web Pages

Add the New item Generic handler in the Asp.Net Website

Here is the Example to use HttpHandler

It defines two parts of the IHttpHandler interface. The important part is ProcessRequest(), which will be invoked whenever the Handler.ashx file is requested or pointed to.

1.Map the Handler:
you will probably want the new handler to take over an old URL in your site. To do this, use urlMappings.

2.Add Image In to ur Project
Juz add the image in to the project , to use the image in the handler

3. Modify your Handler.ashx
Here You must modify the process request.Juz modify the code as below
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
// Comment out these lines first:
// context.Response.ContentType = "text/plain";
// context.Response.Write("Hello World");

context.Response.ContentType = "image/png";
context.Response.WriteFile("~/Flower1.png");
}

public bool IsReusable {
get {
return false;
}
}
}

4.Now compile and run the default.aspx
Above Specified is simple application
Here is the other sample to handle the handler with the query string
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {

HttpResponse r = context.Response;
r.ContentType = "image/png";
//
// Write the requested image
//
string file = context.Request.QueryString["file"];
if (file == "logo")
{
r.WriteFile("Logo1.png");
}
else
{
r.WriteFile("Flower1.png");
}
}

public bool IsReusable {
get {
return false;
}
}
}
It juz checks the querystring and display the result as specified

Handlers vs. web pages. ASP.NET web forms inherit from the Page class. This provides them with many events and a very detailed initialization and event model. You don't need that for dynamic images, XML, or binary files.

Why is it faster? It is faster because it does a lot less. As you can imagine firing 10+ events each time a request is handled is a fair amount more expensive than only firing one event.


Monday, December 1, 2008

Find Top second salary in sql

Hi,
Here is the query to find the 1,2,3,4.. nth highest salary

Select Top 1 salary from (select Distinct top 3 salary from Employee Order By Salary desc) as a order by salary asc