Solved

How to clear a recordset

Posted on 2010-08-26
9
482 Views
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:
CustomerBindingSource.AddNew()

And the OK-button have this:
CustomerBindingSource.EndEdit()
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

Tomas
0
Comment
Question by:ekenman
9 Comments
 
LVL 4

Expert Comment

by:kjetilroe
ID: 33537089

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

dsCustomer.Tables(0).Rows.Clear()

Provided it is a dataset you are using.

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

0
 
LVL 17

Accepted Solution

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

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


0
 

Author Comment

by:ekenman
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
 
0
 
LVL 4

Expert Comment

by:kjetilroe
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.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Expert Comment

by:Ark
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
0
 
LVL 4

Expert Comment

by:kjetilroe
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.
0
 

Author Comment

by:ekenman
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

        CustomerBindingSource.AddNew()

    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Try
            CustomerBindingSource.EndEdit()
            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
        Me.Close()
    End Sub

End Class

0
 

Author Comment

by:ekenman
ID: 33541326
OK. I figured it out eventually. I had to rebind the bindingsource after emptying the dataset.
DsCustomer.Clear()
CustomerBindingsource.DataSource = DsCustomer
CustomerBindingSource.DataMember = "Customer"
CustomerBindingSource.AddNew()
Thanks for all help.
/Tomas
0
 

Author Closing Comment

by:ekenman
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now