Wednesday, May 22, 2019

Winforms and linq to sql classes not saving to mdf file.

So I'm trying to help someone out with a small win forms project and while I've never worked in win forms I'm well versed in web forms so it translates easy enough. Here is the problem I have. I added an MDF file as I'm just trying to keep it so all of the data is contained in the solution itself, so there's no sql express set up or anything.

My code all works fine, and if I run the form, I will see the data I add/edit. However, as soon as I close and reopen the project, the data is gone. Now, if I go in manually to the db and add entries via the sql manager it works, but not from my form objects. Any ideas why? I can attached my full porject upon request, but an example is

Private Sub addBtn_Click(sender As Object, e As EventArgs) Handles addBtn.Click 'double check the added a name

Dim name = nameTb.Text 'note I only put name in the form. You would want to update it with all the options 'and then update the stuff in the using db thing to add all the proper fields If (String.IsNullOrWhiteSpace(name)) Then MessageBox.Show("Please enter a name") Exit Sub End If 'this is using linq to sql classes to connect to the database so you don't have to write actual sql and can use classes 'and intellisense to connect to stuff Using db As New DndClassesDataContext Dim entity = New Entity entity.EntName = name db.Entities.InsertOnSubmit(entity) db.SubmitChanges() MessageBox.Show("The entity was added") End Using 'reload the data LoadEditEntities() LoadEntityList() 

End Sub 'add btn click LoadEditEntities reloads a combo box with the data. As I said, this all works when I run the form, I can add things and I see them show up in the edit combo. However, as soon as I stop and then re run the project, the data never got saved. Any ideas? DndClassesDataContext is a linq object that maps back to my mdf file.

Winforms and linq to sql classes not saving to mdf file. Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team