Click here to Skip to main content
Rate this: bad
good
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
 

 
    Try
        context.SaveChanges()
        MessageBox.Show("Changes saved to the database.")
        Me.Refresh()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    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
good
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().
  Permalink  
Comments
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.

http://msdn.microsoft.com/en-us/data/ff706685.aspx
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
context.customer_account.Add(ca)
 
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 390
1 Jochen Arndt 165
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


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