简体   繁体   中英

C# query results seem to be cached - force reload of modified data

I have a two page website - one page shows a list of documents, the other to edit the properties of that document.

Now if I edit the properties of a document, save the changes I can see the data is changed in the database. But when I reload the list of documents - the list includes the old data. Hit refresh and the modified data is displayed.

If I trace through the changes it really looks like the old data is being returned.

I have tried passing the number of ticks in the URL to make IE load a new version - but it still shows the old version of the data. Even if I refresh the page and it changes - show the same documents details and the old version of the data is shown, again refresh and it is updated.

I have checked the data is being changed in the underlying database.

So taking the following call

          using (nhs_patient_document_s documents = new nhs_patient_document_s(Properties.Settings.Default.DataConnectionEDM))
            {
                List<nhs_patient_document> found = documents.Select<nhs_patient_document>("nhs_patdoc_patientid", String.IsNullOrEmpty(criteria) ? String.Empty : criteria);

                output = JsonConvert.SerializeObject(found);
            }

Ultimately this is the code which executes the query

    public List<T> Select<T>(String Field, String Value)
    {
        List<T> results = null;
        String sql = String.Empty;

        try
        {
            sql = (String.Format("select * From {0} Where {1} = '{2}'", TableName, Field, Value.Replace("'", "''")));
            results = Execute<T>(sql, CommandType.Text);
        }
        catch (System.Exception e)
        {
             throw e;
        }
        finally
        {
            sql = String.Empty;
        }

        return results;
    }

The execute function called above is as follows;

    protected List<T> Execute<T>(String CommandString, CommandType CommandType)
    {
        DataSet dataSet             = null;
        SqlConnection connection    = null;
        SqlCommand command          = null;
        SqlDataAdapter dataAdapter  = null;

        List<T> results             = null;

        try
        {
            connection          = new SqlConnection(connectionString);

            command             = new SqlCommand(CommandString, connection);
            command.CommandType = CommandType;
            command.CommandTimeout = 0;

            if (connection.State != ConnectionState.Open) connection.Open();

            dataSet     = new DataSet();
            dataAdapter = new SqlDataAdapter(command);
            dataAdapter.Fill(dataSet);

            results     = ExtractData<T>(dataSet);
        }
        catch ( System.Exception e)
        {
            throw e;
        }

        return results;

    }

Any ideas how I can force it to get new data from the server.

PS Thanks for the suggestions of other frameworks but I only had a very short time to write this and no time to learn them.

Found it and the problem was not a cache in SQL or C# but in the way jquery makes an ajax call. Basically jquery was caching the result

I need to add

cache: false

to the ajax call

$.ajax({
    url: 'PatientGetdocuments.ashx?patientid=' + patientid,
    cache: false,
    success: function (result) {
.
.
.

        });

    }

});

Thanks for the comments showing how open it is to SQL injection but I had a short deadline of today to get it done - will consider changing that part of the code to avoid that issue when I can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM