I'm trying to use VB.NET to insert the data in the current row of my datagrid into a table. I wrote the following code:

Dim vCustomer, vBulkQty, vItem, vBaseUnit, vEachQty, vEachUnit As String
        vCustomer = dgCustomers.CurrentRow.Cells(0).Value
        vItem = dgItems.CurrentRow.Cells(0).Value
        vBulkQty = dgItems.CurrentRow.Cells(3).Value
        vBaseUnit = dgItems.CurrentRow.Cells(4).Value
        vEachQty = dgItems.CurrentRow.Cells(5).Value
        vEachUnit = dgItems.CurrentRow.Cells(6).Value

        Dim Insert As String

        Insert = "INSERT INTO OTM_ORDERS(CustomerID,ItemNo,BulkQty,BaseUnit,EachQty,StockUnit) " _
        & "VALUES ('" & Trim(vCustomer) & "','" & Trim(vItem) & "','" & Trim(vBulkQty) & "','" _
        & Trim(vBaseUnit) & "','" & Trim(vEachQty) & "','" & Trim(vEachUnit) & "')"

        Dim sqlCom As New SqlCommand(Insert, conn)

The problem is that although there doesn't seem to be anything wrong with the code I get an unhandled SQLException on the sqlCom.ExecuteNonQuery() statement that says "Invalid object name 'OTM_ORDERS'."

I've already created this table using SQL, so why am I not able to insert data into it?

Can you show which do you use connection string with your database? Please ensure your connection string is correct.


The connection string is correct. I can pull information from the same database and display it - I am trying to save the information taken from one table into a newly created table.

Just to let you know, everyone, that the code was correct. When I ran the code to create the table, it was created on the wrong server... so when I ran it, the application was looking for the created table on the wrong server. It's working as expected now.

Sorry for any inconvenience...

This question has already been answered. Start a new discussion instead.