Ruby
Ruby

Reputation: 969

How to create a DataRelation in C#

I get an error when creating a datarelation - 'DataRelation does not contain a constuctor that takes 3 arguments. Where am I going wrong.

DataColumn dcParent = ds.Tables["Directors"].Columns["did"];
DataColumn dcChild = ds.Tables["Movies"].Columns["did"];

//Error here
DataRelation MovieDirectors = new DataRelation("dr1", dcParent, dcChild);

Upvotes: 0

Views: 1131

Answers (4)

AMissico
AMissico

Reputation: 21684

From comments:

You are correct, so the problem must be somthing else. I suspect DataRelation is not the System.Data.DataRelation. Make sure you got the correct class. – AMissico

Thank you AMissico. That worked. But why is it so. All the examples I see just said 'DataRelation'. Is there any other datarelation in some other namespace? – Ruby

Yes, you have a namespace conflict. It depends on your references and what namespaces you added through the using directive. A "namespace alias" is helpful in these cases or use fully qualified names, see Using Namespaces (C# Programming Guide) for more information.

Upvotes: 3

RAMARAJ.M
RAMARAJ.M

Reputation: 129

Intitialzing a DataRelation is quite simple; you can use the basic constructor. In your case, something like:

DataRelation moviedirector = new DataRelation("dr1", ds.Tables["Directors"].Columns["did"], ds.Tables["Movies"].Columns["did"];);

ds.Relations.Add(moviedirector);

Upvotes: 0

RAMARAJ.M
RAMARAJ.M

Reputation: 129

Try this

ds.Relations.Add(MovieDirectors);

Upvotes: 0

Saurabh
Saurabh

Reputation: 5727

Use this:

DataRelation MovieDirectors = new DataRelation("dr1", dcParent, dcChild,true);

Upvotes: 0

Related Questions