locked
Updating DataBase with DataTable & TableAdapte problem in C#? RRS feed

  • Question

  • Hi all.
    I've had a DataBase that named Database1.mdf
    After that VS2008 SP1 creates a DataSet in my Project, named Database1DataSet
    Then I've created a TableAdapter.
    Now I wanna update the DataBase with them ( DataSet & DataTable & TableAdapter ).
    I used this code :

    Database1DataSetTableAdapters.eventsTableTableAdapter eventsTableTableAdapte =  
        new WpfApplication1.Database1DataSetTableAdapters.eventsTableTableAdapter();  
    Database1DataSet.eventsTableDataTable eventsTableDataTable =   
        new Database1DataSet.eventsTableDataTable();  
    eventsTableTableAdapte.Fill(eventsTableDataTable);  
     
    var id = (from c in eventsTableDataTable.Distinct()  
              select c.id).Last();  
    id++;  
        
    DataRow newDataRow = eventsTableDataTable.NewRow();  
    newDataRow["event"] = "event" + id.ToString();  
    newDataRow["date"] = id;  
    eventsTableDataTable.Rows.Add(newDataRow);  
    eventsTableTableAdapte.Update(eventsTableDataTable);  
     
    eventsTableTableAdapte.Dispose();  
    eventsTableDataTable.Dispose(); 

    But the changes not apply to the DataBase , Why ?
    What's wrong with it ?
    Could you guide me ?

    BTW : no exceptions occurred .

    Many thanks in advance.
    Friday, January 9, 2009 12:49 PM

Answers

  • Your code snippet looks good.
    A common mistake is to call AcceptChanges before calling Update.

    You could listen to the DataAdapter.RowUpdated event to verify what commands where executed.

    // snippet is for SqlDataAdapter - will have to adjust if using other data providers
    eventsTableTableAdapte.RowUpdated += delegate(object sender, SqlRowUpdatedEventArgs args) {
      Console.WriteLine("RecordsAffected={0}, Text={1}", args.RecordsAffected, args.Command.CommandText);
    };


    For a general reference to saving data
     http://msdn.microsoft.com/en-us/library/ms171932(VS.80).aspx

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Saturday, January 17, 2009 2:12 AM