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

table.rows.find problem

P: n/a
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();
}
}
}
May 10 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Derek" <De***@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
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();
}
}
}

May 10 '06 #2

P: n/a
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.
May 10 '06 #3

P: n/a
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?
May 10 '06 #4

P: n/a
Hi,

"Derek" <De***@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
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 } );
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
May 10 '06 #5

P: n/a
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
"Derek" <De***@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
| 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();
| }
| }
| }
May 11 '06 #6

P: n/a
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.
May 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.