Category Archives: VB.NET

LINQ: Truly language integrated. Using VB functions inside of LINQ Query

LINQ is truly integrated into VB.NET. This allows not only to use LINQ-specific query language in a standard VB.NET code, but use VB.NET code in a LINQ query. Consider function from the previous post. To make it universal we can pass one more parameter
“Aggregate Type” and based on that parameter return Min, Max, Avg, Sum or Count

Function GroupBy(ByVal i_sGroupByColumn As String,  ByVal i_sAggregateColumn As String, ByVal i_dSourceTable As DataTable, i_iAggregateType as Integer) As DataTable

     dim aQuery = From row In i_dSourceTable Group By Group1 = row(i_sGroupByColumn) Into Group Select Group1,  Aggr = Choose(i_iAggregateType, Group.Min(Function(row) row(i_sAggregateColumn)), Group.Max(Function(row) row(i_sAggregateColumn)), Group.Sum(Function(row) row(i_sAggregateColumn)), Group.Avg(Function(row) row(i_sAggregateColumn)), Group.Count(Function(row) row(i_sAggregateColumn)))

    return aQuery.toDataTable

End Function

In this example VB.NET function Choose is used inside of a LINQ query’s Select Statement. If i_iAggregateType parameter is equal 1 – Minimum value, will be calculated, 2 – Maximum etc.

Grouping ADO.NET DataTable using LINQ

I’ve described before how to group and aggregate data in ADO.NET data table using standard .NET 2.0 features. But if you happen to use .NET 3.5 or above and Visual Studio 2008 or above – that entire block of code can be replaced with a single LINQ query:

Function GroupBy(ByVal i_sGroupByColumn As String,  ByVal i_sAggregateColumn As String, ByVal i_dSourceTable As DataTable) As DataTable

     dim aQuery = From row In i_dSourceTable Group By Group1 = row(i_sGroupByColumn) Into Group Select Group1,  Aggr = Group.Count(Function(row) row(i_sAggregateColumn))

    return aQuery.toDataTable

End Function

The query at line 3 is a LINQ to DataSet query, so reference to System.Data.Linq has to be added to your project. The Group.Count aggregate can be replaced with Group.Max, Group.Min, Group.Sum or Group.Avg to perform respectful function.

There is one caveat though. Continue reading →

IE Modal Dialog and ASP.NET PostBack solution

Internet Explorer has a well known proprietary modal dialog window that can be opened using showModalDialog DOM command. While it is not a good idea to use browser-specific functionality, for many it’s a convenient way to display a modal window and return result back to the parent.

Modal Dialog is designed to display data, accept user input and close window, returning the input back to the parent. It is not meant for postbacks, if you try initiating postback in Modal Dialog, all kinds of weird stuff could happen – from opening postback in a new window to JavaScript errors.

But there is an easy fix for that. If you include following line:

<base target="_self"></base>

inside of your page header in HTML source, e.g.:

<head>
    <title>My Page</title>
    <base target="_self"></base>
</head>

modal dialog will be able to successfully postback to itself.

TNX Text Links Ads: How to automatically fill all your ad spots (including pages with 0 PR) and other tricks

TNX Corp allows you to place static ad links on pages of your site, so you can earn some profit. Unfortunately if you leave it to TNX system, automatic rate at which links are sold and paced on your site could be very slow. You can add links to your pages manually via TNX site interface, but it is only allowed if your page Google PageRank is above 0. Also manual placement can be really inconvenient and you cannot place more than 4 ad links.

Fortunately there is a solution. Continue reading →

UltraWebGrid in OutlookGroupBy Mode: How to display custom aggregates (SUM, AVG, MIN, MAX) in GroupBy row

Last year I described a way to display accurate row count when grid in OutlookGroupBy mode has multiple sub-groups.

That solution requires custom counting function. Fortunately to display aggregates such as SUM, AVG, MIN and MAX UltraWebGrid has a built-in functionality in form of GroupByRowDescriptionMaskDefault property of DisplayLayout. It specifies the string to be displayed in every GroupBy row and can include following substitutes:

  • [caption] – will display GroupBy column header text
  • [value] – will display common to the group cell value
  • [count] – will display row count (does not work correctly with sub-groups)
  • [avg], [sum], [min], [max] – will display the aggregate for the column it’s grouped by
  • [avg:ColumnKey], [sum:ColumnKey], [min:ColumnKey], [max:ColumnKey] – will display the aggregate for any other column, where ColumnKey is the key of the column

With this in mind in just a few simple steps we can make UltraWebGrid to display something like this:


Continue reading →

Internet Explorer renders incorrect HTML if UserAgent string is too long

I’ve encountered a strange problem: 2 identical client machines (WinXP/IE7) were connecting to a website (running on IIS6, ASP.NET 1.1 and employing some Infragistics controls – UltraWebGrid, WebTab, WebMenu). One machine showed a perfectly rendered page, while the other displayed page with missing images and styles and JavaScript functions behaved weirdly. Also, closer examination of HTML source of the page revealed that Infragistics controls rendered very different HTML between 2 machines. Mystery.

The only difference between 2 browsers I found was UserAgent string (For quick way to check your user agent go to http://whatsmyuseragent.com/). Continue reading →

Visual Studio 2003 hangs trying open ASP.NET project from Visual SourceSafe

If you still use Visual Studio .NET 2003 (I still do for some older .NET 1.1 projects) you may experience following situation:

  1. You have an ASP.NET Web project that is controlled by Visual SourceSafe
  2. While opening the project Visual Studio 2003 hangs, stops responding to mouse clicks and keystrokes.

Fortunately there is a quick fix to that.
Continue reading →

Infragistics UltraWebTab: Accessing Tab Separator in client-side JavaScript

I am using Infragistics UltraWebTab and needed to hide a tab and a tab separator located next to it using client-side JavaScript.  Hiding tab is pretty trivial. Assuming we gave it a key:

<igtab:Tab Key="MyTab" Text="My tab">

The code to hide the tab is:

var uwTabControl = igtab_getTabById('xuwMyTabControl');
var tab = uwTabControl.tabFromKey('MyTab');
tab.setVisible(false);

I tried to access separator in the same way but it didn’t work. You can assign it a key, but “fromKey” method works only server-side for separators. Using client-side Tabs array of UltraWebTab didn’t work either – it contains only actual tabs, not separators. But turned out you can access the separator using a little DOM and HTML
Continue reading →

Postback disabled/readonly Textbox that was modified in JavaScript

Let’s say in your ASP.NET application you set a TextBox control’s property ReadOnly to True (or Enabled to False) to prevent user from entering data directly. But you still want to update that text box’s value via client-side JavaScript. Which is happening, the value can be updated.  But during postback to the server – surprise, surprise! – the new value doesn’t persist. This is due to security precaution – if the value wasn’t meant to be changed – the change is not allowed. But there is a way around this.

Continue reading →