Thanks for the help, I was able to figure out how to insert the new items into the listbox and display them on a new windows form. I have another question though, My listbox on the new form is full of database names, So how do I display the sql tables when the user selects a database from the listbox on the new form and display the sql tables in another listbox on the same form since everything is being created dynamically?
Can someone please help
This is what I tried
//get databases
Label labels = new Label();
labels.Location = new System.Drawing.Point(10, 35);
labels.Text = "Select a database from the list";
ListBox listbox1 = new ListBox();
listbox1.Size = new System.Drawing.Size(100, 100);
listbox1.Location = new System.Drawing.Point(10, 60);
Form form = new Form();
DataTable database = myConnection.GetSchema("Databases");
foreach (DataRow row in database.Rows)
listbox1.Items.Add(row["database_name"]);
form.Controls.Add(listbox1);
form.Controls.Add(labels);
string selectedString = listbox1.SelectedIndex.ToString();
// get tables
ListBox listbox2 = new ListBox();
listbox2.Size = new System.Drawing.Size(100, 100);
listbox2.Location = new System.Drawing.Point(120, 70);
Label label = new Label();
label.Location = new System.Drawing.Point(40, 35);
labels.Text = "Select a table from list";
DataTable tables = new DataTable("Tables");
SqlCommand command = myConnection.CreateCommand();
command.CommandText = "SELECT table_name as Name FROM INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE='BASE TABLE'";
tables.Load(command.ExecuteReader());
foreach (DataRow table in database.Rows)
listbox2.Items.Add(tables);
form.Controls.Add(listbox2);
form.Controls.Add(label);
form.Show();
This seems a bit suspect:
-
DataTable database = myConnection.GetSchema("Databases");
-
foreach (DataRow row in database.Rows)
-
listbox1.Items.Add(myConnection.GetSchema("Databases"));
-
Shouldn't you be adding in some value from row, and not the whole collection again?
Maybe like
-
DataTable database = myConnection.GetSchema("Databases");
-
foreach (DataRow row in database.Rows)
-
listbox1.Items.Add(row.Cells[3].Value.ToString());
-
(There are probably typos in there, but you should see what I mean)