Microsoft KB Archive/326176

= HOW TO: Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET =

Article ID: 326176

Article Last Modified on 2/8/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q326176



For a Microsoft Visual Basic .NET version of this article, see

325684 HOWTO: Implement a DataSet SELECT DISTINCT helper class in Visual Basic .NET

NOTE: This article is one of a series of DataSetHelper articles. The code in the DataSetHelper class that is presented in this article can be merged with that given in other DataSetHelper articles to make a single class with a more comprehensive feature set.

Click here to view the complete list of DataSetHelper articles

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data

IN THIS TASK
SUMMARY
 * Requirements
 * DataSetHelper Shell Class
 * SelectDistinct Method
 * Test the Application
 * Enhancement Ideas
 * Troubleshooting



SUMMARY
This step-by-step article illustrates how to implement and how to use a DataSetHelper class that includes sample code to create a DataTable object that contains the unique values of a column of another DataTable object.

To do this, you use the SelectDistinct public method. You can also use a private helper method that compares fields that may contain NULL values (DBNull.Value).

The DataSetHelper class includes a DataSet member variable. Optionally, you can assign an existing DataSet object to the DataSet member variable. If the member variable points to a valid DataSet, any DataTable objects that the SelectDistinct method creates are added to the DataSet. In either case, the method call returns a reference to the DataTable object.

For additional information about DataSet objects, click the article number below to view the article in the Microsoft Knowledge Base:

313485 INFO: Roadmap for ADO.NET DataSet, DataView, and DataViewManager

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Windows XP, Windows 2000, or Windows NT 4.0 Service Pack 6a
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * Visual C# .NET syntax
 * ADO.NET fundamentals and syntax

back to the top

DataSetHelper Shell Class
The code in this section declares the shell class to which all DataSetHelper articles add methods and member variables.  Start Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual C# Projects under Project Types, and then click Class Library under Templates. In the Name box, type DataSetHelper .  Replace the class code with the following code: public class DataSetHelper {   public DataSet ds; public DataSetHelper(ref DataSet DataSet) {           ds = DataSet; }   public DataSetHelper {           ds = null; } } You can use the two overloads for the constructor to create an instance of the class with or without a reference to a valid DataSet. For a class that contains a reference to a valid DataSet, the DataTable objects that the methods return are also added automatically to the DataSet. 

NOTE: Add the following to the top of the code window: using System.Data; back to the top

SelectDistinct Method
This section contains the code for the SelectDistinct method and the private ColumnEqual helper method.   Add the following Private method to the class definition. This method is the same as the method that is used in other DataSetHelper articles. It is used to compare field values (including NULL). private bool ColumnEqual(object A, object B) { // Compares two values to see if they are equal. Also compares DBNULL.Value. // Note: If your DataTable contains object fields, then you must extend this // function to handle them in a meaningful way if you intend to group on them. if ( A == DBNull.Value && B == DBNull.Value ) // both are DBNull.Value return true; if ( A == DBNull.Value || B == DBNull.Value ) // only one is DBNull.Value return false; return ( A.Equals(B) ); // value type standard comparison }                   </li>  Add the following Public method to the class definition. This method copies unique values of the field that you select into a new DataTable. If the field contains NULL values, a record in the destination table will also contain NULL values. public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName) {          DataTable dt = new DataTable(TableName); dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType); object LastValue = null; foreach (DataRow dr in SourceTable.Select(&quot;&quot;, FieldName)) {           if (  LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])) ) {               LastValue = dr[FieldName]; dt.Rows.Add(new object[]{LastValue}); }       }        if (ds != null) ds.Tables.Add(dt); return dt; }                   </li></ol>

back to the top

Test Application
 Save and compile the DataSetHelper class that you created in the previous sections, and then close the solution.</li> Follow these steps to create a new Visual C# Windows Form application in Visual Studio .NET.  Start Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Application under Templates.</li></ol> </li> In Solution Explorer, right-click the solution and then click Add Existing Project. Add the DataSetHelper project.</li> On the Project menu, click Add Reference.</li> In the Add Reference dialog box, click the Projects tab, and then add a reference to the DataSetHelper project to the Windows Form application.</li> In the form designer, drag a Button control and a DataGrid control from the toolbox to the form. Name the button btnSelectDistinct, and then keep the default name for the DataGrid control (DataGrid1).</li>  In the form code, add the following Using statement to the top of the code window: using System.Data; </li>  Add the following variable declarations to the form definition: DataSet ds; DataSetHelper.DataSetHelper dsHelper; </li>  Add the following constructor code (below the InitializeComponent;): ds = new DataSet; dsHelper = new DataSetHelper.DataSetHelper(ref ds);

// Create source table DataTable dt = new DataTable(&quot;Orders&quot;); dt.Columns.Add(&quot;EmployeeID&quot;, Type.GetType(&quot;System.String&quot;)); dt.Columns.Add(&quot;OrderID&quot;, Type.GetType(&quot;System.Int32&quot;)); dt.Columns.Add(&quot;Amount&quot;, Type.GetType(&quot;System.Decimal&quot;)); dt.Rows.Add(new object[] {&quot;Sam&quot;, 5, 25.00}); dt.Rows.Add(new object[] {&quot;Tom&quot;, 7, 50.00}); dt.Rows.Add(new object[] {&quot;Sue&quot;, 9, 11.00}); dt.Rows.Add(new Object[] {&quot;Tom&quot;, 12, 7.00}); dt.Rows.Add(new Object[] {&quot;Sam&quot;, 14, 512.00}); dt.Rows.Add(new Object[] {&quot;Sue&quot;, 15, 17.00}); dt.Rows.Add(new Object[] {&quot;Sue&quot;, 22, 2.50}); dt.Rows.Add(new object[] {&quot;Tom&quot;, 24, 3.00}); dt.Rows.Add(new object[] {&quot;Tom&quot;, 33, 78.75});

ds.Tables.Add(dt); </li> <li> Add the following code to the btnSelectDistinct.Click event: dsHelper.SelectDistinct(&quot;DistinctEmployees&quot;, ds.Tables[&quot;Orders&quot;], &quot;EmployeeID&quot;); dataGrid1.SetDataBinding(ds, &quot;DistinctEmployees&quot;); </li> <li>Run the application, and then click the button one time. Notice that the DataGrid is populated with the tables and the data from the code.

NOTE: You can only click the btnSelectDistinct button one time. If you click the button more than one time, you receive an error message that you are trying to add the same table two times.</li></ol>

back to the top

Enhancement Ideas
You can only use the function to select a single, distinct field. However, you can extend the functionality to include multiple fields. Alternatively, you can call the CreateGroupByTable, the InsertGroupByInto, and the SelectGroupByInto methods, which use GROUP BY-type functionality, to get the same kind of results.

back to the top

Troubleshooting

 * The fieldname and alias parts of the field list must comply with DataColumn naming conventions. The parser also restricts the names, in that the name must not contain a period (.), a comma, or a space.
 * If you click the button more than one time, the same table is added two times to the DataSet, which results in an exception. To workaround this problem, you can add code to the test application to check whether a DataTable of the same name already exists. Alternatively, you can create the DataSetHelper class without a reference to a DataSet, and then bind the DataGrid.DataSource property directly to the dt variable instead of by using the SetDataBinding method call.
 * If the source table uses custom data types (that is, a class), you must add code to the SelectDistinct method to perform a deep copy of the data. Otherwise, only a reference is copied.

back to the top

Keywords: kbhowtomaster kbsystemdata KB326176

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.