How to clear a recordset

Posted on 2010-08-26
Last Modified: 2012-05-10
In my project I have a typed dataset and a binding source to go with my bound controlls on a form. I have enteties in a separate project and a WCF-service to handle the data.

The Dataset is calld dsCustomer.

When I load the form I do this:

And the OK-button have this:
CustomerID = proxy.SaveNewCustomer(DSCustomer)

The customer is saved and everything is fine. Next time I open the form the old record is still in the dataset and I can't remove it. with dsCustomer.Clear and CustomerBindingSource.Clear throws an exception.

I guess I'm doing something wrong here. I'd be greatful for some help. (I run .NET Framework 4.0)

Best Regards

Question by:ekenman
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 33537089

Actually you can easily remove all records in a table in a Dataset. Just call this function:


Provided it is a dataset you are using.

Then bind the dataset to the form again to update it.

LVL 17

Accepted Solution

Zhaolai earned 500 total points
ID: 33537117
In the OK_button event, add a line to re-instantiate the dataset, like this:

CustomerID = proxy.SaveNewCustomer(DSCustomer)
DSCustomer = New DataSet


Author Comment

ID: 33539483
Thanks! I've tried the above solutions without success. Perhaps it is the bindingsource that is causing the problem. The exception is index out of range.
When I open the form the second time the Identity column is -2 and not -1.
Thanks in advance
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.


Expert Comment

ID: 33539992

The identity column, and the array indexes are two different tings. You should investigate your code to see if you are using a direct index into the row array somewhere. If the array length is 0 and you try to access the first row, it will give you that exception.
LVL 28

Expert Comment

ID: 33540019
Dim strSQL As String = "DELETE FROM " & tableName
Dim cmd As New SqlCommand(strSQL, conn)
cmd.ExecuteNonQuery() 'Delete all records from table
cmd.CommandText = "DBCC CHECKIDENT (" & tableName & ", RESEED, 0)"
cmd.ExecuteNonQuery() 'reseed autoincrement to 0 preserving FK

Expert Comment

ID: 33540052

To Ark: I dont think that he want to delete all records from his database table. It is clearing the Dataset in the form that is the question.

He still want to preserve the information in the database.

Author Comment

ID: 33540083
Yes, it is not in the database the problem lies :). Below is the form code. In the designer I've draged a details-view from my data source window which creates the dataset and bindingsource.

Public Class frmNewCustomer
    Public CustomerID As Integer
    Private Sub frmNewCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
            Dim proxy As New LS_Service.LSServiceClient
            CustomerID = proxy.SaveNewCustomer(DsCustomer)

            Me.DialogResult = Windows.Forms.DialogResult.OK
        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
    End Sub

End Class


Author Comment

ID: 33541326
OK. I figured it out eventually. I had to rebind the bindingsource after emptying the dataset.
CustomerBindingsource.DataSource = DsCustomer
CustomerBindingSource.DataMember = "Customer"
Thanks for all help.

Author Closing Comment

ID: 33541359
It would be nice to be able to reward points to others but choose your own answer as solution. Then it is easier to find in the knowledge database.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question