google analytics

Thursday, July 24, 2008

Simple .NET Cookie Management

Hello all

Today I will give a short description about using Cookie with .NET , Cookie is one of the most impotent thing to maintain scalability of server as well as manipulation user's information easier. Generally a Cookie enabled browser can store about 20 cookies for a single domain , so what's happen ;when you try to keep more cookie in a browser for a particular domain it discard the oldest cookie. If you don't set the expiration of a Cooke it will remain in memory till the browser session , when you close browser then the cookie is also expire , but when the expiration is set the the cookie is saved in hard drive.

We can manipulate the cookie in two ways as bellow.

// First Example
 Response.Cookies["index"].Value = "test value first";
 Response.Cookies["index"].Expires = DateTime.Now.AddDays(1);
// Second example
HttpCookie httpCookie = new HttpCookie("NewCookie");
httpCookie.Value = "tast value second";
httpCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(httpCookie);

To retrieve the Cookies information we have to do as following . But one thing we all should have to remember that each and ever time when we try to retrieve data from cookie we have to check that the cookie is no "null". If the cookie is null and we want to retrieve data from that cookie it will through an exception.

// First Example
if (Request.Cookies["index"] != null)
    Response.Write(Request.Cookies["index"].Value.ToString());
else
    Response.Write("Cookie not set.");

// Second example
if (Request.Cookies["NewCookie"] != null)
    Response.Write(Request.Cookies["NewCookie"].Value.ToString());
else
    Response.Write("Second Cookie not set.");

That's all for today .

BYE

Friday, July 18, 2008

Easy dll using in Visual Studio

Hello again

Today I will show how to use a .dll  file using Visual Studio. dll is nothing but the a complied managed code by .NET CLR (at least for this tutorial).

what I did is just wrote a simple class "Employee" in a new project named "EmployeeInformation". It has one property and a method jest to show that how to build a dll of a class and use it in another project.

Now i have build the class by right clicking over the project named "EmployeeInformation" and pressing build, For me a dll has been generated in "C:\Users\MD.Tanvir Anowar\Documents\Visual Studio 2008\Projects\RefTest\EmployeeInformation\bin\Debug\" directory named "EmployeeInformation.dll" you may found it in your Output panel at the bellow of VS after build the project.

Now I have to let the dll know by my main project "RefTest", to do this i have to right click on my "RefTest" project and Click over Add Reference .

Now i have to browse and select the "EmployeeInformation.dll"

to user the dll now we have to instantiate the class in our main porject's class by typing:

EmployeeInformation.Employee employee = new EmployeeInformation.Employee();

now in employee object we will get the our desire property of the class as following

Thank you.

Sunday, July 13, 2008

Simple multiple projects add to Visual Studio

Hello all

Today I will explain how to add multiple project in Visual Studio .(That could be use for n-tier architecture or for other purpose). In .NET or in any language its wanted to fragment individual module of a system should have to be separated. In .NET we do this by adding multiple project in a single solution.

First open a new project ( in the example I have used new website named Emp ).

now add a new project from File > Add > New Project ( which will be a layer of Emp Website)

and then select Windows > > Class Library and give a name in the example I have named "DataAccessLayer".

The initial task has been completed. Now in you solution explorer you will find new project named  "DataAccessLayer" and a default class named Class1.cs which you can rename according to your naming convention.

Now the most vital thing to do is let our main website "Emp" be referenced with the "DataAccessLayer" project ,otherwise we will not be able to use the resource of "DataAccessLayer" in "Emp"  !

To do that we have to select out website "Emp" and right click on it after that we have to select Add Reference .

After a short time a window will came named Add Reference, from there we have to select the Project tab and let select the desired project for us its "DataAccessLayer" then click ok.

the task is done now we will be able to use the "DataAccessLayer" project's resource from our "Emp" website .

There is little additional task to do ..

We have to define the name of the project                  

"using DataAccessLayer;"  at the top of the classes which will use the resource of the "DataAccessLayer" as following.

that's all for today

Bye