Hi
I'm using a simple insert statemnt in from two textfield into my database
using this code

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

        If (TextBox1.Text.Trim = "") Then
            MessageBox.Show("PLEASE DO NOT USE BLANK SPACES", "Data Entry Error")

        ElseIf (TextBox2.Text.Trim = "") Then
            MessageBox.Show("PLEASE DO NOT USE BLANK SPACES", "Data Entry Error")

        Else     'if user has not entered a blank space 
            'Server=.\SQLExpress;AttachDbFilename=|DataDirectory|MyDataFile.mdf;Database=dbname;Trusted_Connection = Yes(Example)
            'Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\Paras\documents\visual studio 2010\Projects\ESI_PF_Payroll_V1\ESI_PF_Payroll_V1\Payroll.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True
            con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Anuj Rishi\documents\visual studio 2010\Projects\ESI_PF_Payroll_V1\ESI_PF_Payroll_V1\Payroll.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "INSERT INTO tbllogin (username,password) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "')"
            cmd.ExecuteNonQuery()
            MessageBox.Show(cmd.CommandText.ToString())
            adapter = New SqlDataAdapter
            adapter.InsertCommand = cmd
            adapter.Update(data)
            'adapter.TableMappings.Add("tbllogin", adapter.ToString())
            con.Close()
            MessageBox.Show("Data Inserted")
            Login.Show()
            End If



    End sub

getting this error

 Update unable to find TableMapping['Table'] or DataTable 'Table'.

Recommended Answers

All 2 Replies

When you create a data adapter, it knows that it is dealing with "Table", so you have to tell it to map that internal "Table" to your table, or as you have seen, things don't work too well; it tries to access your database object called "Table", which is not a valid table name. After defining your commands (Select, Insert, etc.), you need to tell the data adapter what to do with the internal "Table" by mapping it to your table in the database. Additional mappings can also be defined here, as shown in the on-line documentation.

Here is an example modified from some code I use in one of my projects. It is written for OleDB, instead of SQLDB, but the idea should be the same. Note that strTableName is not "Table", but the name of your table in the database itself.

Dim dtaAdapter As Data.OleDb.OleDbDataAdapter
REM These actually get passed as function parameters.
Dim conDB As Data.OleDb.OleDbConnection
Dim strTableName As String   ' The name of the table we are working with.
Dim strFieldList As String   ' A comma seperated list of field names.
Dim strValues As String      ' The values to write into the corresponding table.

dtaAdapter = New Data.OleDb.OleDbDataAdapter("SELECT " & strFieldList & " FROM " & strTableName, conDB)
dtaAdapter.InsertCommand = "INSERT INTO " & strTableName & "(" & strFieldList & ") VALUES (" & strValues & ")"
dtaAdapter.TableMappings.Add("Table", strTableName)

You may also run into problems with the field password. In some SQL implementations, password is a reserved word. If you must use this field name, you can use it in queries as [password].

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.