Click here to Skip to main content
13,043,898 members (48,022 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 5: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 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 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 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 2-Apr-13 14:35pm
The MSDN example shows only savechanges method works. Am I missing something?
Dave Kreskowiak 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 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 2-Apr-13 16:11pm
I guess I have to add

Dim ca As New customer_account

But I run into exception,
adat7378 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 2-Apr-13 19:13pm
It won't save anything to the database until all fields in the object pass validation.
adat7378 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2017
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