Usung C Sharp, I have a dataset that has 4 tables in it, I'm populating a table with a sql command, and then setting that table as the datamember programattically as this:
private void LoadReason() { ugReason.DataBindings.Clear(); string tabname = ""; if (_audit) { tabname = "DXAdditionAuditReasons"; } else { tabname = "DXAdditionReasons"; } DataTable ttable = Requests.SQLGen.ProcessSQLCommand(gobj, "Select * from "+tabname+" where DXAddID = " + _row["ID"], false); if (ttable != null) { if (_audit) { ttable.TableName = "DxAudReason"; dx.Merge(ttable); ugReason.SetDataBinding(dx,"DxAudReason"); ugReason.DisplayLayout.Bands[0].Columns["ID"].Hidden = true; ugReason.DisplayLayout.Bands[0].Columns["DXAddID"].Hidden = true; ugReason.DisplayLayout.Bands[0].Columns["ReasonID"].Header.Caption = "Reason"; LoadValueList(); ugReason.DisplayLayout.Bands[0].Columns["ReasonID"].ValueList = ugReason.DisplayLayout.ValueLists["Reason"]; ugReason.Refresh(); } else { ttable.TableName = "DxAddReason"; dx.Merge(ttable); ugReason.SetDataBinding(dx, "DxAddReason"); ugReason.DisplayLayout.Bands[0].Columns["ID"].Hidden = true; ugReason.DisplayLayout.Bands[0].Columns["DXAddID"].Hidden = true; ugReason.DisplayLayout.Bands[0].Columns["ReasonID"].Header.Caption = "Reason"; ugReason.DisplayLayout.Bands[0].Columns["ReasonID"].ValueList = ugReason.DisplayLayout.ValueLists["Reason"]; ugReason.Refresh(); } } }
When I get to the '.SetDataBinding' I get:
Error Message: Key already existsParameter name: Key
I'm clearing the databindings already, so there shouldn't be any keys to exist yet, until I set it there in code.
Hi,
The only reason you would get an error like this is if your data source structure has columns with duplicate keys.
Note that a child band is also a column. So if you have a data source with a column named "A" and also a child relationship named "A", that would cause an error like the one you are getting here.
Also, column keys are case insensitive. So if you have a column named "A" and another named "a", that will also cause a problem.
Another possibility is that the error is occurring elsewhere in your code, like in the InitializeLayout event if you are trying to add an unbound column that has the same key as an existing column. Sometimes these errors can be masked by DotNet and the line of code the error occurs on isn't the one that is highlighted. Try setting the Visual Studio IDE to break on all run-time exceptions so you are certain it's breaking on the correct line.
And... if none of that helps, see if you can post the call stack of the exception or a small sample project demonstrating the exception and we can look into it further.
Thanks for the reply Mike. I did get it to work by clearing the xsd constraints and it is now working like it is supposed to. Stepping through the process, even though I did the databindings.clear it still left the grid with a datasource and datamember, even though the binging manager was showing with nothing there. some of the old constraints were still present apparently.