Monday, March 15, 2010

BACKUP LOG [db name] WITH TRUNCATE_ONLY

BACKUP LOG [DB name] WITH TRUNCATE_ONLY in sql sever, when transaction log is full.

Friday, January 29, 2010

Singleton Design Pattern in Asp.net using C#

Singleton Design Pattern in Asp.net using C#


Introduction

When we want to make a only one instance of a class and also making sure that there is a global access point to that object then the design pattern we user is called Singleton. The pattern ensures that the class is instantiated only once and that all requests are directed to that one and only object.
Usage

In asp.net you can use singleton through sessions or application variables easily however i will show you how to implement singleton object to User class.
Problem

In Asp.net to get user information like first name, surname or username through Memebership object does not look neat because to get UserID first you need to know user User.Identity.Name then use Membership.GetUser in order to get more detail for user.
Solution

I have created a interface called IUser which you can use anywhere on the page.

public interface IUser

{

string Email { get; set; }

string Firstname { get; set; }

string Surname { get; set; }

string UserID { get; set; }

string Username { get; set; }

}

Here is our Singleton class implementing IUser.

public sealed class Singleton

{

private const string IUserSessionName = "User";

private static IUser objUser = new UserClass();

Singleton()

{



}

///

/// Loading user information

///


public static IUser IUserInstance

{

get

{

if (null == Session[IUserSessionName])

{

if (User.Identity.IsAuthenticated)

{

string userID = Membership.GetUser().ProviderUserKey.ToString();

// getting userinformation from database

objUser = new UserClass().GetUserInformation(userID);

Session[IUserSessionName] = objUser;

}

}

else

{

objUser = (IUser)Current.Session[IUserSessionName];

}



return objUser;

}

set

{

Session[IUserSessionName] = value;

}

}



public static void UserInstanceFlush()

{

Session[IUserSessionName] = null;

}



}

Because of IuserSessionName you are also making sure that you will keep this session name unique and at only one place.

IUser is a static property which can be used on any page from Singleton.IUserInstance.UserID.

UserInstanceFlush flushes the session if you want to remove the session value.

Wednesday, December 23, 2009

syntax to send mail in as forward

href="mailto:?subject=Check%20out%20this%20link...&body=You%20are%20invited%20to%20check%20out%20a%20cool%20site:%20%20http%3A%2F%2Fwww.mysite.com">Mail Link to a Friend - forward


Mail Link to a Friend

Saturday, July 18, 2009

how to select top 2 max record in sql server

write the query for find the top 2 highest salary in sql server

select esalary from emp_sal e where 2>=(select count(distinct esalary)
from emp_sal where e.esalary<=esalary)

Wednesday, June 24, 2009

Dynamic controls in asp.net, Adding a dynamic control to a placeholder control and wire up the event in ASP.net

ASP.NET provides a dedicated control the PlaceHolder control. You can place this control somewhere on your webform and later on use it to hang dynamic controls.

Now i am describing step by step how i generate and use LinkButton Dynamically.
Refer below code to generate Linkbutton at runtime.

Step 1-First take placeholder on your webform

Step 2-write this code on server side in c#


protected override void OnInit(EventArgs e)
{
for(int i=0;i<5;i++)
{

LinkButton LinkToEditPage = new LinkButton();

LinkToEditPage.Text = "Name" + i.ToString();
//Add new LinkButton to placeholder
PlaceHolder1.Controls.Add(LinkToEditPage);
LinkToEditPage.Attributes.Add("IdUser", "LinkButton" + i.ToString());
// Wire up the eventhandler
LinkToEditPage.Click += new EventHandler(LinkToEditPage_Click);
}
}


//its Event handler code below

protected void LinkToEditPage_Click(object sender, EventArgs e)
{
// Get the control and cast it to the
// appropriate type. In our case a LinkButton.

LinkButton LB = (LinkButton)sender;
string IdUser = LB.Attributes["IdUser"];

Response.Write("You Click on " + IdUser);

}

Step 3-see the output:

when you will run this webform, output will be show like refer below.


when you click on any of one output message will come accordingly.
suppose you have click on secod linkButton output will come as
"You Click on LinkButton2"

I hope this example can give you the idea how to generate dynamic control in asp.net and handle their event.

Tuesday, June 23, 2009

ASP.NET MVC for Visual Studio 2010 Available

Microsoft continues to provide solutions orbiting around its next-generation development platform and tools, well after the first public release of Visual Studio 2010 was launched in mid-May, 2009. The latest addition to the list of items designed to expand Visual Studio 2010 is ASP.NET MVC. The web framework tailored specifically for the first beta of Visual Studio 2010 is currently live on Microsoft's open-source project repository
website CodePlex, and is available for download. The company indicated that the bits were in Alpha stage for the time being. View More Information Here

Saturday, June 20, 2009

what is Hibernate Interview Questions

What is Hibernate?

Hibernate is an object-relational mapping (ORM) solution for the language.
it provides an easy to use framework for mapping an object-oriented domain model to a traditional relational database.Hibernate is a powerful, high performance object/relational persistence and query service. This lets the users to develop persistent classes following object-oriented principles such as association, inheritance, polymorphism, composition, and collections.

What is NHibernate?
NHibernate is an Object-relational mapping (ORM) solution for the Microsoft .NET platform. to know more about click here to know NHibernate

What is ORM?

ORM stands for Object/Relational mapping. It is the programmed and translucent perseverance of objects in a Java application in to the tables of a relational database using the metadata that describes the mapping between the objects and the database. It works by transforming the data from one representation to another.

What are the benefits of ORM and Hibernate?

There are many benefits from these. Out of which the following are the most important one.

1. Productivity – Hibernate reduces the burden of developer by providing much of the functionality and let the developer to concentrate on business logic.
2. Maintainability – As hibernate provides most of the functionality, the LOC for the application will be reduced and it is easy to maintain. By automated object/relational persistence it even reduces the LOC.
3. Performance – Hand-coded persistence provided greater performance than automated one. But this is not true all the times. But in hibernate, it provides more optimization that works all the time there by increasing the performance. If it is automated persistence then it still increases the performance.
4. Vendor independence – Irrespective of the different types of databases that are there, hibernate provides a much easier way to develop a cross platform application.