Select the product you need help with
- Internet Explorer
- Windows Phone
- More products
DataSet contains duplicate records based on the SQL outer join query
Article ID: 317058 - View products that this article applies to.
This article was previously published under Q317058
This article refers to the following Microsoft .NET Framework Class Library namespaces:
If you call the Fill method of the DataAdapter object twice on a DataTable in a DataSet that is based on a SQL outer join statement, if you do not first clear the current contents of the DataTable, the DataTable may contain two copies of each record.
The records are duplicated because a DataTable that is based on a SQL outer join query does not contain primary key information to identify each record uniquely. Instead of merging with the existing records based on key information, a second set of exactly the same records is appended.
To resolve this problem, clear the DataTable before you reload the data. To do this, call the Clear method of the DataTable or the DataSet before you call the Fill method of the DataAdapter the second time.
This is the recommended method. This method is more efficient than if you require the DataAdapter to merge the new data with the existing data.
A SQL outer join query requests records from two or more tables, even when some of the records in one table may have no matching records in the other table. For example, if you want a list of all of the customers in the Northwind sample database, including those who had never placed an order, you use the following query:
In the case of a customer who had not placed any orders, the results appear as follows:
Collapse this tableExpand this table
Because the Primary Key column of the Orders table (OrderID) may contain these NULL values, Microsoft SQL Server marks the column as nullable. However, because the DataSet does not accept a nullable Primary Key column, the DataSet does not import any primary key information with the table meta data. The lack of primary key information causes the records to be duplicated instead of merged.
Steps to reproduce behavior
Article ID: 317058 - Last Review: December 6, 2006 - Revision: 4.5