1

My C# program connects to a user request SQL database, gets the first open request, process it and mark the request as closed. Then I get the next open request.

The problem is that I always get the same request back from SQL Query although it is marked as 'closed'. I suspect I get a cached result instead of updated data. But I don't know how to clear that cache.

I tried to dispose the SQLDataAdpater and create new one every time. I also tried to add a random number as parameter to the SQL Select stored procedure. None of them worked.

Can anyone please help me on this issue? Thanks.

The Sql query is:

Select Top(1) RequestID, RequestType, RequestXML from Request 
where RequestStatus='OP' 

SQL Update command:

begin tran  
Update Request Set RequestStatus=@RequestStatus where RequestID=@RequestID;  
if (@RequestXML is not null)   
    Update Request Set RequestXML=@RequestXML where RequestID=@RequestID;  
commit tran  

C# Code:

SqlDataAdapter da = new SqlDataAdapter("SrvGetOpenRequest", cn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlCommand cmd = new SqlCommand("SrvUpdateRequest", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("RequestID", SqlDbType.Int);
cmd.Parameters.Add("RequestStatus", SqlDbType.Char);
cmd.Parameters.Add("RequestXML", SqlDbType.Xml);
DataTable dt = new DataTable();
cn.Open();
da.Fill(dt);
cn.Close();
while (dt.Rows.Count > 0)
{
 // Process returned datatable here.
   ..............

    cmd.Parameters["RequestStatus"].Value = "CL";
    cn.Open();
    cmd.ExecuteNonQuery();
    // fetch the next request to process
    da.Fill(dt);
    cn.Close();
 }

I did checked the database and the record was marked as closed.

6
  • 1
    It sounds like a problem with your code. SqlServer does not cache results. Try posting your code. Commented Feb 18, 2011 at 14:45
  • How do you load data in C# ? What is your SQL query ? Commented Feb 18, 2011 at 14:46
  • 1
    If you are using simple ADO.net without explicitly setting up caching anywhere then there shouldn't be any caching. Can you post example SQL for how you select the next request? Have you confirmed the request are being marked as processed by looking in the database? Commented Feb 18, 2011 at 14:46
  • Are you using transactions to update the requests? Are you updating them directly using SQL or are you using e.g. a DataSet / DataTable? Commented Feb 18, 2011 at 14:49
  • By "request" do you mean "record"? As in, you're getting back the same record back? As Klaus Byskov Hoffmann says, it's probably a code issue so posting your code will help a lot. Commented Feb 18, 2011 at 14:50

3 Answers 3

1

Try calling dt.Clear() before filling it again.

Fill method adds rows to existing DataTable

Sign up to request clarification or add additional context in comments.

Comments

0

Post some code, please. It's possible that you're not actually persisting the changes you make locally back to your actual database, which is why you keep getting the original unchanged data back from your query. It may also be possible that you're making the changes inside a transaction, but then not committing the transaction.

Comments

0

Does DataAdapter.Fill clear the DataTable first? I wonder if you're appending new rows to the end of the DataTable each time?

1 Comment

Yes. Both of you are right. Thank you everybody for responding to my question so fast and helping me fix the problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.