1

I am new here and only signed up because I desperately need the solution to my problem. I have a VB.NET project that is using a local .MDF file that I have imported into my project. The primary keys are intact so I know that isn't my issue.

I am using Linq-to-SQL to query the database and make changes. On my main form I use a Data Grid View that is populated by a query. I am having an issue with my insert where the query works fine and the DGV gets updated with newly created data, but when I call SubmitChanges() the changes do not get saved to the actual physical database. I am not sure what I am missing or doing wrong.

Here is my code to better assist your educated answer.

Public Class frmCell_Leader
    'Reference DataContext
    Private db As New MarquardtClassesDataContext()

    Private Sub frmCell_Leader_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        FillProductionGrid()
        PrepareInsertFields()
    End Sub

    Private Sub FillProductionGrid()
        ''''''''''''''''''''''''''''''''''
        'E.Salce Last Modified 11.29.2012
        'Comments: Working
        ''''''''''''''''''''''''''''''''''

        'Populate DGV
        Dim query = From marq In db.tblProductivities
                 Select
     marq.PartNum, marq.Produced, marq.Packed, marq.Issue, marq.StationNum, _
     marq.PlanDownTime, marq.UnplannedDownTime, marq.CurDate

        dgvProduction.DataSource = query
    End Sub
    Private Sub PrepareInsertFields()
        ''''''''''''''''''''''''''''''''''
        'E.Salce Last Modified 11.29.2012
        'Comments: Working
        ''''''''''''''''''''''''''''''''''

        'Fill Planned Downtime Combo Box
        Dim PlannedQuery = From planned In db.tblPlannedDownTimes
                           Select planned.Plan_ID, planned.Description, planned.Active
                           Where Active = True

        cboPlanned.DataSource = PlannedQuery
        cboPlanned.DisplayMember = "Description"
        cboPlanned.ValueMember = "Plan_ID"

        'Fill UnPlanned Downtime Combo Box
        Dim UnPlannedQuery = From unplanned In db.tblUnplannedDownTimes
                           Select unplanned.Unplan_ID, unplanned.Description, unplanned.Active
                           Where Active = True

        cboUnplanned.DataSource = UnPlannedQuery
        cboUnplanned.DisplayMember = "Description"
        cboUnplanned.ValueMember = "UnPlan_ID"
    End Sub

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        'Add the new Production to database
        'Create an productivity object
        Dim marqpro As New tblProductivity With {
            .PartNum = CShort(txtPartNumber.Text),
            .Produced = CShort(txtProduced.Text),
            .Packed = CShort(txtPacked.Text),
            .Issue = txtIssue.Text,
            .PlanDownTime = CShort(cboPlanned.SelectedValue),
            .UnplannedDownTime = CShort(cboUnplanned.SelectedValue),
            .CurDate = Date.Today}

        'insert newly created data
        Try
            'Submit changes too database
             db.tblProductivities.InsertOnSubmit(marqpro)

            db.SubmitChanges()
            db.Refresh(Data.Linq.RefreshMode.KeepCurrentValues, marqpro)

            'Refresh DGV
            FillProductionGrid()
            MessageBox.Show("Data added successfully")

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message)
        End Try
    End Sub
End Class
salce
  • 409
  • 1
  • 12
  • 28
  • why Attach? db.tblProductivities.Attach(marqpro) - isn't this a new record? – ScottE Dec 01 '12 at 21:10
  • Yes. I actually was playing around with different methods to see if they will work. .Attach was recommended. I also used db.Refresh as well but didn't work. Someone did recommend to check the connections strings, and when I looked at my app.config file, it showed three different connection strings, not sure where VB got them when i just created one. – salce Dec 01 '12 at 21:53

1 Answers1

1

Perhaps you have an .mdf file in your project that is getting copied to the bin/debug folder everytime you build.

Why isn't my SubmitChanges() working in LINQ-to-SQL?

Community
  • 1
  • 1
Pleun
  • 8,856
  • 2
  • 30
  • 50