By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,795 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,795 IT Pros & Developers. It's quick & easy.

DataTable, Contains method

P: n/a
Hi

I'm trying to call the method Contains in a loop: out of the loop I set the
primary key of the dataTable.

The method returns true one time at a certani value, but then, when the loop
steps forward to check another value, the method returns false, although the
value is contained in the dataTable (DateTime values). So the loop goes and
goes ...

Also tried with Find, but I had the same result.

Can anyone help me?

Marco
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,

Can you post the code in question?

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Millo" <mi**************@libero.it> wrote in message
news:Gi*********************@twister2.libero.it...
Hi

I'm trying to call the method Contains in a loop: out of the loop I set the primary key of the dataTable.

The method returns true one time at a certani value, but then, when the loop steps forward to check another value, the method returns false, although the value is contained in the dataTable (DateTime values). So the loop goes and goes ...

Also tried with Find, but I had the same result.

Can anyone help me?

Marco

Nov 16 '05 #2

P: n/a
Millo <mi**************@libero.it> wrote:
I'm trying to call the method Contains in a loop: out of the loop I set the
primary key of the dataTable.

The method returns true one time at a certani value, but then, when the loop
steps forward to check another value, the method returns false, although the
value is contained in the dataTable (DateTime values). So the loop goes and
goes ...

Also tried with Find, but I had the same result.

Can anyone help me?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
[...]

private void setPrimaryKeys() // in order to use the Contains method of the
dataTable
{
DataColumn[] columnPrimaryKeys = new DataColumn[1];
columnPrimaryKeys[0] = this.Columns["date"];
this.PrimaryKey = columnPrimaryKeys;
}

public bool HasDate( DateTime date )
{
bool hasDate;
hasDate = this.Rows.Contains(date);
return hasDate;
}

public DateTime GetQuoteDateOrPreceding( DateTime date )
{
if(this.HasDate(date))
{
return date;
}
else
{
return GetQuoteDateOrPreceding(date.Subtract(new
TimeSpan(1,0,0,0)));
}
}
the problem is in the HasDate method: it works just one time: then
GetQuoteDateOrPreceding continues without returning a date because the
hasDate method fails to return true (although I see with my eyes - with the
VS debugger - that the dates passed as parameters ARE in the dataTable in
the column "date").
It seems to me very absurd ... I can't understand where I'm wrong (I've been
debugging for hours, but nothing.)

Thanks in advance.

"Jon Skeet [C# MVP]" <sk***@pobox.com> ha scritto nel messaggio
news:MP************************@msnews.microsoft.c om...
Millo <mi**************@libero.it> wrote:
I'm trying to call the method Contains in a loop: out of the loop I set the primary key of the dataTable.

The method returns true one time at a certani value, but then, when the loop steps forward to check another value, the method returns false, although the value is contained in the dataTable (DateTime values). So the loop goes and goes ...

Also tried with Find, but I had the same result.

Can anyone help me?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4

P: n/a
Millo,
Remember that a DateTime in .NET has both Date & Time information in it.

Are you certain that the DateTime you pass to HasDate only has a date in it
and the DateTimes in your datatables only have dates in them?

In other words, you don't have time floating around in any of your DateTime
objects? As Time (specifically fractions of milliseconds) would make
matching significantly more difficult...
I would probably add -1 days to the date instead
return GetQuoteDateOrPreceding(date.Subtract(new
TimeSpan(1,0,0,0)));
return GetQuoteDateOrPreceding(date.AddDays(-1));

Hope this helps
Jay
"Millo" <mi**************@libero.it> wrote in message
news:ss*********************@twister1.libero.it... [...]

private void setPrimaryKeys() // in order to use the Contains method of the dataTable
{
DataColumn[] columnPrimaryKeys = new DataColumn[1];
columnPrimaryKeys[0] = this.Columns["date"];
this.PrimaryKey = columnPrimaryKeys;
}

public bool HasDate( DateTime date )
{
bool hasDate;
hasDate = this.Rows.Contains(date);
return hasDate;
}

public DateTime GetQuoteDateOrPreceding( DateTime date )
{
if(this.HasDate(date))
{
return date;
}
else
{
return GetQuoteDateOrPreceding(date.Subtract(new
TimeSpan(1,0,0,0)));
}
}
the problem is in the HasDate method: it works just one time: then
GetQuoteDateOrPreceding continues without returning a date because the
hasDate method fails to return true (although I see with my eyes - with the VS debugger - that the dates passed as parameters ARE in the dataTable in
the column "date").
It seems to me very absurd ... I can't understand where I'm wrong (I've been debugging for hours, but nothing.)

Thanks in advance.

"Jon Skeet [C# MVP]" <sk***@pobox.com> ha scritto nel messaggio
news:MP************************@msnews.microsoft.c om...
Millo <mi**************@libero.it> wrote:
I'm trying to call the method Contains in a loop: out of the loop I
set
the primary key of the dataTable.

The method returns true one time at a certani value, but then, when
the
loop steps forward to check another value, the method returns false,
although
the value is contained in the dataTable (DateTime values). So the loop
goes
and goes ...

Also tried with Find, but I had the same result.

Can anyone help me?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #5

P: n/a
Millo <mi**************@libero.it> wrote:
[...]

private void setPrimaryKeys() // in order to use the Contains method of the
dataTable


This isn't a complete program. Please read the link I posted before.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
Jay,
thank you for your tips.

In my DataTable I had only dates: but not in the parameters I passed to the
HasDate(date) method.
using: <<this.Rows.Contains(date.Date)>> and not simply
<<this.Rows.Contains(date)>> in this method, I made it work properly.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> ha scritto nel
messaggio news:e6**************@TK2MSFTNGP11.phx.gbl...
Millo,
Remember that a DateTime in .NET has both Date & Time information in it.

Are you certain that the DateTime you pass to HasDate only has a date in it and the DateTimes in your datatables only have dates in them?

In other words, you don't have time floating around in any of your DateTime objects? As Time (specifically fractions of milliseconds) would make
matching significantly more difficult...
I would probably add -1 days to the date instead
return GetQuoteDateOrPreceding(date.Subtract(new
TimeSpan(1,0,0,0)));


return GetQuoteDateOrPreceding(date.AddDays(-1));

Hope this helps
Jay
"Millo" <mi**************@libero.it> wrote in message
news:ss*********************@twister1.libero.it...
[...]

private void setPrimaryKeys() // in order to use the Contains method of

the
dataTable
{
DataColumn[] columnPrimaryKeys = new DataColumn[1];
columnPrimaryKeys[0] = this.Columns["date"];
this.PrimaryKey = columnPrimaryKeys;
}

public bool HasDate( DateTime date )
{
bool hasDate;
hasDate = this.Rows.Contains(date);
return hasDate;
}

public DateTime GetQuoteDateOrPreceding( DateTime date )
{
if(this.HasDate(date))
{
return date;
}
else
{
return GetQuoteDateOrPreceding(date.Subtract(new
TimeSpan(1,0,0,0)));
}
}
the problem is in the HasDate method: it works just one time: then
GetQuoteDateOrPreceding continues without returning a date because the
hasDate method fails to return true (although I see with my eyes - with

the
VS debugger - that the dates passed as parameters ARE in the dataTable in the column "date").
It seems to me very absurd ... I can't understand where I'm wrong (I've

been
debugging for hours, but nothing.)

Thanks in advance.

"Jon Skeet [C# MVP]" <sk***@pobox.com> ha scritto nel messaggio
news:MP************************@msnews.microsoft.c om...
Millo <mi**************@libero.it> wrote:
> I'm trying to call the method Contains in a loop: out of the loop I

set
the
> primary key of the dataTable.
>
> The method returns true one time at a certani value, but then, when

the
loop
> steps forward to check another value, the method returns false,

although
the
> value is contained in the dataTable (DateTime values). So the loop

goes
and
> goes ...
>
> Also tried with Find, but I had the same result.
>
> Can anyone help me?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too





Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.