Edit a C# table adapter and get hundreds of duplicate attribute errors

Today I ran into a rather bizarre problem that appears to be a bug in Visual Studio itself. It is always fun when the auto-magical functionality of Visual Studio breaks down.

After editing a table adapter in a dataset I hit compile and was greeted with several hundred compilation errors all suggesting that I was redefining things. The locations in the code were relatively nonsensical plus this code compiled 30 seconds ago. Lots of lots of errors like these…

Error CS0579 Duplicate ‘global::System.Serializable’ attribute

Error CS0579 Duplicate ‘global::System.ComponentModel.DesignerCategoryAttribute’ attribute

Digging through the uncommitted changes revealed that the csproj file of the project containing the dataset had been modified in an unusual way. The dataset’s dataset.designer.cs had been joined by a newly created dataset1.designer.cs. For an unknown reason rather than editing the original designer file Visual Studio had left the original alone and created a duplicate designer file alongside it. Both were included in the project and both were essentially identical which explained all of the “already defined” type errors.

After some googling here is how I fixed it.

1) Close Visual Studio

2) Delete dataset.designer.cs

3) Rename dataset.designer1.cs to dataset.designer.cs

4) Using notepad or similar open the affected csproj file. In one or two places I changed a reference to dataset.designer1.cs to instead reference dataset.designer.cs. There was one section that I simply deleted. I entirely removed the Compile block that was including the erroneously created designer file. Below you can see the one we want and the one that has to be removed.

<Compile Include="DataSet.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>DataSet.xsd</DependentUpon>
</Compile>
<Compile Include="DataSet1.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>DataSet.xsd</DependentUpon>
</Compile>

5) Make sure that there are absolutely no references to dataset.designer1.cs left in the csproj file. Close and save it.

6) Reopen Visual Studio. Successfully compile.

A very odd problem. Fiddly but simple enough to fix once you know how. Hopefully I will not have to do it too often.

Leave a Reply