table.rows.find problem

G

Guest

The following code never finds a matching row even when one exists. It
appears that the method thinks there is only one value being passed to the
method even though the object array that is passed has 2 elements, and both
elements have values.

Any ideas?

if ((dsReturned.Tables.Count > 0) && (dsReturned.Tables[0].Rows.Count > 0))
{
object[] objKeys = new object[2];
DataRow drFound;

foreach (DataRow dr in dsReturned.Tables[0].Rows)
{
objKeys[0] = dr["ItemID"]; // String
objKeys[1] = dr["ItemTrackDateTime"]; // DateTime
drFound = dsItemTracks.Tables[0].Rows.Find(objKeys);

if (drFound != null)
{
drFound.Delete();
}
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Make sure that the searched table ( dsItemTracks.Tables[0] ) define the
primary keys in the same order.

Alternatively you can iterate the table manually and see what happens when
the expected row is found.
 
G

Guest

Hi,

All keys seem to be defined correctly. When I try to run
"?dsItemTracks.Tables[0].Rows.Find(objKeys)" in the Command Window I get the
following error:
{"Expecting 2 value(s) for the key being indexed, but received 1 value(s)." }
System.SystemException: {"Expecting 2 value(s) for the key being
indexed, but received 1 value(s)."}
m_paramName: null
Message: "Expecting 2 value(s) for the key being indexed, but received 1
value(s)."
ParamName: null

I'm not sure if this is caused by running it in the CW or if it is the
underlying cause in the code.

I checked all keys, and the object array and everything seems in order.
 
G

Guest

PS:

I believe there is a way to declare the key values (not just a single key
value) directly in the Find method.

e.g. drFound = dsItemTracks.Tables[0].Rows.Find("Something in here");

Is there an example of this I could see?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Derek said:
PS:

I believe there is a way to declare the key values (not just a single key
value) directly in the Find method.

e.g. drFound = dsItemTracks.Tables[0].Rows.Find("Something in here");

you can do

drs Found = dsItemTracks.Tables[0].Rows.Find( new object[] { 'key1',
DateTime.Now } );
 
J

Jay B. Harlow [MVP - Outlook]

Derek,
In addition to the other comments. Remember that Dates have times associated
with them.

Are you certain that both tables have the same values date & time in the
DateTime column? To the same precision...

FWIW: I would consider setting up a DataRelation between the two tables,
them simply use the children of the relationship (via DataRow.GetChildRows)
rather then using Rows.Find...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| The following code never finds a matching row even when one exists. It
| appears that the method thinks there is only one value being passed to the
| method even though the object array that is passed has 2 elements, and
both
| elements have values.
|
| Any ideas?
|
| if ((dsReturned.Tables.Count > 0) && (dsReturned.Tables[0].Rows.Count >
0))
| {
| object[] objKeys = new object[2];
| DataRow drFound;
|
| foreach (DataRow dr in dsReturned.Tables[0].Rows)
| {
| objKeys[0] = dr["ItemID"]; // String
| objKeys[1] = dr["ItemTrackDateTime"]; // DateTime
| drFound = dsItemTracks.Tables[0].Rows.Find(objKeys);
|
| if (drFound != null)
| {
| drFound.Delete();
| }
| }
| }
 
G

Guest

Thanks for the info guys. This problem is intriguing me. I think the problem
lies in:

dsItemTracks.Tables[0].Rows.Find(objKeys);

When I put a watch on this, I get a value of:

{"Expecting 2 value(s) for the key being indexed, but received 1 value(s)." }

There are definately 2 key columns defined in dsItemTracks, and objKeys is
positively an array of objects with 2 elements in the same order, and of the
same types as the keys for the table.
 

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