Archive for the ‘.Net 2.0’ Category

List Databinding Performance With DisplayMembers and ValueMembers

My copy of MSDN magazine arrived last night, and I read the article Practical Tips For Boosting The Performance Of Windows Forms Apps. Good read. Anyway, I was shocked to find out that I have been databinding lists improperly ever since I have been using .Net. I frequently wrote my code like this:

//Bad Code
combobox.DataSource = datatable;
combobox.DisplayMember = “State”;
combobox.ValueMember = “Id”;

//Good Code
combobox.DisplayMember = “State”;
combobox.ValueMember = “Id”;
combobox.DataSource = datatable;

Apparenly, order matters very much. In the first example, the combobox binds using the DisplayMember, then rebinds when updated with the ValueMember. In the second example, the binding only happens once.

In our current app, we have two lists that contain thousands of items that need to be bound, so we are binding them during the startup process so the user won’t wait when requesting that data. The startup time was reduced by just under 40% by changing the order of the code for binding.

Posted on:
Posted in .Net 2.0, Performance, Winforms | Comments Off on List Databinding Performance With DisplayMembers and ValueMembers

ADO.Net 2.0 Presentation on January 20th

I will be giving a presentation at a Microsoft Developer Care event at their offices in Southfield, MI on January 20th. The event is sponsored by New Horizons and I will be talking about What’s New in ADO.Net 2.0. There will be two other talks as well, see the agenda for details.

Posted on:
Posted in .Net 2.0, Speaking | Comments Off on ADO.Net 2.0 Presentation on January 20th

Generics and Enums or Enum.TryParse

I have a case where the user is entering strings into the application, and I have to match that string with the values from a given Enum. Normally that is not too difficult of a task, just convert the enum item to a string and compare, but in this instance I have a bunch of enums to do this with, so I didn’t want to write a separate function for each enum type. So I figured generics might provide a solution and here is what I came up with, which is loosely based on the idea of int.TryParse:

/// <SUMMARY>
/// Takes a string that represents an enum member 
///   and returns the enum member
/// </SUMMARY>
/// <TYPEPARAM name="T">An Enum</TYPEPARAM>
/// <PARAM name="input">
///  The string that is the enum member name, case does 
///    not matter
/// </PARAM>
/// <PARAM name="returnValue">
/// The value from the enum that matches the string, or the 
/// first value of the enum /// </PARAM>
/// <RETURNS>
/// True when there is a match, false when not
/// </RETURNS>
/// <REMARKS>
/// - When no match the first item in the enum is returned
/// - The where clause attempts to constrain the input of T 
/// at compile time to be an Enum
/// </REMARKS> private bool GetEnumValue<T>(string input, out T returnValue)
where T : struct, IComparable, IFormattable, IConvertible { if(Enum.IsDefined(typeof(T), input)) { returnValue = (T)Enum.Parse(typeof(T), input, true); return true; } else //input not found in the Enum, fill the out parameter
// with the first item from the enum
{ string[] values = Enum.GetNames(typeof(T)); returnValue = (T)Enum.Parse(typeof(T), values[0], true); return false; } }

Posted on:
Posted in .Net 2.0 | Comments Off on Generics and Enums or Enum.TryParse

Comboboxes and Double-Clicks

The standard combobox control doesn’t support the double-click event, as it is actually a combination of more than one control itself. In my case, I am using the combobox in simple mode with a textbox at the top and a permanently open list directly below the textbox. I needed for the listbox below to respond to the user double-clicking a list item. I found a great solution in a Usenet post by Jacques Bourgeois in microsoft.public.dotnet.framework.windowsforms.controls. Essentially the code simulates a double-click using the single-click event and a timer control.

Declare a timer control in your class, and in the load event of the form instantiate the timer set the interval of the timer to the interval of the double-click as set in the user’s system:

this.comboDoubleClick = newTimer();

this.comboDoubleClick.Interval = SystemInformation.DoubleClickTime;

In the event handler for the timer, set the code to stop the timer:

this.comboDoubleClick.Enabled = false;

Add code to the single-click event handler to handle the double-click. When the event handler fires, if the timer is not enabled, start the timer (the first click). If the timer is enabled, a previous click has occurred within the system set interval for a double-click. This must be the second click of the double-click, so do whatever you needed the double-click to do and then disable the timer for the next single-click event.

private void Combo_MouseClick(object sender, MouseEventArgs e)
{
   
if (this.comboDoubleClick.Enabled == true)
    {
       
//Do double-click work here
       
this.comboDoubleClick.Enabled = false;
    }
   
else
   
{
       
this.comboDoubleClick.Start();
    }
}

Posted on:
Posted in .Net 2.0, Winforms | Comments Off on Comboboxes and Double-Clicks

Whidbey Beta 1 to Beta 2 Migration with Typed Datasets

Today we are migrating my project from Whidbey Beta1 to Beta 2 (if you know why this is happening today, go ahead and laugh with us), and our biggest hurdle so far is Typed Datasets.

We used dozens of XSD schemas to create typed datasets. Of course, Beta 2 no longer gives a menu option for creating a typed dataset directly from a schema (see this post in the Fedback Center for Visual Studio). The prospect of converting to the Dataset type of schema was daunting, and moving to the command line for xsd.exe was not going over so well either.

We did discover, however, that you can still generate the dataset using the IDE. In the properties of the XSD file, you can set the custom tool to MSDataSetGenerator. Then you can right-click the XSD file and choose Run Custom Tool. The IDE generates the code for the dataset. Whew!

Posted on:
Posted in .Net 2.0 | Comments Off on Whidbey Beta 1 to Beta 2 Migration with Typed Datasets

Select Distinct in a DataTable (The Whidbey Version)

In my last post I described a “Select Distinct“ function for a datatable. This is a whole lot easier using Whidbey. You can get a distinct table based on a column in the DefaultView of the datatable:

DataTable d = dataSetName.dataTableName.DefaultView.ToTable(true, new string[] { "ColumnName" });

Posted on:
Posted in .Net 2.0, ADO.Net | Comments Off on Select Distinct in a DataTable (The Whidbey Version)

Getting Started with the User Interface Application Block

There is an excellent “Hello World” article  for getting started with the UIAB at CodeProject by Roy Clemmons. If you have checked the docs that come with the block, you can appreciate a simplistic example. The block is a powerful piece of code, but you have to get started before harnessing the monster. I had a very difficult time distilling the example code from the block down to something simple that worked. The article from CodeProject did the trick.

Posted on:
Posted in .Net 2.0 | Comments Off on Getting Started with the User Interface Application Block

Data Access Application Block v2 Bug

Mark Brown posted his fix for a bug in the FillDataSet method of the SqlHelper in the Data Access Application Block. The problem occurs when trying to use the FillDataSet method to fill a typed dataset that contains more than two tables. It works great for the first two, not at all for the subsequent tables.

I gave Marks\’s fix a try, but it still didn’t fix the problem for me. I did more digging and found a slightly different fix straight from the authors of the DAAB on the GotDotNet workspace for the DAAB. The fix is in the bug section (direct links don’t seem to work) This cleared up my problem right away. Of course, the bug is fixed in version 3+ of the DAAB. If you are like me, the MSDN site has been my source for the code for the application blocks, and of course there is only 2.0 on the site.

I did not realize they were on version 3 of the DAAB. The authors have added support for an abastract factory to make the SqlHelper ADO.Net provider independant. I don’t know about the rest of you, but the only projects I have ever had to change the database provider on have been Access upgrades. I have never had to move a database back end, say from SqlServer to Oracle. Is it more common than I realize?

Posted on:
Posted in .Net 2.0, ADO.Net | Comments Off on Data Access Application Block v2 Bug