Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 



Sign in to follow this  
mstester

Displaying a list of Databases in a combo box in VB.NET

Recommended Posts

Hi guys,

Ive a simple question for you all (well what i hope is a simple question :) )

Lets say I have a msql server with three databases created i.e. test, test_1 and test_3. Is there a way that I can display these three databases in a combo box? I want the user to be able to select which database he/she wishes to connect to. Im using VB.NET for this and any help would be really cool :)

Thanks in advance.

Share this post


Link to post
Share on other sites

should the statement not be



Show databases;

See my problem is that i dont know how to populate the combobox with the result :S

Share this post


Link to post
Share on other sites

I quicky dit it in a Asp.Net / Vb.Net project:

    code for the page:
<div>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="name" DataValueField="name">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [name] FROM [master].[dbo].[sysdatabases] Order By [name]"></asp:SqlDataSource>
</div>

code for the web config:

<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=SERVER2003;Initial Catalog=master;User ID=sa;Password=mysapassword"
providerName="System.Data.SqlClient" />
</connectionStrings>

Share this post


Link to post
Share on other sites

Thanks for the help guys, I figured it out below is a snippet of the code:

However now my problem is that I cant figure out a way to allow the user to connect to the database which they just selected, any ideas??

Note: The code is written very poorly :blink:



Dim conn As MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim dataset As New DataSet
Dim SQL As String
Dim myData As New DataTable

conn = New MySqlConnection
conn.ConnectionString = "server=*******;" & "user id=" & usernametxt.Text & ";" _
& "password=" & pwdtxt.Text & ";"
Try
conn.Open()
MessageBox.Show("Connection Opened Successfully!")
Catch myerror As MySqlException
MessageBox.Show("Error was encountered when connecting" & myerror.Message)
Finally

End Try


SQL = "Show Databases;"
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
comboBox.DataSource = myData
comboBox.DisplayMember = "Database"
conn.Close()


Edited by mstester

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×