Thursday, September 18, 2008

Add Dynamic Column in SQL

Here is the Code to Add column dynamically in a table

Declare @ColumnName as Varchar(50)
declare @ssql nvarchar(200)
Set @ColumnName ='Column1'
set @ssql = 'Alter table TableName Add ' + @ColumnName + ' int'
exec sp_executesql @ssql

Here is the code to add dynamic column with data type

Declare @ColumnName as Varchar(50)
declare @ssql nvarchar(200)
declare @DataType

Set @ColumnName ='Column1'
Set @
DataType ='Bigint'
set @ssql = 'Alter table TableName Add ' + @ColumnName + ' ' +@DataType
exec sp_executesql @ssql

Tuesday, September 16, 2008

Remove ?xml version="1.0" encoding="utf-8"? in c#

Here is the code to remove xml header

public class XmlTextWriterFormattedNoDeclaration : System.Xml.XmlTextWriter
public XmlTextWriterFormattedNoDeclaration(System.IO.TextWriter w): base(w)
Formatting = System.Xml.Formatting.Indented;

public override void WriteStartDocument() { } // suppress
Then you can serialize like this

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer serializer = new XmlSerializer(typeof(object));
StringWriter sw = new StringWriter();
XmlWriter writer = new XmlTextWriterFormattedNoDeclaration(sw);
serializer.Serialize(writer, this, ns);

Wednesday, September 10, 2008

Difference Between Rank and DenseRank in Sql

Here is the differnce between  Rank() and DenseRank() function
Consider Below  Table Employee Which is ordered by salary
Table Name: Employee
EmpId         Name         Salary
 1  John       100
 2               Jenny           170
 3               Mac             170
 4               Millon 350

Here is the query to rank employee by salary

Select *, Dense_RANK() over(order by Salary) As DenseRank,RANK() over(order by Salary) As Rank From Employee  

Now the tablewill be
EmpId           Name            Salary       Rank     DenseRank
 1                John             100          1             1
 2                 Jenny             170          2             2
 3                 Mac                170         2             2
 4                 Millon  350         4             3

Dense Rank last value will be the total unique value in table

Friday, September 5, 2008

Cropping and Resizing Image in c#

Here is the best link to crop and resize the image step by step process

Merge Image in c#

Here is the code to merge image in c#
// Create a new image
Image img = new Bitmap(300, 100);
// Create image object from existing image
Image img = Image.FromFile("Image.jpg");
Before make changes with img we need to convert it to Graphics object: 
Graphics g = Graphics.FromImage(img);
// Place a.gif
g.DrawImage(Image.FromFile("a.gif"), new Point(10, 10));
// Save changes as output.jpg
img.Save("output.jpg", ImageFormat.Jpeg);