onlky refreshing and not adding rows using Fill()

Hi,

I need to refresh my dataset after a certain period and I am using DataAdapter.Fill method

myCommand = new SqlCommand(ReferenceClass.MarketExposureSPName(), myAccessConn );

myCommand.CommandType = CommandType.StoredProcedure;

myCommand.CommandText = ReferenceClass.MarketExposureSPName();

myCommand.Parameters.Add(ReferenceClass.MarketExposureSPLimitParamName(),iMarketExposure);

myAdapter.SelectCommand = myCommand ;

but doing this adds the same rows to the DataSet DS.

So, the first time I do fill I get 3 rows in this datatable. Now if I do the fill again, it again adds the 3 rows while keeping the 3 rows that were added in the previous fill. So, I get 6 rows. If I do Fill the third time then I have 9 rows and so on.

I dont want add rows that are already in the dataset. If I ahev 3 rows in the dataset and database still have the same 3 rows then my dataset should have only 3 rows no matter how many times I do the Fill.

Is this possible How can I do this Would I have to explicitly clear the datatable before doing the Fill()

I am using framework1.1



Answer this question

onlky refreshing and not adding rows using Fill()

  • Matt David

    This is an excerpt from the MSDN Library for Visual Studio 2005 - DataAdapter.Fill(DataSet) method...

    "You can use the Fill method multiple times on the same DataTable. If a primary key exists, incoming rows are merged with matching rows that already exist. If no primary key exists, incoming rows are appended to the DataTable."

    Basically this means that if your database table that you use to fill the dataset/datatable does not have a primary key, the fill function assumes that the data in the database are new entries and appends them instead of merging the matching rows, because it has no primary key to determine unique entries. You can either add a primary key to your database table or like you suggested, explicitly clear the database table before calling Fill();


  • Feret

    It has primary keys.

    I figured adding of extra rows problem. But there is another related problem. When I do .Fill , for some reason it does'nt update the the dataset at all.

    I create a dataadapter object as a singleton. I use the same dataadapter object to populate the dataset throughout the application. The dataset has multiple tables and each table is populated by a different SQL.

    private void PopulateDataSet()

    {

    SQL = "select * from table1"

    myAdapter.SelectCommand = new SqlCommand(SQL,myAccessConn );

    myAdapter.Fill(DSRiskManager,ReferenceClass.OrderTableName());

    SQL = " select * from table2" +

    " from oes_orders , orders where oes_orders.order_id = orders.order_id";

    myAdapter.SelectCommand = new SqlCommand(SQL,myAccessConn );

    myAdapter.Fill(DSRiskManager,ReferenceClass.SubOrderTableName());

    }

    After I the 2 tables in the dataset are populated. I periodically calls a method that does this

    private void RefreshDataset()

    {

    myAdapter.Fill(DSRiskManager,ReferenceClass.OrderTableName())

    myAdapter.Fill(DSRiskManager,ReferenceClass.SubOrderTableName()

    }

    Note that in in both cases it is using the same dataadapter object, in both PopulateDataSet() an RefreshDataset() functions. Now whats happening is that when I do myadapter.Fill in RefreshDataset() it does'nt update the dataset. The rows are all the same and nothing basically changes. It looks like .Fill in RefreshDataset() does'nt do anything at all. I dont know how to fix the problem

    Thanks,


  • FlyingHorse

    I deleted it from the Enterprise manager.

    Yes, I did .Fill() menthod after I deleted the row from the database, but the dataset still showed the deleted rows.

    But as I mentioned in my last post when I add row to the database using Enterprise Manager the dataset shows the newly added row.


  • cmtytest2

    How did you delete data from database After deleting from database did you refresh data in dataset

    Thank you,
    Bhanu.



  • Pete Wojtkowiak

    Thanks, I explicitly added the PKs and now it looks fine but I noticed another thing. When I add rows to the database table it shows up fine in the dataset's datatable but when I delete rows from the database then it is not reflectd in the datatable. It still shows the deleted rows. Any idea how to reflect current database state
  • onlky refreshing and not adding rows using Fill()