Search code examples
c#sqldatasetcompact-frameworkwindows-mobile-6.5

Clear() or Dispose() to clear dataset assigned to new Dataset() in C#?


What should I use to clear DataSet which I assign to new Dataset() on every new SQL call in my C# code (Windows Mobile Compact Framework)?

At the moment I am using Clear(), however I am not sure if I should be using Dispose instead. What would be proper choice for this?

Some code:

Inside frmA (main form where I perform SQL search of database) I save my search result into Database and if count is greater than 0, I go to another form frmNewWork to do some work with found data:

// search SQL
// declare connectionString and command... 
SqlCeDataAdapter adapter = new SqlCeDataAdapter(mCommand);
modFunctions.tempDataset = new NEWDataSet();
adapter.Fill(modFunctions.tempDataset, "item");
foundCount = modFunctions.tempDataset.item.Count;

// Goes off to another form to do somework with that dataset
if (foundCount > 0)
{
    frmNewWork myForm = new frmNewWork();
    myForm.ShowDialog()
}

Now... once I return from myForm, I want to clear that database, and I do that inside form A GotFocus function:

private void frmA_GotFocus(object sender, EventArgs e)
{
    // Clear prior tempDataset (was .Clear() originally)
    modFunctions.tempDataset.Dispose();
}

Solution

  • Two points

    1) If you want to clear the Dataset after other form which is shown using ShowDialog() method. You can do it just after your myForm.ShowDialog(); statement.

    2) If ASTSDataSet is unmanaged call Dispose. Otherwise do not do anything, GC will take care.