1

I am working on mvc c# , with sql database at backend. My problem is when I update a table's data manually from sql table at backend. It is not updated on the frontend until I open the website again after closing the webpage.

Here is the Image of database table where am updating it manually.

here am retrieving data from database.

  public ActionResult AddQuiz()
    {

        ViewBag.quizes_List = db.EntityFrameWorkConnection.Zasa_Quiz.OrderByDescending(mm => mm.quiz_code).ToList();
        return View();
    }

This is where am using my ViewBag to display quizes list

 @foreach (var item in @ViewBag.quizes_List)
                        {
                            <tr>
                                <td>
                                    <span class="badge bg-blue">@item.quiz_code</span>
                                </td>
                                <td>
                                    @item.quiz_title
                                </td>

                                <td><span class="badge bg-blue">@item.quiz_date.ToString().Substring(0, 10)</span></td>
                            </tr>
                        }

EntityFrameWorkConnection is in constants class. and db is declared like this in controller: and all this is working fine.

 private static Constants db = new Constants();



 public class Constants
{
    public ZASA_AK_DBEntity EntityFrameWorkConnection = new ZASA_AK_DBEntity();
    public SqlConnection ADOConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["adocon"].ConnectionString);
    public string ConnectionString = ConfigurationManager.ConnectionStrings["adocon"].ConnectionString;

}
11
  • have you check the viewbag using debugger that list items are coming in it? Commented Nov 16, 2017 at 12:33
  • Are you say that refreshing the page is not working? In which case it may be a caching issue. Commented Nov 16, 2017 at 12:34
  • Does F5 or CTRL-F5 update the value? You now it's not going to automatically appear right? What you're seeing is a static page that won't refresh unless you tell it to Commented Nov 16, 2017 at 12:38
  • Yes i have debugged it. actually db.entityframework thing is not bringing the updated value. i have tried it using a separate list too. it stays same. Commented Nov 16, 2017 at 12:39
  • No not cache issue. have tested it using breakpoints. list brings old value. Commented Nov 16, 2017 at 12:40

2 Answers 2

2

Entity framework catches the data. You have to use Using block.

using(var context = new DBEntities())
{

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

5 Comments

It is behaving the same. no difference
change the structure so that on each request new object of context is created.
when you are accessing with the help of "db" you will get catched data in entity. you will get updated data with the help of new object of entity.
I have tried entries.reload(). its working and giving me updated data !
Of course this is the only right answer. Use new context instance, don't Reload.
1

@Zainab The snippet you provided doesn't have any refresh or dynamic data retrieval. The AddQuiz() method retrieves data and calls the View. Now, if you change something in Backend, view should be refreshed or AddQuiz() should be called again to get the Updated Data.

Try .Reload() method on your entity and if that doesn't fix it, try disposing your dbcontext as soon as you are finished with the method call and create a new dbcontext everytime.

1 Comment

It doesnot brings data on refresh actually. Am refreshing the page and it calls AddQuiz() again. But data is not changed. am not concerned with refreshing it dynamically actually.

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.