FADV
FADV

Reputation: 35

compare two datatable value in if statement

 for (int i = 0; i < dtblAllDB.Rows.Count; i++)
 {                 
      if ((table.Rows[i]["customer_id"].ToString() && table.Rows[i]["time"].ToString() ) != (dtblAllDB.Rows[i]["customer_id"].ToString() && dtblAllDB.Rows[i]["time"].ToString() ))
      {

      }
 }

I need to compare two datatable column values, but I am getting the error, here table and dtblAllDB are the datatables..

Operator '&&' cannot be applied to operands of type 'string' and 'string'

Upvotes: 1

Views: 2551

Answers (4)

Siddique Mahsud
Siddique Mahsud

Reputation: 1453

Use plus sign(+) to concatenate two strings in c#

if ((table.Rows[i]["customer_id"].ToString() + table.Rows[i]["time"].ToString()) != (dtblAllDB.Rows[i]["customer_id"].ToString() + dtblAllDB.Rows[i]["time"].ToString()))
   { }

OR

if (table.Rows[i]["customer_id"].ToString() != dtblAllDB.Rows[i]["customer_id"].ToString() && table.Rows[i]["time"].ToString() != dtblAllDB.Rows[i]["time"].ToString())
   { }

Upvotes: 0

Adil
Adil

Reputation: 148170

The && operand could be applied to bool but not string, your condition would be something like

for (int i = 0; i < dtblAllDB.Rows.Count; i++)
{
    if (!(table.Rows[i]["customer_id"].ToString() == dtblAllDB.Rows[i]["customer_id"].ToString() 
    && table.Rows[i]["time"].ToString()  ==  && dtblAllDB.Rows[i]["time"].ToString()))
    {
    }
}

Although this does not make much sense as it will compare the tables row by row and filter out row which does not match both cutomer_id and time

If you want to compare two tables irrespective of row position then you need two loops like shown below.

for(int k = 0; k < table.Rows.Count; k++)
{
    for (int i = 0; i < dtblAllDB.Rows.Count; i++)
    {
        if (!(table.Rows[k]["customer_id"].ToString() == dtblAllDB.Rows[i]["customer_id"].ToString() 
        && table.Rows[k]["time"].ToString()  ==  && dtblAllDB.Rows[i]["time"].ToString()))
        {
        }
    }
}

Upvotes: 1

waka
waka

Reputation: 3417

You need to compare every value seperatly. I think this is what you want to do:

if ((table.Rows[i]["customer_id"].ToString() != dtblAllDB.Rows[i]["customer_id"].ToString())
    && ((table.Rows[i]["time"].ToString() != dtblAllDB.Rows[i]["time"].ToString())
    && (table.Rows[i]["customer_id"].ToString() != dtblAllDB.Rows[i]["time"].ToString())
    && (table.Rows[i]["time"].ToString() != dtblAllDB.Rows[i]["customer_id"].ToString()))

Upvotes: 0

autopilot
autopilot

Reputation: 362

ToString() returns a string that represents the current object. Use

Convert.ToString(table.Rows[i]["customer_id"])

in every case. And also use == to compare between strings

Upvotes: 0

Related Questions