I have a loop that is for a text reader that is looking up ID's of records in the SqlServer table that need to be updated. However the loop is faster than the server and I am trying to figure out how to make the loop wait
Example:
using (tr = File.OpenText(myFilePath)
{
SqlCommand UpdateCmd = new SqlCommand("MyStoredProcedure", conn);
UpdateCmd.CommandType = CommandType.StoredProcedure;
while((templine = tr.ReadLine()) != null)
{
conn.Open();
UpdateCmd.Parameters.AddWithValue("@IDNumber", templine);
UpdateCmd.ExecuteNonQuery();
//this is the point at which I need to Wait for the Nonquery to finish.. How?
conn.Close();
}
}I can get errors on similar issues like connection still open or you already have an open reader. etc.
Is there a quick and dirty way to make it wait for procedure to wait?
Any help will be appreciated.
Thanks
Frank

Felipe RamosRank #1,409 · 1.2K reputationPosted May 19, 2011, 10:49 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted May 19, 2011, 6:44 PM EDT
My suggestion is to use try/catch so you get the benefit of the information available that way. Then if you still need help, tell us what the error is.
Frank Galavan9 reputationPosted May 20, 2011, 8:48 AM EDT
I have been coding it all day and I have implemented the new code based on what both of you have said.
Open and closed are now out side the read loop and all of the statements are surrounded by try...catch statements.
As soon as I run it I will let you know the outcome.
Thanks again.
Posted May 20, 2011, 9:15 AM EDT
Is it possible for you to implement the asynchronous sql command execution like SQLCommand.BeginExecuteNonQuery(), though your server is not responded immediately still you can process the other records.
http://msdn.microsoft.com/en-us/library/ca56w9se(v=vs.80).aspx
Frank Galavan9 reputationPosted May 20, 2011, 10:10 AM EDT
Let me ask you, Murali,
If I do an asynch call on the first record of in the text file, what will happen on the second record if the first hasn't finished? Do they queue up? It's all the same table.
I will take a look at the msdn artical you suggested.
Frank Galavan9 reputationPosted May 20, 2011, 10:15 AM EDT
IAsynchResult to wait for.... Nice!
Thanks Murali!
Sam HobbsRank #107 · 20.5K reputationPosted May 20, 2011, 2:03 PM EDT