Hello all, I'm somewhat new to programming but I had a quick question. I have created a program to take a text file and convert it into a datatable. Unfortunately, however, I seem unable to attach this file to a database. I am getting an invalid object error stating that there is no table named "DataTable"

Here is my code for attaching to the database. Thank you very much in advance!

Private Sub AddTableToDataBase(ByVal dataTable As DataTable)

Dim objConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("InitialCatalog").ConnectionString)

Dim ds As DataSet = New DataSet
Dim dataAdapter As New SqlDataAdapter("Select * From DataTable", objConn)
Dim cmd As SqlCommandBuilder

ds.Tables.Add(dataTable)

dataAdapter.Fill(dataTable)

cmd= New SqlCommandBuilder(dataAdapter)
dataAdapter.Update(ds, "DataTable")

Is there a table in your database called "DataTable"? If not revise line 6.

I personally wouldn't use the word "datatable" so much, it makes it kind of confusing to know what you're talking about where(a table called datatable, a variable called datatable and built-in object called datatable. Might make your life easier.

forgive me.. "DataTable" and dataTable refer to the same data table. The declaration to create the table was:

Dim dataTable As DataTable = new DataTable("DataTable")

do you need the code I used to convert the text to a data table?

steveneaster: Datatable is a function to do with the database controls not a table.

Aldius: Its a bit long winded, but one way you could do this is through comma delimited view in a text box. You can read from the field names and input them into variables and use the variable in an SQL statement input into the database

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.