Archives

Archives / 2008 / February
  • Different SQL between VB.NET and C# LINQ to SQL SOLVED!!

    Hi All,

    Following my previous post I have solved this issue. Somehow accidently I discovered that if the field has allow nulls set on it VB.NET will add the Where Coalesce in your query hence in my case slowing it down dramatically. It seems that C# does not do this. So finally I have my VB.NET sql comming out exactly the same as in C# simply by turning off allow nulls on my database column... Still seems like odd behaivour to me but for now it all works fine.


    Thanks
    Stefan

  • UserControl OutputCache and VaryByParam not working with postback!!

    Don't know if anyone has come across this before. But if you have a usercontrol that uses VaryByParam and you have it on a page and do a postback you will get the wrong cache item. Use the below test bed to see what I mean, this is following to my post for help on the asp.net forums, I then found this post which showed me it is an issue, following this I think I came up with a solution. That is to add a hidden field to my page with the name MenuID and populate that. Now on postback it seems to work fine, as VaryByParam="MenuID" seems to read the form var MenuID and gets the correct value hence fixing caching.
     

  • Fully Accessible And SEO Friendly Ajax Paging Using DataPager

    Hey All,

    Working on my current project I implemented paging using a listview and a datapager. I then decided it would be much nicer to use AJAX for my paging so wrapped all this up in an updatepanel. Next step was the issue when you would goto a page, select a product and hit the browser back button you would get the first page not the page you were last on. To fix this I simply implemented the ASP.NET AJAX Futures History control which allowed me to save my current page and restore this at a later time.

    Perfect I thought until I started thinking about SEO, now my product catalogue was dead to a search engine as it would only see the first page and not be able to do any further paging. To fix this I went about creating a SEO friendly linkbutton control (I have blogged about this a while back but this is the first time I used it in real life). Basically what the SEO Friendly linkbutton does is render a normal navigateURL and the postback as an onclick. This way with Javascript turned on you get a postback but without you have a normal URL, in my case I am passing the page # in my url like so: http://site.com/catalogue/page-XX/Whatever.aspx, I am using URL Rewriter.NET for my URL rewriting so making a nice URL for this was as simple as adding a new rule into my web.config.

    Firstly here is my custom SEOLinkButton control (Its in VB.NET as is my current project, I have a C# version too but will just post the VB.NET version unless requested):

    Public Class SEOLinkButton
        Inherits LinkButton

    #Region "Properties"

        Public Property NavigateURL() As String
            Get
                Return If(ViewState("NavigateURL") Is Nothing, "", ViewState("NavigateURL").ToString())
            End Get
            Set(ByVal value As String)
                ViewState("NavigateURL") = value
            End Set
        End Property

    #End Region

        Protected Overrides Sub AddAttributesToRender(ByVal writer As System.Web.UI.HtmlTextWriter)

            If (Me.Page IsNot Nothing) Then
                Me.Page.VerifyRenderingInServerForm(Me)
            End If

            Me.EnsureID()
            writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)

            If (Not String.IsNullOrEmpty(Me.CssClass)) Then
                writer.AddAttribute(HtmlTextWriterAttribute.Class, Me.CssClass)
            End If

            If (Not Me.Enabled) Then
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled")
            End If

            If (Not String.IsNullOrEmpty(Me.NavigateURL) AndAlso Me.Enabled) Then
                ' Set the href to be our navigateUrl.
                writer.AddAttribute(HtmlTextWriterAttribute.Href, Me.ResolveUrl(Me.NavigateURL))
            End If

            If (Me.Enabled) Then

                Dim customScript As String = Me.OnClientClick

                If (customScript.Length > 0 AndAlso Not customScript.EndsWith(";")) Then
                    customScript = customScript + ";"
                End If

                Dim opts As PostBackOptions = Me.GetPostBackOptions()
                Dim evt As String = Nothing

                If (opts IsNot Nothing) Then
                    evt = Me.Page.ClientScript.GetPostBackEventReference(opts)
                End If

                ' The onclick now becomes our postback, and the appended custom script.           
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, String.Format("{0}; {1} return false;", evt, customScript))

            End If

        End Sub

    End Class

  • Different SQL between C# and vb.net using LINQ to SQL causes performance issues

    Hi All,

    I am currently working on a project and we are using VB.NET, I am using LINQ to SQL for my data access. I have just implemented my search query and thought I would check the generated SQL's execution plan and found that the subtree cost was about 7.3. I know that I get different SQL between C# and VB.NET when using LINQ to SQL, as VB.NET seems to add a WHERE COALESCE instead of the straight WHERE that C# will do. In the past I did a quick check on a small query to see if it would make a difference and found that there was not a noticable one.

    Now having a larger more complex query I thought I would check the execution plan for the same query but written in C#, I now get a much smaller SQL query and the subtree cost drops from 7.3 to 0.1. A big difference IMO. Has anyone come across this before and what are your throughts on this. It baffles me that you would get such a difference in queries between the two languages.