Dear All,

I have a value, strObjectAPIID, and I am comparing it to a dataset value in a loop. What i want is, if the value is equal, do not do anything, but if it is not equal, write it to the table.

However, i have to check the value with the whole dataset, for example i have the following:-

strObjectAPIID = abc123

and the values in the dataset are aaa123, aab123, abc123.

So the first value is not equal, however the third value is equal, so I am not supposed to write it in the table.

So basically, what I want to do is, if strObjectAPIID is not existing in the whole dataset, then commit to table.

At the moment my current code is like this:-

for (int j = 0; j < dsExisitingCodes.Tables[0].Rows.Count; j++)
{
rowCounter = dsExisitingCodes.Tables[0].Rows.Count;
strTableAPIID = (dsExisitingCodes.Tables[0].Rows[j][0].ToString());

if ( strObjectAPIID == strTableAPIID)
{
bFound = true;
break;
}
}

And what happens is that if the value is not equal, then I am loosing it. How can i arrange it to commit to the database?

Recommended Answers

All 4 Replies

Have you initialised bFound to False anywhere ?

try this:

bFound = false;
rowCounter = dsExisitingCodes.Tables[0].Rows.Count;
for (int j = 0; j < dsExisitingCodes.Tables[0].Rows.Count && !bFound; j++){
	strTableAPIID = dsExisitingCodes.Tables[0].Rows[j][0].ToString();
	bFound = (strObjectAPIID==strTableAPIID);
}

Hi,

The previous post is pretty much optimized and must have solved your problem but if you have many entires on your DB you might want to use an SQL query like (Select * from <tablename> where <fieldname> = '<idyousearchfor>') and then just check for wether the number of rows returned is greater then 0.

Loren Soth

yeah, duh, why didn't i think of that. fix this in your sql query, then you don't need to do a loop at all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.