Click here to Skip to main content
15,889,992 members
Home / Discussions / C#
   

C#

 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Agent__00725-May-15 22:43
professionalAgent__00725-May-15 22:43 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Alan N25-May-15 23:22
Alan N25-May-15 23:22 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Agent__00725-May-15 23:56
professionalAgent__00725-May-15 23:56 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Gyana_Ranjan Dash26-May-15 21:08
Gyana_Ranjan Dash26-May-15 21:08 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Armugam Indrani28-May-15 22:48
professionalArmugam Indrani28-May-15 22:48 
AnswerRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# PinPopular
harold aptroot26-May-15 1:02
harold aptroot26-May-15 1:02 
QuestionDataRelation problem Pin
Supratik De25-May-15 6:35
Supratik De25-May-15 6:35 
AnswerRe: DataRelation problem Pin
Richard Deeming27-May-15 1:19
mveRichard Deeming27-May-15 1:19 
Supratik De wrote:

C#
DataTable parentTable = new DataTable("Parents");
//parentTable.Columns.Add("CategoryId", typeof(int));
parentTable.Columns.Add("ParentCategoryId", typeof(int));
...
DataRelation parentChildRelation = new DataRelation("ParentChild", parentTable.Columns["CategoryId"], childTable.Columns["ParentCategoryId"]);

...
I am getting a ArgumentNullException on DataRelation line. The error is "'column' argument cannot be null.

parentTable doesn't contain a column called CategoryId, because the line which adds that column has been commented out.

From the description, it sounds like you only want a sinlge DataTable, with a self-referencing relationship.

You'll need to tell the DataRelation not to try to create constraints, since there is no row with a CategoryId of "-1". Alternatively, replace the "-1" in the ParentCategoryId column with null.

You can use the GetParentRow method[^] to get the parent row, and the GetChildRows method[^] to get the child rows.

If you set the table's PrimaryKey, you can use the Rows.Find method[^] to quickly find a row by its CategoryId.

This should get you started:
C#
DataTable theTable = new DataTable("Data");
theTable.Columns.Add("CategoryId", typeof(int));
theTable.Columns.Add("ParentCategoryId", typeof(int));
theTable.Columns.Add("Name", typeof(string));
theTable.Columns.Add("Keywords", typeof(string));

theTable.PrimaryKey = new DataColumn[] { theTable.Columns[0] };

theTable.Rows.Add(new object[] { 100, -1, "Business", "Money" });
theTable.Rows.Add(new object[] { 200, -1, "Tutoring", "Teaching" });
theTable.Rows.Add(new object[] { 101, 100, "Accounting", "Taxes" });
theTable.Rows.Add(new object[] { 102, 100, "Taxation", null });
theTable.Rows.Add(new object[] { 201, 200, "Computer", null });
theTable.Rows.Add(new object[] { 103, 101, "Corporate Tax", null });
theTable.Rows.Add(new object[] { 202, 201, "Operating System", null });
theTable.Rows.Add(new object[] { 109, 101, "Small Business Tax", null });
ds.Tables.Add(theTable);

DataRelation parentCategoryRelation = new DataRelation("ParentCategory", 
    theTable.Columns["CategoryId"],
    theTable.Columns["ParentCategoryId"],
    false); // Can't create constraints, as there is no row with CategoryId == -1.

ds.Relations.Add(parentCategoryRelation);


DataRow theRow = theTable.Rows.Find(201);
DataRow parentRow = theRow.GetParentRow(parentCategoryRelation);
DataRow[] childRows = theRow.GetChildRows(parentCategoryRelation);




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


QuestionInterface implementation strategies : four techniques; but: when (if) to use (which) one of them Pin
BillWoodruff25-May-15 4:06
professionalBillWoodruff25-May-15 4:06 
AnswerRe: Interface implementation strategies : four techniques; but: when (if) to use (which) one of them Pin
Richard Deeming27-May-15 1:29
mveRichard Deeming27-May-15 1:29 
QuestionToolstrip grey border Pin
Member 1171517124-May-15 6:52
Member 1171517124-May-15 6:52 
AnswerRe: Toolstrip grey border Pin
Ravi Bhavnani25-May-15 9:53
professionalRavi Bhavnani25-May-15 9:53 
QuestionReport From Date to Date Pin
abdo.kouta23-May-15 5:45
abdo.kouta23-May-15 5:45 
AnswerRe: Report From Date to Date Pin
OriginalGriff23-May-15 6:15
mveOriginalGriff23-May-15 6:15 
GeneralRe: Report From Date to Date Pin
abdo.kouta23-May-15 8:02
abdo.kouta23-May-15 8:02 
AnswerRe: Report From Date to Date Pin
Mycroft Holmes23-May-15 17:22
professionalMycroft Holmes23-May-15 17:22 
QuestionRe: Report From Date to Date Pin
abdo.kouta23-May-15 21:37
abdo.kouta23-May-15 21:37 
AnswerRe: Report From Date to Date Pin
Mycroft Holmes24-May-15 0:28
professionalMycroft Holmes24-May-15 0:28 
GeneralRe: Report From Date to Date Pin
abdo.kouta24-May-15 4:02
abdo.kouta24-May-15 4:02 
GeneralRe: Report From Date to Date Pin
Mycroft Holmes24-May-15 14:16
professionalMycroft Holmes24-May-15 14:16 
AnswerRe: Report From Date to Date Pin
jschell24-May-15 7:01
jschell24-May-15 7:01 
Questionasp.net with c# Pin
Member 1158609823-May-15 0:50
Member 1158609823-May-15 0:50 
AnswerRe: asp.net with c# Pin
Sascha Lefèvre23-May-15 1:18
professionalSascha Lefèvre23-May-15 1:18 
AnswerRe: asp.net with c# Pin
OriginalGriff23-May-15 2:05
mveOriginalGriff23-May-15 2:05 
QuestionEncypt a number to a 6 unique digits code Pin
Jassim Rahma22-May-15 13:04
Jassim Rahma22-May-15 13:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.