Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
0 down vote
favorite
I am comparing two DataTable, if the result is false I add row to my database table. If the table database is empty it works, but when I have values in it, I got "Column 'Tags' does not belong to table". The first DataTable data is Filled from database table.

the code of comparing is the following:

C#
DataTable result = new DataTable(); 
result.Columns.Add("Tags", typeof(string));

 for (int k = 0; k < result.Rows.Count; k++)
 {
     for (int j = 0; j < tableTags.Rows.Count; j++)
      {
         if (tableTags.Rows[k]["Tags"].ToString().Equals(result.Rows[j]["TagName"].ToString()))
          {
            continue;
          }
         else
         {
           AddTags(result.Rows[k]["Tags"].ToString());
           break;
          }
      }
   }
Posted
Comments
phil.o 30-Apr-14 18:30pm    
Is it the real code? Because, as is, you create a DataTable, give it one column, and then iterate over its Rows collection, which at that time is empty. So you never compare anything to anything.
Member 10672813 30-Apr-14 18:33pm    
No it is example code, the two tables have data, so actually rows are not empty, one DataTable have column name "Tags" and the other "TagName", I am comparing these to each other.
ZurdoDev 30-Apr-14 20:34pm    
The error should be clear. That column is not in your datatable. You need to post the actual relevant code.

You wrote:
C#
if (tableTags.Rows[k]["Tags"].ToString().Equals(result.Rows[j]["TagName"].ToString()))


It should be:
C#
if (tableTags.Rows[k]["TagName"].ToString().Equals(result.Rows[j]["Tags"].ToString()))


You named the column of result variable "Tags"; I supposed you just switched their names. Why not giving them the same name?

Another possible solution is to put a breakpoint on the first line and launch the solution in debug mode. Then see line by line what the variables you declare actually contain.
 
Share this answer
 
Hi, Here is a sample code to do the comparison between 2 datatables:

DataRow dr;
        DataTable result = new DataTable();
        result.Columns.Add("Tags", typeof(string));
        //
        dr = result.NewRow();
        dr[0] = "a";
        result.Rows.Add(dr);
        //
        dr = result.NewRow();
        dr[0] = "b";
        result.Rows.Add(dr);
        //
        dr = result.NewRow();
        dr[0] = "c";
        result.Rows.Add(dr);
        result.Rows.Add();
        //
        DataTable tableTags = new DataTable();
        tableTags.Columns.Add("Tags", typeof(string));
        //
        dr = tableTags.NewRow();
        dr[0] = "A";
        tableTags.Rows.Add(dr);
        //
        dr = tableTags.NewRow();
        dr[0] = "e";
        tableTags.Rows.Add(dr);
        //        
        for (int k = 0; k < result.Rows.Count; k++)
        {
            var match = from tag in tableTags.AsEnumerable() where tag.Field<String>("Tags").ToLower() == result.Rows[k]["Tags"].ToString().ToLower() select tag;
            if (match.Any())
            {
                //The tag exists, write related code here.
            }
            else
            {
                //The tag does not exist, write related code here.
            }            
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900