DataTable, Contains method

M

Millo

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
 
J

Jon Skeet [C# MVP]

Millo said:
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.
 
M

Millo

[...]

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.
 
J

Jay B. Harlow [MVP - Outlook]

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 said:
[...]

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.

set
the
although
goes
 
J

Jon Skeet [C# MVP]

Millo said:
[...]

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.
 
M

Millo

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 said:
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 said:
[...]

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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top