# How to compare each row of the column RSS

## 5 replies

Last post Apr 09, 2012 07:01 PM by customer22

Member

67 Points

164 Posts

### How to compare each row of the column

Hello:

I need to compare the third column: Month each row. For example: the second row of Month at original table is 2,

The comparable table is 3. The twelfth column of Month at original table is 12. The comparable table is 13.

If there is the difference, give the message: it is different.

Is any idea how to accomplish this?

Thank you very much

Original Table

```Name NameID  Month    Base    Factor
JJ 22 1 1 0
JJ 22 2 2 0
JJ 22 3 3 0
JJ 22 4 4 0
JJ 22 5 5 0
JJ 22 6 6 0
JJ 22 7 7 0
JJ 22 8 8 0
JJ 22 9 9 0
JJ 22 10 10 0
JJ 22 11 11 0
JJ 22 12 12 0

Comparable table

Name    NameID  Month    Base    Factor
JJ    22    1    1    0
JJ    22    3    3    0
JJ    22    4    4    0
JJ    22    5    5    0
JJ    22    6    6    0
JJ    22    7    7    0
JJ    22    8    8    0
JJ    22    9    9    0
JJ    22    10    10    0
JJ    22    11    11    0
JJ    22    12    12    0
JJ    22    13    12    0```

All-Star

16310 Points

2773 Posts

### Re: How to compare each row of the column

***DON'T FORGET TO CLICK “MARK AS ANSWER” ON THE POST IF HELPED YOU.

Star

10672 Points

2426 Posts

### Re: How to compare each row of the column

Do you mean that you want to check  whether the values in  corresponding  rows of column 3 in  both the tables are identical or not?

Are they Database tables? Please make it clear.

B

Kindly mark this post as "Answer", if it helped you.

(Talk less..Work more)

Member

67 Points

164 Posts

### Re: How to compare each row of the column

Thanks for all responses. Yes, I would like to check the values in corresponding each row of column 3 in both the tables are identical. They are database tables.

Star

10672 Points

2426 Posts

### Re: How to compare each row of the column

Try something like this...

```private void matchColumns()

{
string constr = ConfigurationManager.ConnectionStrings["YourString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();

string qry1 = "SELECT * FROM tbloriginal";
SqlCommand cmd1 = new SqlCommand (qry1,con);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
string qry2 = "SELECT * FROM tblcompare";
SqlCommand cmd2 = new SqlCommand(qry2, con);
DataTable dt2 = new DataTable();
da2.Fill(dt2);

for (int I = 0; I <= dt1.Rows.Count - 1; I++)
{
string value1 = dt1.Rows[I][2].ToString();
string value2 = dt2.Rows[I][2].ToString();
string rowId = dt1.Rows[I][1].ToString();

if(value1 != value2)
{
Response.Write ("For NameId " + rowId + ": Column 2 mismatch <br>");
}
}```
```           con.Close();
}```

Kindly mark this post as "Answer", if it helped you.

(Talk less..Work more)

Member

67 Points

164 Posts