Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
<pre> Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim ConnectionString As String
        Dim binding As New BindingSource
        
        ConnectionString = ("Integrated Security = true;Initial catalog = " & ComboBox2.SelectedText.ToString & ";Data Source = " & ComboBox1.SelectedText & "; ")
        Dim SQL As String = "SELECT * FROM Table_1"
        Dim conn As SqlConnection = New SqlConnection(ConnectionString)

        ' open the connection


        'Create a SqlDataAdapter object
        Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(SQL, conn)

        ' Call DataAdapter's Fill method to fill data from the
        ' Data Adapter to the DataSet
        Dim ds As New DataSet
        'ds.Tables.Add("Table_1")
        conn.Open()
        adapter1.Fill(ds, "Table_1")
        conn.Close()
        binding.DataSource = ds
        binding.DataMember = "Table_1"
        DataGridView1.DataSource = binding

    End Sub




above code doesnt work error :Additional information: Invalid object name 'Table_1'. when I type the Initial catalog like this

VB
pre>Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim ConnectionString As String
        Dim binding As New BindingSource
        ConnectionString = ("Integrated Security = true;Initial catalog = logistiki;Data Source = " & ComboBox1.SelectedText & "; ")
        Dim SQL As String = "SELECT * FROM Table_1"
        Dim conn As SqlConnection = New SqlConnection(ConnectionString)

        ' open the connection


        'Create a SqlDataAdapter object
        Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(SQL, conn)

        ' Call DataAdapter's Fill method to fill data from the
        ' Data Adapter to the DataSet
        Dim ds As New DataSet
        'ds.Tables.Add("Table_1")
        conn.Open()
        adapter1.Fill(ds, "Table_1")
        conn.Close()
        binding.DataSource = ds
        binding.DataMember = "Table_1"
        DataGridView1.DataSource = binding

    End Sub

it works it accepts the combobox1 selected text but not the combobox2

any help?

What I have tried:

added a variable for combox2 selected text to sql string but doesnt work either
Posted
Updated 20-Nov-19 21:32pm
v2

if you have many databases and servers, use SqlConnectionStringBuilder. Here is an example using SqlConnectionStringBuilder to create connection string | C# Online Compiler | .NET Fiddle[^].

If you have few, I would suggest to keep in config file and write switch case to decide connection string.
 
Share this answer
 
Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that throughout your app, and the chances are that the problem you have noticed will go away as well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900