locked
How to get the auto-increment ID after submitChanges()? RRS feed

  • Question

  • HI,

    I try to develop a "Ticketing System" for a help desk. 

    Therefore I created a Silverlight page where I can create new "Ticket". After saveing this "Ticket" into the database another silverlight page opens where I should be able to view and edit this new created "Ticket".

    My problem is now, that i don't get the auto-increment ID after submitting the data. 

    During debugging I recognized that the submit happens AFTER running through following method: ViewTicket_Loaded of the second page. 

    I mean, the debugger jumps from dbmodel.Tickets.Add(this.ticket); to dbModel.SubmitChangs(); to dbModel.Load(dbModel.GetTicketQuery(), LoadBehavior.RefreshCurrent, TicketLoaded, null); but the data appears after the ViewTicket_Loaded in the database. 

    Furthermore, I don't understand how the dbmodel.load works because the Loadoperation TicketLoaded also processes after the ViewTicket_Loaded. 

     I hope someone can help me with this problem. 

    FIrst page:

    TicketingSystemContext dbModel = new TicketingSystemContext();
    dbModel.Tickets.Add(this.ticket);
    dbModel.SubmitChanges();
    dbModel.Load(dbModel.GetTicketQuery(), LoadBehavior.RefreshCurrent, TicketLoaded, null);

    currentTicketID = this.ticket.TicketID;
    dbModel.EntityContainer.Clear();
    MessageBox.Show("Data successful saved");

    this.NavigationService.Navigate(new Uri("/ViewTicket", UriKind.RelativeOrAbsolute));
    
    
    public void TicketLoaded(LoadOperation<Ticket> ticket)
    {
    foreach (var item in ticket.AllEntities)
    {
    if ((item as Ticket).TicketID == this.ticket.TicketID)
    {
    this.ticket = (item as Ticket);

    }
    }
    }
    
    
    Second page:
    public ViewTicket()
    {
    InitializeComponent();
    currentTicketID = NewTicket.currentTicketID;
    Loaded += new RoutedEventHandler(ViewTicket_Loaded);
    }

    void ViewTicket_Loaded(object sender, RoutedEventArgs e)
    {
    }
    
    
    
    
    Thank you!
    Haberl
    Monday, March 26, 2012 2:20 PM

Answers

  • You have to put your code in OnSubmitCompleted, that is why you are passing that callback into the SubmitChanges.

    Monday, March 26, 2012 4:45 PM

All replies

  • All communications in Silverlight are asynchronous. When you call SubmitChanges or Load you are just starting the procesof submitting or loading. You have to wait for the Submit to complete before you could do a new load and get new data.

    However, if you are using a DAL like EF or LINQ to SQL, the identity values will automatically be returned to the client once the submit is complete. You do not need to do a new load.

    Monday, March 26, 2012 2:31 PM
  • Hi,

    thank you very much for your reply. 

    I thought the same and tried this before I tried to load it again: 

    dbModel.SubmitChanges(OnSubmitCompleted, null);

    currentTicketID = this.ticket.TicketID;
    dbModel.EntityContainer.Clear();
    MessageBox.Show("Data successful saved");
    
    
    However, I still don't get the TicketID. The Value of the ID is always 0. 
    How can I wait till the SubmitCHanges is complete? 
    
    
    Thank you!
    Haberl
    Monday, March 26, 2012 2:43 PM
  • You have to put your code in OnSubmitCompleted, that is why you are passing that callback into the SubmitChanges.

    Monday, March 26, 2012 4:45 PM
  • It worked! Thank you very much!

    Tuesday, March 27, 2012 7:43 AM