Question

I have a strongly typed dataSet that calls some stored procedures.In one case I add some data in the database using the strongly typed data set.The data gets added to the database but for some reason the new data is not being displayed.

This leads me to believe that I have to somehow refresh the strongly typed dataset after I add a new item.I am not sure what code I should post here but I will post the code that adds the elements to the database:

var addBookAdapter = new QueriesTableAdapter();           

addBookAdapter.AddBook(book.Name,book.Author,book.Description,book.PublicationDate,book.CategoryId);

This is the stored procedure that does this:

CREATE PROCEDURE [dbo].[AddBook]
    @Name nvarchar(MAX),
    @Author nvarchar(MAX),
    @Description nvarchar(MAX),
    @Date date  ,
    @CategoryId int
AS
    INSERT INTO Books (Name , Author , Description , PublicationDate , CategoryId)
    VALUES (@Name , @Author , @Description , @Date , @CategoryId)

As I already mentioned this works.The new item gets added to the db but they are not displayed

Was it helpful?

Solution

If the underlying data in your database has changed, the only way to load that information into your dataset is to load it manually. You have a couple of options here, depending on your needs:

  • You can discard the existing data and re-fill the data set from your data adapter.
  • You can load a second copy of the dataset and use DataSet.Merge to bring in any changes.
  • You can just add your new row into the dataset directly.

However, when you're using strongly typed data sets, the intention is for you to use that object when adding new rows, not to add them to the database via the table adapter. When you add a new row to a table in the data set, it is tracked as "added" in memory. When you then go to save changes from that dataset back to the database via your table adapter, the dataset will call whatever the appropriate InsertCommand or UpdateCommand is for the tables. In that way, your database and dataset are synchronized.

The workflow for using typed datasets typically has the following steps, in his order. You can get rather complex with the merge resolution logic and how you handle conflicts, but the basic idea is the same:

  • dataAdapter.Fill(dataset)
  • (make changes to your typed dataset via the typed data rows)
  • dataChanges = dataSet.GetChanges()
  • dataAdapeter.Update(dataChanges)
  • dataSet.Merge(dataChanges)
  • dataSet.AcceptChanges()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top