none
Get Linq to SQL count RRS feed

  • Question

  • I have an application which adds a customer and then im trying to show the count of customers on the same page. So:

    I add a customer to the database using the InsertOnSubmit(Customer) command.
    View the database to see if the customer has been added - it has.
    Do a count on the table items but it returns the wrong count.

    The code that counts the customers is

    MyDataContext.Customers.Count.

    If the app is closed/refreshed then the correct count shows up until another customer is added.

    What am i missing?

    Monday, November 25, 2013 9:19 PM

Answers

  • Hello,

    Where the codes have you be placed? Before the SubmitChanges(); like below:

    int count;
    
                    Order order = new Order() { OrderID = 3, OrderCode = "3", OrderName = "3" };
    
                    db.Orders.InsertOnSubmit(order);
    
                    count = db.Orders.Count();
    
                    db.SubmitChanges();
    

    Or after the SubmitChanges() like below:

    int count;
    
                    Order order = new Order() { OrderID = 3, OrderCode = "3", OrderName = "3" };
    
                    db.Orders.InsertOnSubmit(order);
    
                   
    
                    db.SubmitChanges();
    
                    count = db.Orders.Count();
    

    If it is before, then we have to change it to be after because the transaction has not been committed.

    If it is after, it is strange for that, could you please share the codes regarding it so that we could help you better?

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 26, 2013 9:22 AM
    Moderator