Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
I am an absolute beginner. But I searched the net. There are not many example shown in VB anywhere.
I have created a test project which holds the win form. Then added another project which holds the Entity Model, after building the Model I added a reference of this to the Win form project. After that I added the Model object as a datasource.
I dragged the datasource controls into the form and added the below code.
Private context As TestModelEntities
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    context = New TestModelEntities
    Dim query = context.employees
    EmployeeBindingSource.DataSource = query.ToList


End Sub
Private Sub DepartmentBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeBindingNavigatorSaveItem.Click

        MessageBox.Show("Changes saved to the database.")
    Catch ex As Exception
    End Try
End Sub
I am scratching my head all day. Don't know what I am doing wrong.
Posted 2-Apr-13 6:47am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I never use the designer to do any of my database work, ever. There's too much that goes on that you need to know about and understand how it works before using it and people just don't know anything about it. It's not really as simple as "drag, drop and you're done!".
But, you have a BindingSource (EmployeeBindingSource), but you don't mention what controls are being bound to it, such as a DataGridView, or whatever.
You can try adding EmployeeBindingSource.EndEdit() to just before you call context.SaveChanges().
adat7378 at 2-Apr-13 14:11pm
Controls are textboxes. Even if I use datagrid the result is same. Is it the vb what makes it different. I mean the different way of doing the same work. I followed the c# below example.
Dave Kreskowiak at 2-Apr-13 14:16pm
The language does not matter. The .NET classes work exactly the same regardless of the language you're using.
adat7378 at 2-Apr-13 14:33pm
Thanks Dave. At least it updates the records. But no luck for adding new record yet. I will appreciate any suggestion.
adat7378 at 2-Apr-13 14:35pm
The MSDN example shows only savechanges method works. Am I missing something?
Dave Kreskowiak at 2-Apr-13 15:09pm
What do you mean by "adding"?? What exactly are you clicking on and doing before you click the Save button?
adat7378 at 2-Apr-13 16:01pm
I may sound stupid. When I click addnew button, I thought as the controls are mapped to the database simply savechanges on save button will do the job. Do I have to assign the value of the control to the dataobject.
adat7378 at 2-Apr-13 16:11pm
I guess I have to add
Dim ca As New customer_account
But I run into exception,
adat7378 at 2-Apr-13 17:16pm
Ok. This way it creates a empty record, therefore does not meet the validation criteria. I think I need to try to save the databindingsource.
Customer_accountBindingSource.Add(New customer_account) does not work
Customer_accountBindingSource.AddNew() does not work
Dave Kreskowiak at 2-Apr-13 19:13pm
It won't save anything to the database until all fields in the object pass validation.
adat7378 at 2-Apr-13 20:03pm
Even if the fields contain valid data, still does not save. I forgot to mention I am using MySQL and the key is auto generated by database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 445
1 Suvendu Shekhar Giri 134
2 Abhinav S 126
3 Maciej Los 125
4 Peter Leow 120
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 8,870
2 Peter Leow 4,984
3 Kornfeld Eliyahu Peter 3,300
4 Maciej Los 2,456

Advertise | Privacy | Mobile
Web02 | 2.8.150327.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100