473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DataTable, Contains method

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
6 17409
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
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
[...]

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: gabyluna | last post by:
Hi, I´m trying to use the Select method for the DataTable objects of the System.Data library. The problem is that when the datatype of the object that's being searched is an string, I get the...
6
by: Mountain Bikn' Guy | last post by:
When one gets a row from a database (ie, a DataTable), the row contains a typed value in each column. How is this typically implemented behind scenes. I want to build this functionality myself. The...
2
by: | last post by:
Hello all I have a dataset which contains 3 datatables. I traverse each datatable and check the rows. I should be able to get values out of these datatables and create a new datatable? Is this...
3
by: Frans Bouma | last post by:
Hi, I have a serious problem with VB.NET and a DataTable derived class and I can't figure out how to solve it. I have implemented it in C# where it works perfectly, but I can't port one...
4
by: George | last post by:
Got a question about the side effect of DataAdapter.Update() and DataTable.GetChanges(). Say I set up a DataTable and a DataAdapter in a class. Delete (Not remove) a row in the data table and...
4
by: Joseph Geretz | last post by:
I don't get it. A DataTable can't be returned, but a DataSet can. Yet a DataSet contains one or more DataTables. So obviously a DataTable must be serializable. So why not just let me return a...
5
by: Frank Hauptlorenz | last post by:
Hello, I recognized some days ago, that returning a DataTable blocks my WCF-Service. Is this a known bug? If I add this table to a new DataSet() and return this, it works. Thank you, Frank
4
by: ArunDhaJ | last post by:
Hi, I've to filter rows from DataTable Select There is a column named "Phone" which contains values in the following format: (901) 789 1234<BR>(901) 789 1235<BR>(901) 789 1221 I need to...
0
by: Craig Buchanan | last post by:
I would like to override the DataTable.Rows.Contains() method to encapsulate the DataTable's DefaultView's FindRows() method. It would be nice to be able to test an array of alternate keys in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.