Controls bound to a DataView: Problems inserting new line/record

Posted on 2006-03-24
Last Modified: 2011-09-20
I have a form in my application (built using Visual Studio 2003) that has a listbox control (its DataSource set to MyDataView and it’s DisplayMember set to a MyDataView column). Several textfields also exist and all bound to the same MyDataView.

How do I successfully insert a new record (datarow) into the DataView with the data from the respective textboxs and then list the respective column in the ListBox with the rest, ready to update the datasource (SQL 2000 table) when the adapter’s update method is called. The adapter, dataset, SQLConnection and dataView were generated using VS 2003 wizards. I can’t get it to do the insert. If I hard code the update, the record gets added twice then throws a constraint exception (duplication of the PK). If I just call the update method when the save button is clicked, nothing happens or things start to go horribly wrong. The other select, delete, updates all work ok.

Big thanks in advance!

Break down of what I have done:
On load (Select):      - WORKS

sqlDataAdapter1.MyTable.SelectCommand.Parameters("@MyRangeColumn").Value = “Range”
sqlDataAdapter1.Fill(MyDataSet)                      ‘see above on how the dv is linked


Delete a record based on the selected ListBox value - WORKS

Dim i As Integer = myDataView.Find(CType(myListBox.SelectedValue, DataRowView)_(myListBox.DisplayMember.ToString).ToString())



Saving data to source (Update) - WORKS



Insert (add new record)                 - DOESN’T WORK

'Upon pressing a button:
Set all textBoxs.text = “”

‘the following duplicates the record throwing the unique constraint exception
Dim drvNew As DataRowView = myDataView.AddNew
drvNew("MyRangeColumn") = “Range”                
drvNew("MyColumn2") = txtBox2.Text
drvNew("MyColumn3") = txtBox3.Tex
‘…and so on

Question by:IvanHowarth
    LVL 34

    Accepted Solution

    Use this


    for your insert code.  The problem you are facing with your present approach is that the changes that have been made to the controls have not been committed to the datatable when you are trying to do the update because the user is moving directly from those controls to the button.  If you call .EndCurrentEdit explicitly, it should overcome that problem.


    Author Comment

    Thanks Roger - perfect!

    One note to add for those following: Although Roger's answer is 100% right, don't then hard code anything in the insert code like I have, otherwise it still gets duplicated. Keep it simple!
    LVL 34

    Expert Comment

    Yes, perhaps I should have added that the reason that you were getting duplication when you did have that extra code was because the .EndCurrentEdit that was NOT being automatically called when users moved direct from editing a control to the button WAS being automatically called as soon as it met

        Dim drvNew As DataRowView = myDataView.AddNew

    Anyway, glad it's sorted.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    758 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

    8 Experts available now in Live!

    Get 1:1 Help Now