tags:

views:

111

answers:

4

Let's say I have a DataTable:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Country", typeof(string)));
dt.Columns.Add(new DataColumn("State", typeof(string)));

Now I populate it

DataRow dn = dt.NewRow();
dn["Country"] = "1";
dn["State"] == "2";

// Add it
dt.Rows.Add(dn);

That works fine. Now if Country and State are zero, I don't want it to add to the table. For example:

dn["Country"] = "0";
dn["State"] = "0";

if (dn["Country"].ToString() != "0" &&
    dn["State"].ToString() != "0")
   dt.Rows.Add(dn);

That doesn't work. What am I doing wrong there? I've tried setting type to int and that didn't help much either...

Thanks, Jim

Update:

This is more like what I'm doing:

dn["Country"] = (from c in db.Country where c.Zone == 3 select c.Code).Count();

Now if that were to return Zero, the row should not be added to the DataTable. What instead happens is that if the count is greater than 0, its not added either.

Update 2

Got it to work with Any() for now, as suggested by Jon Skeet below. Thanks everyone!

+1  A: 

Your populate code is wrong, i don't know if this is a typo or directly from your source.

You wrote

//the "==" won't assign anything to the dn["State"] column
dn["State"] == "2";

where you actually need

//the "=" will
dn["State"] = "2";

Because the State column never gets populated your if condition won't work.

if (dn["Country"].ToString() != "0" 
 && dn["State"].ToString() != "0") 
//this will always be false because dn["State"] was never assigned to

Another point is that if the column hasn't had a value specified the value will be DBNull. You should perhaps modify your check to also see if the values are DBNull, you can do this using the IsNull method.

I hope this helps :-)

DoctaJonez
Except that *will* work with the snippet which sets them both to "0"
Jon Skeet
I think it was a typo.
rahul
Sorry, was a typo.
Jim
Good point Jon, I didn't notice the assignment in the 3rd snippet.
DoctaJonez
+2  A: 

By "that doesn't work" do you mean the row gets added anyway? That sounds very odd - do you have a short but complete program which demonstrates the problem?

Here's a short but complete program which does work:

using System;
using System.Data;

class Test
{
    static void Main()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Country", typeof(string)));
        dt.Columns.Add(new DataColumn("State", typeof(string)));

        DataRow dn = dt.NewRow();
        dn["Country"] = "0";
        dn["State"] = "0";

        if (dn["Country"].ToString() != "0" &&
            dn["State"].ToString() != "0")
        {
            Console.WriteLine("Broken");
            dt.Rows.Add(dn);
        }
        else
        {
            Console.WriteLine("Working");
        }
    }
}

It's hard to tell exactly what you're doing based on snippets, but is it possible that you're changing the values of a row which is already in the DataTable, instead of one which hasn't been added yet?

EDIT: This is a pretty strange line of code you've got:

dn["Country"] = (from c in db.Country where c.Zone == 3 select c.Code).Count();

Why would you set the country to a count? Furthermore, if you only care about the count why are you bothering with the projection?

Finally, I'd probably just use a local variable instead of putting it in the datarow to start with:

int count = db.Country.Where(c => c.Zone == 3).Count();
if (count != 0)
{
    // Use count, add the row etc
}

Oh, and if you don't really need the count, but just need to know whether it's non-zero, use Any:

if (db.Country.Any(c => c.Zone == 3))
{
    // ...
}
Jon Skeet
This is more like what I'm doing:dn["Country"] = (from c in db.Country where c.Zone == 3 select c.Code).Count();Now if that were to return Zero, the row should not be added to the DataTable. What instead happens is that if the count is greater than 0, its not added either.
Jim
Formatting messed up: I've updated the main post with my reply.
Jim
You haven't said whether you're reusing an existing row or creating a new one.
Jon Skeet
+1 for the point about Any().
DoctaJonez
Jon, that was an example. I'm not sure what is going wrong but none of the methods work. The comparison fails irrespective of the count. I'll try and work on a sample. Thanks again.
Jim
Got it to work with Any() for now. Thanks Jon.
Jim
@Jim: The closer your examples are to reality, the more chance we have of working out what's wrong.
Jon Skeet
A: 

Try putting "0" in a string variable and then comparing. Probably doesn't like the raw string.

Edit

I wrote a small test app with the following:

var dt = new DataTable("Table1");
dt.Columns.Add("Country");
dt.Columns.Add("State");
DataRow r = dt.NewRow();
r["Country"] = 7;  // alternatively changed this value to 0 and also changed it to a string
r["State"] = 7;  // alternatively changed this value to 0 and also changed it to a string

if (r["Country"].ToString() != "0" && r["State"].ToString() != "0")
{
    dt.Rows.Add(r);
}

And the code worked as expected...

The only thing I can think of is that Country - State are never both above 0.

Edit 2

I looked at your LINQ expression.... try changing "select c.Code" to "select c". Could be the problem.

James
Try the same thing using LINQ as with my edit in the main post... Doesn't work for me.
Jim
The problem must be to do with the assignment of the LINQ expression to the data table. Try getting the value and storing it in a variable before assigning it to the DataTable
James
A: 

The problem is that you are assigning an int (that will be boxed to object) to the DataRow:

dn["Country"] = (from c in db.Country where c.Zone == 3 select c.Code).Count();

And then comparing it to a string:

if (dn["Country"].ToString() != "0"

Try changing Count() to Count().ToString() and the problem may be solved.

Jason
the .ToString() does the conversion of whatever datatype is in the dn["Country"] field anyways...
James
That's true; for some reason I didn't catch the .ToString() as part of the comparison.
Jason