Hi All,

I have created a form for a user to create a booking of a meeting room, I need this form to do 3 things.

have print functionality

update the new booking to a dataset

view the current bookings screen

I seem to be struggling with the second task, when I try and save my new booking I get this error

System.InvalidOperationException was unhandled
  Message="Update unable to find TableMapping['mtrbookings'] or DataTable 'mtrbookings'."

Please see the code for my form.

Imports System.Data.SqlClient
Public Class Form1
    Dim conn As SqlConnection
    Dim Bookings As SqlDataAdapter
    Dim dsBookings As DataSet
    Dim cmdBuilder As SqlCommandBuilder = New SqlCommandBuilder(Bookings)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            conn = New SqlConnection("Data Source=JOHN-ELLIS-PC;Initial Catalog=mtrbookings;Integrated Security=True")
            conn.Open()
        Catch ex As Exception
            MessageBox.Show("CONNECTION FAILED" & ex.Message)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form2.Show()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dsBookings = New DataSet
        Bookings = New SqlDataAdapter("SELECT BookingID,RoomID,UserID,BookDate,StartTime,EndTime,Reason FROM Bookings", conn)
        Bookings.Update(dsBookings, "mtrbookings")
    End Sub
End Class

My feeling is that I have not declaired any data sources, like a data grid view or the text boxes from which my information is typed by the user (text boxes).

Can anyone help.

Thanks very much

John

Sub Test()
conn = New SqlConnection("Data Source=JOHN-ELLIS-PC;Initial Catalog=mtrbookings;Integrated Security=True")
dsBookings = New DataSet
Bookings = New SqlDataAdapter("SELECT BookingID,RoomID,UserID,BookDate,StartTime,EndTime,Reason FROM Bookings", conn)
Dim cmdBuilder As SqlCommandBuilder = New SqlCommandBuilder(Bookings)


Bookings.Update(dsBookings, "mtrbookings")

Thanks for the reply on that adatapost. I re wrote and fixed the error, however now my database won't update.

Please see my code below

Imports System.Data.SqlClient  

Public Class Form1  
Dim conn As SqlConnection  
Dim Bookings As SqlDataAdapter  

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

Me.BookingsTableAdapter.Fill(Me.MtrbookingsDataSet.Bookings)  

Try  

conn = New SqlConnection("Data Source=JOHN-ELLIS-PC;Initial Catalog=mtrbookings;Integrated Security=True")  

conn.Open()  

Bookings = New SqlDataAdapter("SELECT BookingID,RoomID,UserID,BookDate,StartTime,EndTime,Reason FROM Bookings", conn)  

TextBox1.Text = "" 
TextBox2.Text = "" 
TextBox3.Text = "" 
TextBox4.Text = "" 
TextBox5.Text = "" 
TextBox6.Text = "" 
TextBox7.Text = "" 

Catch ex As Exception  

MessageBox.Show("CONNECTION FAILED" & ex.Message)  

End Try  

End Sub  

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click  

Form2.Show()  

End Sub      

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  

Me.BookingsTableAdapter.Update(Me.MtrbookingsDataSet.Bookings)  

End Sub  

End Class

I dont understand because I have put the update command in for the dataset.

Can anyone see what I have done wrong.

Thanks very much

John

I have just had a thought, is it because I am using the select command

Bookings = New SqlDataAdapter("SELECT BookingID,RoomID,UserID,BookDate,StartTime,EndTime,Reason FROM Bookings", conn)

Should I be using INSERT INTO

Bookings = New SqlDataAdapter("INSERT INTO BookingID,RoomID,UserID,BookDate,StartTime,EndTime,Reason FROM Bookings", conn)

I will test this theory tonight.

John

Have a look at sample,

Imports System.Data.SqlClient

Public Class Sample1
    Inherits Windows.Forms.Form

    Friend WithEvents TxtEno As System.Windows.Forms.TextBox
    Friend WithEvents TxtEname As System.Windows.Forms.TextBox
    Friend WithEvents TxtEdate As System.Windows.Forms.TextBox
    Friend WithEvents BtnNew As System.Windows.Forms.Button
    Friend WithEvents BtnCancel As System.Windows.Forms.Button
    Friend WithEvents BtnSave As System.Windows.Forms.Button
    Friend WithEvents BtnNext As System.Windows.Forms.Button
    Friend WithEvents BtnPrev As System.Windows.Forms.Button
    Private Sub InitializeComponent()
        Me.BtnPrev = New System.Windows.Forms.Button
        Me.BtnNext = New System.Windows.Forms.Button
        Me.BtnSave = New System.Windows.Forms.Button
        Me.BtnCancel = New System.Windows.Forms.Button
        Me.BtnNew = New System.Windows.Forms.Button
        Me.TxtEdate = New System.Windows.Forms.TextBox
        Me.TxtEname = New System.Windows.Forms.TextBox
        Me.TxtEno = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'BtnPrev
        '
        Me.BtnPrev.Location = New System.Drawing.Point(290, 192)
        Me.BtnPrev.Name = "BtnPrev"
        Me.BtnPrev.Size = New System.Drawing.Size(52, 23)
        Me.BtnPrev.TabIndex = 7
        Me.BtnPrev.Text = "Prev"
        Me.BtnPrev.UseVisualStyleBackColor = True
        '
        'BtnNext
        '
        Me.BtnNext.Location = New System.Drawing.Point(348, 192)
        Me.BtnNext.Name = "BtnNext"
        Me.BtnNext.Size = New System.Drawing.Size(52, 23)
        Me.BtnNext.TabIndex = 9
        Me.BtnNext.Text = "Next"
        Me.BtnNext.UseVisualStyleBackColor = True
        '
        'BtnSave
        '
        Me.BtnSave.Location = New System.Drawing.Point(174, 192)
        Me.BtnSave.Name = "BtnSave"
        Me.BtnSave.Size = New System.Drawing.Size(52, 23)
        Me.BtnSave.TabIndex = 10
        Me.BtnSave.Text = "Save"
        Me.BtnSave.UseVisualStyleBackColor = True
        '
        'BtnCancel
        '
        Me.BtnCancel.Location = New System.Drawing.Point(116, 192)
        Me.BtnCancel.Name = "BtnCancel"
        Me.BtnCancel.Size = New System.Drawing.Size(52, 23)
        Me.BtnCancel.TabIndex = 11
        Me.BtnCancel.Text = "Cancel"
        Me.BtnCancel.UseVisualStyleBackColor = True
        '
        'BtnNew
        '
        Me.BtnNew.Location = New System.Drawing.Point(58, 192)
        Me.BtnNew.Name = "BtnNew"
        Me.BtnNew.Size = New System.Drawing.Size(52, 23)
        Me.BtnNew.TabIndex = 8
        Me.BtnNew.Text = "New"
        Me.BtnNew.UseVisualStyleBackColor = True
        '
        'TxtEdate
        '
        Me.TxtEdate.Location = New System.Drawing.Point(307, 58)
        Me.TxtEdate.Name = "TxtEdate"
        Me.TxtEdate.Size = New System.Drawing.Size(100, 20)
        Me.TxtEdate.TabIndex = 6
        '
        'TxtEname
        '
        Me.TxtEname.Location = New System.Drawing.Point(201, 58)
        Me.TxtEname.Name = "TxtEname"
        Me.TxtEname.Size = New System.Drawing.Size(100, 20)
        Me.TxtEname.TabIndex = 5
        '
        'TxtEno
        '
        Me.TxtEno.Location = New System.Drawing.Point(82, 59)
        Me.TxtEno.Name = "TxtEno"
        Me.TxtEno.Size = New System.Drawing.Size(100, 20)
        Me.TxtEno.TabIndex = 4
        '
        'Sample1
        '
        Me.ClientSize = New System.Drawing.Size(464, 273)
        Me.Controls.Add(Me.BtnPrev)
        Me.Controls.Add(Me.BtnNext)
        Me.Controls.Add(Me.BtnSave)
        Me.Controls.Add(Me.BtnCancel)
        Me.Controls.Add(Me.BtnNew)
        Me.Controls.Add(Me.TxtEdate)
        Me.Controls.Add(Me.TxtEname)
        Me.Controls.Add(Me.TxtEno)
        Me.Name = "Sample1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Sub New()
        InitializeComponent()
    End Sub

    Private Cn As SqlConnection
    Private Adp As SqlDataAdapter
    Private Cmb As SqlCommandBuilder
    Private Dt As DataTable
    Private Sub Sample1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Cn = New SqlConnection("connecttion_string")

        'Table emp has three columns : eno int primarykey, ename varchar(50), edate datetime
        Adp = New SqlDataAdapter("select * from emp", Cn)
        Cmb = New SqlCommandBuilder(Adp)
        Dt = New DataTable()


        Adp.Fill(Dt)

        'Simple/single data binding
        TxtEno.DataBindings.Add("Text", Dt, "eno")
        TxtEname.DataBindings.Add("Text", Dt, "ename")
        TxtEdate.DataBindings.Add("Text", Dt, "edate")
    End Sub

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        Me.BindingContext(Dt).AddNew()
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.BindingContext(Dt).CancelCurrentEdit()

    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Me.BindingContext(Dt).EndCurrentEdit()
        Adp.Update(Dt)
    End Sub

    Private Sub BtnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrev.Click
        Me.BindingContext(Dt).Position -= 1
    End Sub

    Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click
        Me.BindingContext(Dt).Position += 1
    End Sub
End Class

Thanks for the help on this one adatapost. Things have worked out great.

Thanks again

John

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.