6 Replies - 3332 Views - Last Post: 24 July 2012 - 07:31 AM Rate Topic: -----

#1 purvabhilare1587  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 27-May 12

InvalidOperationException Unhandled

Posted 20 July 2012 - 03:15 AM

Hi,

I am displaying a record from database in a datagridview.The record which the user wants is successfully displayed in the datagridview.Now when I edit this record and click on the save button to save changes to the database, I get the following error: Update unable to find TableMapping['Infotable'].Someone please tell how do I resolve this?
My code is below:

Public Class EditRecordPage

    Dim dataadapter As New OleDb.OleDbDataAdapter
    Dim ds As New DataSet

    Private Sub EditRecordPage_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        HomePage.Show()
    End Sub

    Private Sub EditRecordPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Private Sub DisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayButton.Click
        Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0 ;Data Source=C:\Users\technowin\Documents\Candidates.accdb;Jet OLEDB:Database Password=techno;"
        Dim sql As String

        If LastNameTextBox.Text = Nothing Then
            sql = "SELECT * FROM Infotable WHERE FirstName = '" & FirstNameTextBox.Text & "' AND MiddleName = '" & MiddleNameTextBox.Text & "'"
        Else
            sql = "SELECT * FROM Infotable WHERE LastName = '" & LastNameTextBox.Text & "' AND FirstName = '" & FirstNameTextBox.Text & "' AND Lastname = '" & LastNameTextBox.Text & "' "
        End If
        ' DataGridView1.Visible = True
        Dim connection As New OleDb.OleDbConnection(cs)

        Dim ds As New DataSet
        Dim da As New OleDb.OleDbDataAdapter
        Dim str As String

        'check if the record exists in the database
        str = "Select * From Infotable Where LastName='" & LastNameTextBox.Text & "' AND FirstName='" & FirstNameTextBox.Text & "' AND MiddleName='" & MiddleNameTextBox.Text & "' "
        ds = New DataSet()
        da = New OleDb.OleDbDataAdapter(str, connection)
        da.Fill(ds)

        'if record is found then display record
        If (ds.Tables(0).Rows.Count > 0) Then
            dataadapter = New OleDb.OleDbDataAdapter(sql, connection)
            ds = New DataSet()
            connection.Open()


            'Dim dgvcCombo As New DataGridViewComboBoxColumn
            'Dim dgvwCombo As New DataGridViewComboBoxColumn




            'With dgvcCombo
            '.Width = 150
            '.Items.Add("Excellent")
            '.Items.Add("Very Good")
            '.Items.Add("Good")
            '.Items.Add("Okay")
            '.Items.Add("Bad")
            '.DataPropertyName = "ComputerAwareness"
            '.HeaderText = "ComputerAwareness"
            'End With

            'EditDataGridView.Columns.Add(dgvcCombo)

            'With dgvwCombo
            '.Width = 150
            '.Items.Add("APMC")
            '.Items.Add("Bandra Terminus")
            '.DataPropertyName = "WorkCenter"
            '.HeaderText = "WorkCenter"
            'End With

            'EditDataGridView.Columns.Add(dgvwCombo)



            'make the datagrid visible
            EditDataGridView.Visible = True
            ' DataGridView1CloseButton.Visible = True
            dataadapter.Fill(ds, "Infotable")
            connection.Close()
            EditDataGridView.DataSource = ds
            EditDataGridView.DataMember = "Infotable"
        Else
            'if the record is not found then display message
            MsgBox("Record Not Found")
        End If
    End Sub

    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        Dim cmd As New OleDb.OleDbCommandBuilder(dataadapter)
        cmd.GetUpdateCommand()
        dataadapter.Update(ds, "Infotable")
        MsgBox("Data updated successfully")

        'ask user if he wants to continue
        Dim result As Windows.Forms.DialogResult = MessageBox.Show("Do you want to edit more records", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        If result = Windows.Forms.DialogResult.Yes Then

            EditDataGridView.Hide()
            CloseButton.Hide()
            LastNameTextBox.Text = ""
            FirstNameTextBox.Text = ""
            MiddleNameTextBox.Text = ""

        Else

            Me.Close()

        End If

    End Sub





Is This A Good Question/Topic? 0
  • +

Replies To: InvalidOperationException Unhandled

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: InvalidOperationException Unhandled

Posted 20 July 2012 - 05:44 AM

Is Infotable a valid data table?
Was This Post Helpful? 0
  • +
  • -

#3 purvabhilare1587  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 27-May 12

Re: InvalidOperationException Unhandled

Posted 20 July 2012 - 09:45 PM

View Posttrevster344, on 20 July 2012 - 05:44 AM, said:

Is Infotable a valid data table?



Yes it is a valid Data Table.
Was This Post Helpful? 0
  • +
  • -

#4 purvabhilare1587  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 27-May 12

Re: InvalidOperationException Unhandled

Posted 21 July 2012 - 02:42 AM

Someone please reply.its been many days I am trying to do this.
Was This Post Helpful? 0
  • +
  • -

#5 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: InvalidOperationException Unhandled

Posted 21 July 2012 - 08:06 AM

It's been half a day at most, but anyways place a linebreak at the beginning of your code and run through each line with f11. Check for variables to be correct, etc. No one else will debug your code for you, you have to.
Was This Post Helpful? 0
  • +
  • -

#6 purvabhilare1587  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 27-May 12

Re: InvalidOperationException Unhandled

Posted 24 July 2012 - 03:31 AM

View Posttrevster344, on 21 July 2012 - 08:06 AM, said:

It's been half a day at most, but anyways place a linebreak at the beginning of your code and run through each line with f11. Check for variables to be correct, etc. No one else will debug your code for you, you have to.



The code I pasted above now works well.I did not make any changes to the code above.It was not working before.
But now it does work.I dont understand what the problem was.
Thnks trevster for your time.
Was This Post Helpful? 0
  • +
  • -

#7 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: InvalidOperationException Unhandled

Posted 24 July 2012 - 07:31 AM

You're welcome, if the problem arises again come on back, and we'll take a more detailed run through the code and see what's the deal. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1