Click here to Skip to main content
15,909,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey evry1..

I had developed a project, in which following code is written But i got the ERROR in t while loop after 3rd or 4th iteration.

So plz let me kn whether the query is wrong or any other problem.



string qr2 = "select Quan from sampleOB where date<(select max(date) from sampleob where date<'"+ CDate +"') and (Particular=(select RMtype from productcode where RMCode='"+ RMCode +"' limit 1) or subparticular='" + RMCode + "') limit 1";


while()
{
con.Open();
cmd = new MySqlCommand(qr2, con);
dr = cmd.ExecuteReader();
dr.Read();
currBalOB = Convert.ToDouble(dr[0]);//Invalid attempt to access a field before calling Read()
con.Close();
}


thank you
swapnil
Posted

1 solution

Why are you doing that?
At best, you will sit in a loop reading the same data from teh DB and loading the same record and converting the same field to a double, then throwing away the result.

Why?
Why not look at all the records the reader returns instead?
Why not have an exit from the loop?

BTW: you should Dispose your MySqlCommand objects as well, rather than just creating a new one each time.
 
Share this answer
 
Comments
swapnilKumbhar 9-Feb-12 5:39am    
@OriginalGriff
Actually, i put the condition in while loop.And it doesnt read same data(i checkd it personly).And it reads the number of records and perform the operation below the code(i didnt post that code).
and i got the error after 3rd or 4th iteration even if the data changes.
OriginalGriff 9-Feb-12 6:05am    
If the code looks like your code fragment, then the only way the data can change is if the data in the database changes. You aren't using any parameters to the command, and you don't change the command string inside the loop...

So if your actual code doesn't look anything like your code fragment, then don't you think it would help if it did?
swapnilKumbhar 9-Feb-12 6:54am    
@originalGriff
Sorry but i didnt get anything.
Same code with different database is working.And the query is also executed in data base but doesnt work in c#.
OriginalGriff 9-Feb-12 7:20am    
What are you actually trying to do? Not what does the code do, but what function are you trying to write code to perform?
Espen Harlinn 11-Feb-12 5:33am    
Another 5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900