Saturday, September 18, 2010

Website Thumbnail Image Generator

By using simple predefined class we can make website Thumbnail Image , check c# code here


    Bitmap bmp = WebsiteThumbnailImageGenerator.GetWebSiteThumbnail("http://" + txtadd.Text,800, 600, Int32.Parse(txtWidth.Text), Int32.Parse(txtHeight.Text));
            bmp.Save(Server.MapPath("~") + "/thumbImage.bmp");


         Here txtadd.Text,txtWidth.Text and txtHeight.Text values comes from aspx file

Thursday, August 5, 2010

Build Solution and Rebuild Solution option in VS.Net

We have two options for compiling and creating builds for our application. Such as

1)Build Solution
2)Rebuild Solution

1)Build Solution
The Build Solution option compiles only particular project files and components in the solution that have changed since the last build.
For example: Assume that we have two projects P1 and P2 in our solution WebsiteSolution. When we compile the solution using Build Solution option after making some changes to P1 only Pj1 will be compiled and built but P2 will not be compiled.

2) Rebuild Solution
The Rebuild option builds all project files and components.
For example : Assume that we have two projects P1 and P2 in your solution WebsiteSolution. When you compile the solution using Rebuild Solution option after making some changes to P1, both P1 and P2 will be compiled and built even though there are no changes made to P2.

Tuesday, July 27, 2010

Ranking Functions in SQL server

Let’s add RANK function onto this query, you can see additional column ranking the data.
 For example:
 the  Account Id  of ' kingslin ' are assigned a rank of 1 as they are equal, the Account Id  of Bala are assigned the next rank, stalin the next, etc. This is a simple way of assiging a Account Id   by class or category depending on how we order the data - the rank value of 3 simply means that all data with this rank number is the same.
Here I have used Table name with schema name (Customer.ShippingAddress)
Customer is the schema name for that table.



 select
 RANK() over (order by s_Acc_id) as Rank
, ROW_NUMBER() over (order by s_Acc_id) as RowNumber
, s_Acc_id from Customer.ShippingAddress


1 1    kingslin
 2 2    Bala
 3 3    Rajesh
 4 4    varakulan
 5 5    stalin
5 6    stalin
5 7    stalin
5 8    stalin
 9 9    vasanth
9 10    vasanth

Here Stalin Accound Id has the same values generated by the RANK and ROW_NUMBER functions giving us a very simple way of deduplicating the data.
How its working
•    the 'order by' clauses are used to bring specific sorted list
•    the ROW_NUMBER function gives sequential numbers
•    the RANK function allocates a value to each block of data matching the order criteria

Create CTE table and filter rows

 with AlphaRank(Rank, RowNumber, s_Acc_id) as (
select
  RANK() over (order by s_Acc_id) as Rank
, ROW_NUMBER() over (order by s_Acc_id) as RowNumber
, s_Acc_id
from Customer.ShippingAddress
)
select s_Acc_id from AlphaRank where Rank=RowNumber

output is :

kingslin
Bala
Rajesh
varakulan
stalin
vasanth

ROW_NUMBER function using Sql server

ROW_NUMBER using Sql server

Using Row_number function we can get a listing of table with a sequential list.


select ROW_NUMBER() over (order by s_Acc_id) as RowNumber, s_Acc_id from Customer.ShippingAddress



Output is

1    kingslin
2    Bala
3    Rajesh
4    varakulan
5    stalin
6    stalin
7    stalin
8    stalin
9    vasanth
10    vasanth



Using this Row_number function, we can find out particular row.

Example :

You are running a sql query , you need 2nd row of that result,
Using CTE and Row_number function , we can get particular row

Example Query:


WITH OrderedOrders AS
(select ROW_NUMBER() over (order by s_Acc_id) as RowNumber, s_Acc_id from Customer.ShippingAddress
)
SELECT *
FROM OrderedOrders
WHERE RowNumber=2

Output is
2    Bala

Wednesday, July 21, 2010

Compare date with current date using c#

Here I have publish how to compare text box date with current date using c#

DateTime dat1 = DateTime.Parse(txtFromDate.Text);

DateTime dat2 = DateTime.Parse(DateTime.Now.ToShortDateString());

if (DateTime.Compare(dat1, dat2) > 0)
{
lbl.Text = "Date must be less than current date.";

}

First Date I have got from my application, Second date is system date , using
c# compare method , I have compared both date.

Thursday, July 1, 2010

COLLECTIONS INTERFACE USING STACK

Represents a simple last-in-first-out (LIFO) non-generic collection of objects.

Namespace: System.Collections

First action you need to do on Stack is Push elements into it.
The Pop method on stack, and also the Peek method. When you call Pop, the elements from the top of the Stack is returned, and the element is removed from the collection.
The Pop and Peek methods both act on the top of Stack.


Example Programe

using System;
using System.Collections;
class stack
{
public static void Main(string[] args)
{
Stack s1=new Stack();
s1.Push(10);
s1.Push(20);
s1.Push(30);
s1.Push(40);

Console.WriteLine(s1.Pop());
Console.WriteLine(s1.Peek());
Console.WriteLine(s1.Pop());
}
}

COLLECTIONS INTERFACE USING QUEUE

Represents a first-in, first-out collection of objects.

Namespace: System.Collections

the .NET Framework provides the serialized Queue class that implements both the ICollection and the IEnumerable interfaces.
To add value in queue , Enqueue method is used.
Peek()-the first item added to a queue stays in front of all others that would be added.
The process of removing an item from a queue is called "dequeue".


Example Programe

using System;
using System.Collections;
class queue1
{
public static void Main(string[] args)
{
Queue q1=new Queue();
q1.Enqueue(55);
q1.Enqueue(65);
q1.Enqueue(75);
q1.Enqueue(85);

Console.WriteLine(q1.Dequeue());
Console.WriteLine(q1.Peek());
Console.WriteLine(q1.Dequeue());
}
}

PROPERTIES IN C#

In the .NET Framework provides a standard way to specify and mutate name in the C# language.
you can access this type and avoid writing your own name routines.Properties combine aspects of both fields and methods
Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields
A get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. For more information, see Accessor Accessibility.


Example Programe

using System;
using System.Collections;
public class Car
{
private string name;
public string Name
{
get{return name;}
set{name=value;}
}
public Car(string n)
{name=n;}
}
public class CarCollection:CollectionBase
{
public void Add(Car newcar)
{List.Add(newcar);}
public void Remove(Car oldcar)
{List.Remove(oldcar);}
public Car this[int index]
{
get{return (Car)List[index];}
set{List[index]=value;}
}
}








class properties
{
public static void Main(string[] args)
{
CarCollection c=new CarCollection();
c.Add(new Car("santro"));
c.Add(new Car("suzuki"));
c.Add(new Car("nano"));
c.Add(new Car("azer"));

foreach(Car car in c)
{
Console.WriteLine(car.Name);
}

Console.WriteLine(c[2].Name);
c[2].Name="zen";
Console.WriteLine(c[2].Name);

foreach(Car car in c)
{
Console.WriteLine(car.Name);
}
}
}

SHALLOW CLONE USING ICLONEABLE INTERFACE

A shallow copy creates a new instance of the same type as the original
object, and then copies the non-static fields of the original object. If the
field is a value type, a bit-by-bit copy of the field is performed.

Example Programe
using System;
public class one
{public int a;}
public class two:ICloneable
{
public int b;
public one o=new one();
public two(int a1,int b1)
{
o.a=a1;
b=b1;
}
public void show(){Console.WriteLine(o.a+" "+b);}
public Object Clone()
{
return (this.MemberwiseClone());
}
}
class tth
{
public static void Main(string[] args)
{
two t1=new two(1,2);
two t2=(two)t1.Clone();
t1.show();
t2.show();
t1.o.a=5;
t1.b=5;
t1.show();
t2.show();
}
}

DEEP CLONE USING ICLONEABLE INTERFACE

.NET objects are of two types: value and reference. Variables of value type objects hold the object bits themselves and have "copy-on-assignment" behavior. Variables of reference (ref) type are actually pointers to memory. That is, when you create a new variable of ref type and assign it to an existing object, you are actually creating another pointer to the same memory.

A deep copy of an object
duplicates everything directly or indirectly referenced by the fields in the
object.It provides a mechanism to duplicate an object using an object's persistence mechanism (IPersistStream).

Example Programe
using System;
public class one{public int a;}
public class two:ICloneable
{
public int b;
public one o=new one();
public two(int a1,int b1)
{
o.a=a1;
b=b1;
}
public void show(){Console.WriteLine(o.a+" "+b);}
public Object Clone()
{
two t=new two(o.a,b);
return t;
}
}
class twoclone
{
public static void Main(string[] args)
{
two t1=new two(1,2);
two t2=(two)t1.Clone();
t1.show();
t2.show();
t1.o.a=5;
t2.b=5;
t1.show();
t2.show();
}
}

IMPLEMENTING ICOMPARABLE INTERFACE

Array of types that support IComparer, you can sort that array without providing any explicit reference to IComparer. In that case, the elements of the array are cast to the default implementation of IComparer . However, For sorting or comparison capability for custom objects It can be used.

Its works under System.Collections Namespace

IComparable
The role of IComparable is to provide a method of comparing two objects of a particular type.

Example Programe

using System;
class combo:IComparable
{
public int id;
public string name;
public combo(string name1,int x)
{
name=name1;
id=x;
}
public int CompareTo(object o)
{
combo temp=(combo) o;
if(this.id>temp.id)
return (1);
if(this.idreturn (-1);
else
return (0);
}
}
class icomparable
{
public static void Main(string[] args)
{
combo[] c=new combo[3];
c[0]=new combo("aaa",123);
c[1]=new combo("bbb",12);
c[2]=new combo("ccc",1);
Array.Sort(c);
foreach(combo m in c)
Console.WriteLine(m.name+"\t"+m.id);
}
}

IMPLEMENTING IENUMERABLE INTERFACE

IMPLEMENTING IENUMERABLE INTERFACE
Exposes the enumerator, which supports a simple iteration over a non-generic collection
Its works under System.Collections Namespace

IEnumerable, IEnumerator interfaces, which facilitates the iterative access in a custom collection
Implementation default method GetEnumerator of IEnumerable interface

Example Programe

using System;
using System.Collections;
class car:IEnumerable
{
private car[] carr;
public string name;
public car(string s)
{name=s;}
public car()
{
carr=new car[3];
carr[0]=new car("san");
carr[1]=new car("man");
carr[2]=new car("van");
}
public IEnumerator GetEnumerator()
{return carr.GetEnumerator();}
}
class enumInterface
{
public static void Main(string[] args)
{
car o=new car();
foreach(car c in o)
{
Console.WriteLine(c.name);
}
}
}

Binary operators overloading

Binary operators overloading there are two arguments
the general form of operator function for binary operators is as follows.
One of the parameters has to be of a type in which the operator is declared. They include +, -, *, /, %, &, |, ^, <<, >>, ==, !=, >, <, >=, and <=.
public static return_type operator op (Type1 t1, Type2 t2)
{
//Statements
}

Example

public static biop operator +(biop ob1,biop ob2)
{
biop b=new biop();
b.real=ob1.real+ob2.real;
b.im=ob1.im+ob2.im;
return b;
}


Example program in C# Console application

using System;
class biop
{
public int real,im;
public biop(){}
public biop(int r,int i)
{
real=r;
im=i;
}
public void disp(){Console.WriteLine(real+"+i"+im);}
public static biop operator +(biop ob1,biop ob2)
{
biop b=new biop();
b.real=ob1.real+ob2.real;
b.im=ob1.im+ob2.im;
return b;
}
}
class binaryoperator
{
public static void Main(string[] args)
{
biop b1=new biop(5,5);
b1.disp();
biop b2=new biop(2,2);
b2.disp();
biop b3=b1+b2;
b3.disp();
}
}

unary operators overloading

unary operators overloading there is only one argument
the general form of operator function for unary operators is as follows.

public static return_type operator op (Type t)
{}

Example

public static uop operator -(uop ob)
{
ob.a=-ob.a;
ob.b=-ob.b;
return ob;
}


Example Program

using System;
class uop
{
public int a,b;
public uop(int a1,int b1)
{
a=a1;
b=b1;
}
public void disp(){Console.WriteLine(a);Console.WriteLine(b);}

public static uop operator -(uop ob)
{
ob.a=-ob.a;
ob.b=-ob.b;
return ob;
}
}
class unaryoperator
{
public static void Main(string[] args)
{
uop u=new uop(-10,-10);
u.disp();
uop u1=-u;
u1.disp();
}
}

Thursday, June 24, 2010

Diffetence web versions

Difference web versions

Web 1.0
This is mostly ready only purpose, For example HTML and portals web forms.

Web 2.0

This is wildly read- write web purpose. For example blogs, sharing content, Wikipedia,XML and RSS in the web.

Web 3.0

This is describes the portable personal web, For example web widgets and drag & drop (iGoogle)

Tuesday, April 20, 2010

RSS Reader using ASP.NET

RSS Feed is very popular in Internet. This tutorial will show you how to create a RSS Reader using ASP.NET 2.0 and C#.

At first, import the namespace of System.Net, System.IO, and System.Xml

In this sample, we created a simple function to process the RSS feed from a sample URL. This function define a string of rssURL as its parameter. This string contains the RSS's URL. It will use the value of rssURL to create a WebRequest.

WebRequest is the abstract base class for the .NET Framework's request/response model for accessing data from the Internet. An application that uses the request/response model can request data from the Internet in a protocol-agnostic manner, in which the application works with instances of the WebRequest class while protocol-specific descendant classes carry out the details of the request.

Requests are sent from an application to a particular URI, such as a Web page on a server. The URI determines the proper descendant class to create from a list of WebRequest descendants registered for the application. WebRequest descendants are typically registered to handle a specific protocol, such as HTTP or FTP, but can be registered to handle a request to a specific server or path on a server.

The response to this request will be put into WebResponse object. The WebResponse class is the abstract base class from which protocol-specific response classes are derived. Applications can participate in request and response transactions in a protocol-agnostic manner using instances of the WebResponse class while protocol-specific classes derived from WebResponse carry out the details of the request. Client applications do not create WebResponse objects directly; they are created by calling the GetResponse method on a WebRequest instance.

After then, the WebResponse object will be used to create a stream to get the XML data. Stream is the abstract base class of all streams. A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or a TCP/IP socket. The Stream class and its derived classes provide a generic view of these different types of input and output, isolating the programmer from the specific details of the operating system and the underlying devices. The we used a XmlDocument to store the stream data. XmlDocument manipulating the data of XML, finally read the RSS contents from Feed.






using System;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//RssDataSource1.Url = "http://blogs.sivababa.org/index.php/category/testimonials/feed";
//Eval("Title").ToString().Length>50 ? Eval("Title").ToString().Substring(0,49)+"..." : Eval("Title").ToString()

lblDate.Text = DateTime.Now.ToString();
}


protected void btnsmt_Click(object sender, EventArgs e)
{
if(txtrss.Text=="")
{
lblErrMsg.Text = "Please Rss URL";
return;
}
try
{
object Manifest = Cache.Get("Manifest");
if (Manifest != null)
{
//DataTable dt=new DataTable();
//dt =
Datalistmanifest.DataSource = Cache.Get("Manifest");
Datalistmanifest.DataBind();

}
else
{

RssDataSourceManifest.Url = txtrss.Text.Trim();
RssDataSourceManifest.MaxItems = 5;
DataTable dt = new DataTable();
Datalistmanifest.DataSourceID = "RssDataSourceManifest";
Datalistmanifest.DataBind();
Cache.Insert("Manifest", RssDataSourceManifest, null, DateTime.Now.AddHours(3), TimeSpan.Zero);
}
lblErrMsg.Text = "";
}
catch (Exception)
{
lblErrMsg.Text = "RSS Format is not correct";
}

}
protected void lbtncacheRemove_Click(object sender, EventArgs e)
{
Cache.Remove("Manifest");
}
}


Here I have used the cache to display RSS

Add RssToolkit dll in Bin folder