jump to navigation

ASP.NET 4.0 Entity DataSource and GridView September 27, 2009

Posted by fofo in ASP.NET 4.0, c# 4.0, Visual Studio 2010.
Tags: , , , ,

Recently I had the time to examine thoroughly my blog stats. From that it was evident people liked a lot the posts regarding the new features in .Net 4.0. So in this post I am going to discuss the new Entity DataSource web server control which is  similar to the LINQ to SQL DataSource, except it allows you to use your Entity Framework data object model instead. I will also demonstrates the new features of the GridView control. I have talked about EF and how to build an application with EF and C# in one of my previous posts. In this post I will talk more about the Entity Datasource object and enhancements made in the GridView control.

I will create a sample website as always so it is easier for you to understand.In order to follow along you must have installed in your PC VS 2010 and the .NET 4.0 framework.Obviously we are going to need a data store for our application. I will use the AdventureWorksLt which you can download from this site .

1) Launch Visual Studio 2010 and create a new web site.

2) Choose Asp.Net application from the available templates and C# as the development language

3) You now have your files in the Solution Explorer window.

4) Choose the default.aspx page and go to your Toolbox and under the Data Controls drag and drop an Entity Datasource control on the page.

5) Now you need an Entity Data model that the Entity Datasource can bind to. In the Solution Explorer add (right-click) a new special folder , App_Code

6) Select the App_Code and add a new item(right-click) , ADO.NET Entity Data Model.Leave the default name.

7) In the next window select “Generate from database” and click Necxt.

8) In the next window of the Entity model wizard create a new connection to you database (AdventureWorksLT)or select an existing connection that points to the same db.

9) Save the entity connection settings in the web.config file

10) Click Next on the wizard window. From all the available db objects we want to include in our model only tables and more specifically only the Products table.

11) Select the table and click Finish. So now we have a Product Entity with all the mappings to the database.

12) Select the Entity Datasource control and click the arrow on the right-top corner and select Configure Data Source.

13) Select in the Named Connection option of the wizard and select the AdventureWorkLTEntities and click Next

14) In the EntitySetName select Product and from the available Entity values select ProductID,Name,ListPrice,Size,Weightand and click Finish.

15) Drag and drop on the default.aspx page a Gridview control (control in your Toolbox data controls area).Give it some formatting from the AutoFormat options

16) Set the datasource of the Gridview control to the Entity datasource.

17) Enable Sorting,Paging and Selection for the Gridview control

18) Run your application by hitting F5 from your keyboard. If everything is ok you will be able to see a list of products in your page.

The imporevement in the GridView control is about selecting rows.If you selected an item in GridView(the third one for example) and then browsed through the pages the third item in every page will also be selected. That was not always what we wanted. The GridView control did that based on an index it had and used to find the third item on that page.

We can overcome that by enabling the EnablePersistenSelection property and setting it to True.

Then we need to select the DataKeynames property and set it to ProductID.

19) Now if run our application again and select a product in the first page(6th product), and browse through the pages the product in position 6(index 6) is not selected anymore.Now the selection is made over the underlying primary key and not the index in the view of the GridViewControl

Hope it helps!!!

If you need the source code just email me or just comment on this post.

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Furl


VB 10.0 new features and Visual Studio 2010 IDE enhancements June 27, 2009

Posted by fofo in Visual Basic 10.0.
Tags: ,
add a comment

I have downloaded the .Net 4.0 version of the framework and VS 2010 beta version 1 from the microsoft site and I started looking at the new features, IDE e.t.c. Visual studio 2010 has some exciting features.

In this post I will try with a step by step example to highlight the new features of VB 10.0. I will also highlight some of the new enhancements on VS 2010 IDE. There are many new things in .net 4.0 and VS 2010 and certainly I will not cover everything in this post.

The first thing we must do is to create a sample application. I will create a console application. So just fire VS 2010 and from the available templates select a Console application.

Consume first mode

There is a new enhancement in the IntelliSense functionality. We have two alternatives for IntelliSense statement completion: standard mode and consume-first mode. This basically means that we consume classes,types,methods before we actually define them. Have a look at the picture below. In the Main() method I start to use this method without even declaring it first.

Sub Main()

addtwonums(3, 5)

End Sub


When I type the new method I receive a curly line from the compiler and I can realise that there is not a method with such a name.

If I hit CTRL + . a simple method structure is created for me.

Private Sub addtwonums(ByVal p1 As Integer, ByVal p2 As Integer)
Throw New NotImplementedException
End Sub

Now I can go on and finish my method.

This works equally well with classes I have not implemented yet. I have the declaration below in my Main() method.

Dim myperson As New Person

With the same mechanism, CTRL+. we can have a new Person.vb file created in the Solutions window.

Auto-Implemented Properties

Auto-implemented properties, which is a feature that was available with C# 3.0, provides us with a way to specify a property of a class without having to write code to Get and Set the property.If I start setting values to the myperson object, like the code below

Dim myperson As New Person

myperson.name = “Michael”
myperson.surname = “Owen”


I can create properties (without Get and Set)for the name,surname and a method stub for the Walk() method.So in my Person.vb file I have:

Class Person

Property name As String
Property surname As String
Sub Walk()
Throw New NotImplementedException
End Sub
End Class

Also note that you can With auto-implemented properties, a property, including a default value, can be declared in a single line. For example

Public Property Name As String = “John”

It is great seeing that Microsoft has decided to bring VB to the same level as C#. For the future of VB you can listen to this fantastic podcast where Joe Stagner talks with Lisa Feigenbaum from the .NET Managed Languages Group. Another great thing is that Αnders Hejlsberg is going to be in charge of the VB language as well. We know Anders is probably the most suitable person for driving VB to the future.

Collection Initialisers

Collection initialisers was a feature of C# 3.0 and basically it is a new syntax we can create a collection and populate it with an initial set of values. Let’s create a collection of List(Person). Each value that is supplied in the collection initializer is passed to the appropriate Add method of the collection.Inside my Main() method I can create

Dim people As New List(Of Person) From {
{New Person With {.name = “michael”, .surname = “owen”}},
{New Person With {.name = “kenny”, .surname = “daglish”}},
{New Person With {.name = “robbie”, .surname = “fowler”}}}

Please note that there is no “_”, underscore anymore.For more information on this topic see this video and this site.

Multiline Lambdas

In order to demonstrate this, let’s add another console application to our solution. Until VB 9.0 we had only inline lambda expressions. In VB 10.0 we have multiline lambdas. Consider the code below, we just have an array of temperatures and we want to find values greater than 30 degrees.

Dim temperatures(4) As String

temperatures(0) = 12
temperatures(1) = 23
temperatures(2) = 34
temperatures(3) = 45

Dim heightemperatures = temperatures.Where(

Return (temperature > 30)

End Function).ToList()


End Sub)

As you can see in this line bit of the code,

Dim heightemperatures = temperatures.Where(Function(temperature)
Return (temperature > 30)

End Function).ToList()

I am using a multiline lambda expression where I pass into the temperature.

In this part of the code I just display the temperatures on the console by using  Lambda statements (a new feature in vb 10.0) by using the Sub keyword.


End Sub)

For our array declaration we could use .Instead of typing this

Dim temperatures(4) As String

temperatures(0) = 12
temperatures(1) = 23
temperatures(2) = 34
temperatures(3) = 45

we could type this equivalent chunk of code

Dim temperatures = {12, 23, 34, 45}

This is a another new feature called array literals.

Parallel Support

We can execute parts of our program asynchronously-not in sequence using the Parallel object of the.Net 4.0 framework.

The first thing is to import the relevant namespace at the beginning of our code.

Imports System.Threading

To give you a simple example , we can write the following bit of code to output two strings in parallel on the console.Place the code inside the Main () routine.

Console.WriteLine(“My name”)
End Sub,
Console.WriteLine(“My surname”)
End Sub

We use the Invoke method to execute the tasks in parallel.

Run the code and see that the two strings outputted  simultaneously on the screen.For more information on Parallel computing have a look here.

Call Hierarchies

This is a brand new feature of the IDE in VS 2010. If you select a method and right click on it and select View Call Hierarchy so we can see in a niew window that appears titled “Call Hierarchy” which method calls (Calls to) this particular method and what methods this method calls(Calls from). See the picture below.

call hierarchies

Quick Symbol Search

In the code editor if you type CTRL + , the Navigate To window appears. You can enter the name of the type,variable you want and it will find all instances and by clicking on them you will navigate easily to them. Have a look at the picture below.

quick search

Highlighting References

We can highlight all instances of a particular of property , method e.t.c by simply clicking on it.

We can navigate between instances with CTRL+SHIFT+DOWN ARROW or CTRL+SHIFT+UP ARROW.

Enhancements to ASP.NET Multi-Targeting

In Visual Studio 2008, when we targeted a project for an earlier version of the .NET Framework most features of the development environment adapted to the targeted version. However, IntelliSense displayed language features that are available in the current version, and property windows displayed properties available in the current version. In Visual Studio 2010 only language features and properties available in the targeted version of the .NET Framework are shown.

Code Editor

The new Code Editor makes code easier to read. You can zoom in on text by pressing CTRL and scrolling with the mouse wheel.

Visual Studio Debugger

There are Breakpoint enhancements in VS 2010 such as

  • Searching in the Breakpoints window
  • Label breakpoints
  • Import and export breakpoints
  • String comparison for breakpoint conditions in native debugging

See the picture below.



The Chart Control

This control sports various chart types, including point, column, bar, area, doughnut and many more.It can be rendered in a 3D mode. For more information click here and here .

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl