simple question if and do while vb.net

Discussion in 'Software' started by unclematty, Feb 29, 2004.

  1. unclematty

    unclematty Private First Class

    I am trying to run a do while only if the .net datareader is populated...here is what i was doin...

    If DataReader.Read() Then
    'a row was returned
    Do While DataReader.Read()
    intDatareaderColCount = DataReader.FieldCount
    For intCounter = 0 To intDatareaderColCount - 1
    xmlResult = xmlResult & "<" & DataReader.GetName(intCounter) & ">" &DataReader.GetValue(intCounter) & "</" & DataReader.GetName(intCounter) & ">"
    Next
    Loop
    Else 'no rows are returned
    'lookup word at google
    'rerun search with word as symptom
    'still nothing? return error
    xmlResult = xmlResult & "<error>No data retreived</error>"
    End If

    what is happeneing is...when the reader is populated, it hits the first line of the do while, " Do While DataReader.Read()" but then jumps to the end if as if the if statement confirmed that the reader was populated and the while loop confirmed that it was not populated.

    any help?
     
  2. iamien

    iamien Cptn "Eh!"

    i dont do any .net but nromaly a do while loop is
    Do
    statesments
    while (conditions)
    this way it ensures at least one run of the program statements. perhaps you just what a while loop?


    Oh and please use code tags
    Code:
    	do 
    		{
    			
    			cout<<endl <<FileSize;
    			
    			if ( (FileSize - CurrentSize) < 1024)
    			       AmountToRead = FileSize - CurrentSize;
    			
    		
    			else
    				AmountToRead = 1024;
    
    			cout<<endl <<AmountToRead;
    
    
    			
    			//InFile.read(Buffer, AmountToRead);
    			AmountRead =	ReadToBuf(Buffer, AmountToRead, InFile);
    			CurrentSize += AmountRead;
    			
    			if(AmountRead == 0)
    			{
    				cout<<"Couldn't Read the Data";
    			//	exit(-2); // Ok nothing could be read, get out
    			}
    
    			cout<<"In IO loop\n" << AmountRead << endl;
    			
    			
    				OutFile.write(Buffer, AmountToRead);
    			
    			
    		}
    		while(CurrentSize < SizePerFile);
    
    Much easier to read, eh?
    Anyway thats a c++ example of a Do While. maybe it helps, hope so
     
    Last edited: Mar 1, 2004
  3. Ciz

    Ciz Corporal

    Right, I am pretty sure that an end while is missing... I am guessing you still need that in vb.net

    I dont know the circumstances tho, and have never looked at .net in any detail... I think the "loop" (the word loop) that you have in there is wrong placed, if you wanted to do it that way I THINK you would need to do this:


    Code:
    If DataReader.Read() Then 
    'a row was returned 
    Do While DataReader.Read() 
    intDatareaderColCount = DataReader.FieldCount 
    For intCounter = 0 To intDatareaderColCount - 1 
    xmlResult = xmlResult & "<" & DataReader.GetName(intCounter) & ">" &DataReader.GetValue(intCounter) & "</" & DataReader.GetName(intCounter) & ">" 
    Next 
    End While 'instead of loop
    
    if you wanted to have "loop" there, you would also have declare the exit test
    e.g 
    Do 
    STATEMENTS 
    Loop while DataReader.Read()
    
    Else 'no rows are returned 
    'lookup word at google 
    'rerun search with word as symptom 
    'still nothing? return error 
    xmlResult = xmlResult & "<error>No data retreived</error>" 
    End If 
    
    Right, well, I hope that helped... although I am not sure if it is any good to ya(I dont know if I trust in my abilty that much)...

    good luck with it...

    -Ciz
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds