Translate

Sunday, July 7, 2013

Anonymous types in vb.net

Visual Basic supports anonymous types, which enable you to create objects without writing a class definition for the data type. Instead, the compiler generates a class for you. The class has no usable name, inherits directly from Object, and contains the properties you specify in declaring the object. Because the name of the data type is not specified, it is referred to as an anonymous type.

The following example declares and creates variable product as an instance of an anonymous type that has two properties, Name and Price.

' Variable product is an instance of a simple anonymous type. 
Dim product = New With {Key .Name = "paperclips", .Price = 1.29}
 
 A query expression uses anonymous types to combine columns of data selected by a query. You cannot define the type of the result in advance, because you cannot predict the columns a particular query might select. Anonymous types enable you to write a query that selects any number of columns, in any order. The compiler creates a data type that matches the specified properties and the specified order.

In the following examples, products is a list of product objects, each of which has many properties. Variable namePriceQuery holds the definition of a query that, when it is executed, returns a collection of instances of an anonymous type that has two properties, Name and Price.  


Dim namePriceQuery = From prod In products
                     Select prod.Name, prod.Price
 

Monday, July 1, 2013

Fix the Session Null with Url Rewriting in asp.net

Symptoms:

Some times when you are doing some URL rewriting in asp.net , you came a cross a strange situation , your Session Property inside your pages becomes null.

The Code:

Public Sub Init(context As HttpApplication) Implements IHttpModule.Init
        AddHandler context.BeginRequest, AddressOf OnBeginRequest
End Sub
 
Public Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim app As HttpApplication = CType(sender, HttpApplication)
        If app.Context.Request.RawUrl.ToLower().Contains(".aspx")
           Then
            If Not app.Context.Request.RawUrl = "/" Then
                Dim PageId As String = PathsLayer.GetPageId(app.Context.Request.RawUrl)
                app.Context.RewritePath("~/Pages/viewpage.aspx?pageID=" 
                                        + PageId, False)
            End If
        End If
 End Sub

When you try to access the session variable inside your page after the rewriting completes you can see the following error :

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.

 Solution :

The solution was to add the following 2 lines to web.config inside <system.webserver> under the <modules> section 

<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>

and that did the trick , hope this helps some body.