Tuesday, August 3, 2010

Binding A Custom Entity Class to stored procedure using Linq-To-SQL

I have already written several post about Linq its a great ORM that we can use in various way. The purpose of this post to demonstrate How we can bind custom entity to stored procedure result with use of Linq-To-SQL. Let’s go through it and you will realize that how easy it will be to bind a Custom Entity to Stored Procedure result.

Let’s first create a simple table to which will hold the user data. It will contain four field like UserId,UserName,FirstName and LastName like following.

SQLTable

Now let’s insert some data into the table like following. SQLTableData

Now let’s create a stored procedure which will return the table data and a new field called Full Name like following. Here full name is a combination of first name and last name

CREATE PROCEDURE dbo.GetAllUsers

AS
SET NOCOUNT ON
SELECT
UserId,
UserName,
FirstName,
LastName,
FirstName + ' ' + LastName AS [FullName]

FROM dbo.Users
After creating a stored procedure it time to create a Linq-To-SQL Right Click Project->Add New Item and Go To->Data and Add LINQ to SQL Classes called MyBlogDataContext.dbml.After creating datacontext class for Linq just drag above store procedure to Linq-To-SQL classes and it will create a function like following.

StoredProcedureInLinqClass

Now let’s add a New Entity Class called UserInfo into Linq-To-SQL DataContext via Right Click Add New Class Just like following.AddClass

After adding class I have added same property as its having in user table and Hence our UserInfo Class will look like following.

UserInfoClass

Now everything is ready Custom Entity Class called UserInfo and we have Our Function ready which will return Stored Procedure output. Here Linq-To-SQL Provides a property called ReturnType If you select function which we have created via dragging a stored procedure in data context class. We just need to select our UserInfo class there just like following and it will bind the stored procedure with that particular UserInfo class. here only condition should be satisfied that Our Custom Entity class should contain all the field with compatible .NET Data types which will return the data. Below is the property which we are talking about.

SelectProperty

Now let’s add grid view to default.aspx page like following and Let’s bind output of stored procedure to that grid view.
<asp:GridView ID="grdUserList" runat="server">
</asp:GridView> 
After placing the Grid View in page here is the code for biding grid view in default.aspx page_load event.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (MyBlogDataContextDataContext myContext =
new MyBlogDataContextDataContext())
{
List<UserInfo> MyUserList =
myContext.GetAllUsers().ToList<UserInfo>();
grdUserList.DataSource = MyUserList;
grdUserList.DataBind(); 
}
}
}
And here is the output which we get in browser after running our web application.

Ouput

That’s it its very easy.. Hope this will help you…

Shout it
Share:
Sunday, August 1, 2010

Visual Studio –>Add Database –> Named pipe Provider Error for SQL Server

Recently I have been working on a article for my blog for that I just tried to add a database file on my solution with visual studio and I have received following error.

An error has occurred while establishing a connection to the server.
(provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 5)
An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2008, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326)

After checking my configuration I have found that in my machine there was more then one instance of SQL Server and the Default Instance was not properly configured and that’s why I am getting this error. This error was occurred because my Default Instance of SQL Server Express was not having TCP/IP Protocol Enabled. So I have enabled it just like following.

Go to All Programs->Microsoft SQL Server 2008-> Configuration Tools –>SQL Server configuration manager. It will open up windows like following.

SQLServerConfiguration

After that Go To SQL Server Network Configuration and Select Protocols for your default instances and then enabled TCP/IP like following and that’s it. Now error is resolved.

SQL Server TCP/Ip Protol configuration
Hope this will help you…

Technorati Tags: ,
Shout it
kick it on DotNetKicks.com
Share:

TSQL Challenges on beyondrelational.com

My friend and Guide Jacob Sebastian who is an Microsoft SQL Server MVP running a very popular series of TSQL on his site beyondrelational.com. If you know something about SQL Server then this challenge is for you. And if you are master of SQL then this challenge is for you to test you knowledge. If you have some time and If you want to test you knowledge or you want enhance your knowledge challenge then please spare some time to take it. Here is link for that.

http://beyondrelational.com/blogs/tc/archive/2010/07/26/tsql-challenge-35-find-the-total-number-of-full-attendees-in-each-24-hop-session.aspx

Currently he is running TSQL Challenge Number 35 which is about fine number full attendee for a conference.

Shout it
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews