IvanHowarth
asked on
Controls bound to a DataView: Problems inserting new line/record
I have a form in my VB.net 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.Se lectComman d.Paramete rs("@MyRan geColumn") .Value = “Range”
sqlDataAdapter1.Fill(MyDat aSet) ‘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(myLi stBox.Sele ctedValue, DataRowView)_(myListBox.Di splayMembe r.ToString ).ToString ())
myDataView.Delete(i)
========================== ========== ========== ========== ====
Saving data to source (Update) - WORKS
sqlDataAdapter1.Update(MyD ataSet)
========================== ========== ========== ========== ====
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
drvNew.EndEdit()
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.Se
sqlDataAdapter1.Fill(MyDat
==========================
Delete a record based on the selected ListBox value - WORKS
Dim i As Integer = myDataView.Find(CType(myLi
myDataView.Delete(i)
==========================
Saving data to source (Update) - WORKS
sqlDataAdapter1.Update(MyD
==========================
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
drvNew.EndEdit()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Roger
Dim drvNew As DataRowView = myDataView.AddNew
Anyway, glad it's sorted.
Roger
ASKER
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!