0

I have a listbox and a datagrid.

When the form is loaded the listbox is filled with a list of tables from a selected database (SQL 2008).

sqlstr = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME"

Then I have a SelectedIndexChanged that populates the datagrid with the contents of the selected table.

Dim ConnString As String = mySQLconn

        Dim ds As DataSet
        Dim dv As DataView
        Try
            Dim SQLConn As New SqlConnection(ConnString) 'The SQL Connection

            ds = New DataSet
            dv = New DataView

            Dim ad As New SqlDataAdapter("SELECT * FROM " + lbListAllTables.SelectedItem, SQLConn)
            ad.Fill(ds, "SelectedTable")
            dv.Table = ds.Tables("SelectedTable")

            Me.DataGridView1.DataSource = dv
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try

This works just fine if the table name is one word (clients) but if it two words (setup client) I get a 'Incorrect syntax near the keyword 'setup'' error.

4
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by sknake
0

First of all, you should use parameters instead of concatenating query strings. That in itself might resolve the issue but either way you can add brackets around the [table name] so the database knows.

0
Dim ad As New SqlDataAdapter("SELECT * FROM '" & lbListAllTables.SelectedItem.ToString & "'", SQLConn)

i have to agree with TomW. it is very unsafe not to use parameter in sql queries.

0

This is my first day working with datagridview etc so I have no idea how you would complete the same thing with parameters.

0

Here is an example.

Also in you example, a dataview is just making an additional copy of your datasets table. You dont really need it unless you want to filter the results further.

sqlstr = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME"
Dim ConnString As String = mySQLconn

        Dim ds As DataSet
        Dim dv As DataView
        Dim cmd As New DataCommand

        Try
            Dim SQLConn As New SqlConnection(ConnString) 'The SQL Connection

            ds = New DataSet
            'dv = New DataView

            Dim ad As New SqlDataAdapter()
            
            cmd.Connection = SQLConn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "SELECT * FROM @TableName"
            cmd.Parameters.AddWithValue("@TableName", "[" & lbListAllTables.SelectedItem & "]")

            ad.SelectCommand = cmd
            ad.Fill(ds, "SelectedTable")

            'dv.Table = ds.Tables("SelectedTable")

            Me.DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
0

Thank you all so much for your help. When I try to use you code I get a Type 'Sql.DataCommand' is not defined error.

Do I need to imports something?

0

DataCommand is defined in Microsoft.ManagementConsole.Internal but you could use an SqlCommand which is defined in System.Data.SqlClient

0

My fault, Sknake is right - the command object is actually sqlCommand not datacommand. See what I get for coding in this text box with out the autocomplete features... lol

0

Thanks for the help, working good.

Do I have to setup something different to update the database if users make changes in the datagridview ?

0

Yes. You have Select Queries, Update Queries, Insert Queries, and delete queries. In your example you provided the select query:

cmd.Connection = SQLConn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "SELECT * FROM @TableName"
            cmd.Parameters.AddWithValue("@TableName", "[" & lbListAllTables.SelectedItem & "]")

            ad.SelectCommand = cmd

Take a look at ad.UpdateCommand , ad.InsertCommand , and ad.DeleteCommand on your SqlDataAdapter

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.