Friday, 13 August 2010

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

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

01.CREATE PROCEDURE dbo.GetAllUsers
02.
03.AS
04. SET NOCOUNT ON
05. SELECT
06. UserId,
07. UserName,
08. FirstName,
09. LastName,
10. FirstName + ' ' + LastName AS [FullName]
11.
12. 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.

1.<asp:GridView ID="grdUserList" runat="server">
2.</asp:GridView>

After placing the Grid View in page here is the code for biding grid view in default.aspx page_load event.

01.protected void Page_Load(object sender, EventArgs e)
02.{
03. if (!Page.IsPostBack)
04. {
05. using (MyBlogDataContextDataContext myContext =
06. new MyBlogDataContextDataContext())
07. {
08. List<UserInfo> MyUserList =
09. myContext.GetAllUsers().ToList<UserInfo>();
10. grdUserList.DataSource = MyUserList;
11. grdUserList.DataBind();
12. }
13. }
14.}

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…

Wednesday, 11 August 2010

Where is IPagedCollectionView?

This is nothing but a sticky note for me than a post. I downloaded a project and when I tried compiling the project I got an error from the compiler that it couldn’t find IPagedCollectionView. I checked the name space of the class in my other box and it pointed me to System.ComponentModel, I checked the references and I do have that dll. (One thing I did not check was to double click open the reference and see if the class definition there). Anyway, I went directly to the trusted source regarding these kind of problems, Microsoft msdn web site and found the missing dll. Even though the name space points to System.ComponentModel. the class definition is in System.Windows.Data.dll. So for future reference if you run into this problem, now you know, where it is :)

http://msdn.microsoft.com/en-us/library/system.componentmodel.ipagedcollectionview(VS.95).aspx

Wednesday, 4 August 2010

Full screen mode in SilverLight 4

In one our project page it was requested to show the silver light content in Full screen, with out IE headers, tool bars etc., you can achieve this by simply pressing F11. I guess it is a power option and most of the users would not know this command. One option is document it and train. I guess you all know how that one goes. I knew there was a solution to this problem since I have seen examples of silver light media playing movie clip in full screen mode. I looked around on net and found the following link. But before you go there and try out, it will not work in SL4. I am putting the link for documentation purpose of the initial approach taken in SL2.

Tips and Tricks in Silver light (Full screen in SL2 not SL4)

The concept is straight forward. If the full screen option is selected, the browser host will blow up to full screen. Something changed from SL2 and SL4. After more research on the web, found the solution in the following link and I thought I share it with you for anyone looking for full screen solution in SL4. The link is not only to identify what changed in SL4 to get full screen mode but also all the changes in SL4. This is a good reference to have, since most of the net searches bring back SL2 or 3  version of solution, refer the following to find if it changed or not. If changed what is the correct way to do it in SL4.

Full Screen Mode in SL4 and other changes to SL4