RSS Forums RSS
Please support our C# advertiser: Programming Forums
Views: 1217 | Replies: 4
Reply
Join Date: Apr 2006
Posts: 1
Reputation: monfu is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
monfu monfu is offline Offline
Newbie Poster

getting details from the loop

  #1  
Apr 18th, 2006
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: getting details from the loop

  #2  
Apr 18th, 2006
Have you initialised bFound to False anywhere ?
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 482
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: getting details from the loop

  #3  
Apr 18th, 2006
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);
}
Reply With Quote  
Join Date: Mar 2006
Posts: 219
Reputation: Lord Soth is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 3
Lord Soth's Avatar
Lord Soth Lord Soth is offline Offline
Posting Whiz in Training

Re: getting details from the loop

  #4  
Apr 19th, 2006
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
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 482
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: getting details from the loop

  #5  
Apr 19th, 2006
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:10 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC