views:

46

answers:

3

I want to paste table name as function parameter and function need to return DataSet, this is the my code for that:

 Public Function GetTTabele(ByVal tableName As String) As DataSet
        Dim DAT As SqlDataAdapter = New SqlDataAdapter("SELECT *  FROM tableName", nwindConn)
        Dim DAT  As DataSet = New DataSet()
        DAT.MissingSchemaAction = MissingSchemaAction.AddWithKey
        DAT.Fill(DAT, tableName)
        GetTTabele = DAT 
    End Function

Now when i execute this code I'm getting next error: System.Data.SqlClient.SqlException: Invalid object name 'tableName'.

+2  A: 

Table "tableName" does not exists on your database. Specify a existing table name.

Jojo Sardez
I paste in function name of table that exists in database!GetTTabele(Orders)
Comii
But currently you are passing the string tableName, instead of the variable string tableName. See nicholas answer
PoweRoy
A: 

Change the line of code

 Dim DAT As SqlDataAdapter = New SqlDataAdapter("SELECT *  FROM tableName", nwindConn)

to read

 Dim DAT As SqlDataAdapter = New SqlDataAdapter("SELECT *  FROM " & tableName, nwindConn)

You are trying to find a table called, literally, "tableName", rather than the table name stored in the variable tableName.

RB
+2  A: 

"SELECT * FROM tableName"

should be changed to "SELECT * FROM " & tableName

allowing the contents of your parameter tableName to be appended to the string "SELECT * FROM "

Nicholas Murray