I am using the following code to attempt to get a list of tables from a list of databases that I previously fetched. I cannot get the tables to work because I cannot split the connection string for some odd reason. I should tell you that I am very new to C#. I was taking Java at school and decided that C# was much better for me.
This is the code to get my list of databases from the SQL server:
while (SqlDR.Read()) { comboBox1.Items.Add(SqlDR.GetString(0)); } }
This is my code to get the tables from the database that I just fetched: private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { comboBox2.Items.Clear(); SqlConnection SqlConn = new SqlConnection("Data Source=10.100.1.6; Initial Catalog=" +comboBox1.SelectedItem.ToString +";Trusted_Connection=True;"); SqlConn.Open();
Mahesh ChandRank #1 · 393K pointsPosted Oct 29, 2010, 4:18 PM EDT
ToString() is a method.
Add closing brackets after ToString.
+ comboBox1.Selectedtem.ToString() +
Matt Butler11 pointsPosted Oct 29, 2010, 11:51 AM EDT
Here is the code again.
This is the code to get my list of databases from the SQL server:
private void frmImport_Load(object sender, EventArgs e)
{
comboBox2.Items.Clear();
SqlConnection SqlConn = new SqlConnection("Data Source=10.100.1.6; Trusted_Connection=True;");
SqlConn.Open();
SqlCommand SqlCom = new SqlCommand();
SqlCom.Connection = SqlConn;
SqlCom.CommandType = CommandType.StoredProcedure;
SqlCom.CommandText = "sp_databases";
SqlDataReader SqlDR;
SqlDR=SqlCom.ExecuteReader();
while (SqlDR.Read())
{
comboBox1.Items.Add(SqlDR.GetString(0));
}
}
This is my code to get the tables from the database that I just fetched:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
SqlConnection SqlConn = new SqlConnection("Data Source=10.100.1.6; Initial Catalog=" +comboBox1.SelectedItem.ToString +";Trusted_Connection=True;");
SqlConn.Open();
SqlCommand SqlCom = new SqlCommand();
SqlCom.Connection = SqlConn;
SqlCom.CommandType = CommandType.StoredProcedure;
SqlCom.CommandText = "sp_tables";
SqlDataReader SqlDR;
SqlDR = SqlCom.ExecuteReader();
while (SqlDR.Read())
{
comboBox2.Items.Add(SqlDR.GetString(0));
}
}
Matt Butler11 pointsPosted Nov 1, 2010, 5:18 AM EDT
That was the problem. Thanks so much for your assistance on this. It looks like I need to learn the nuances.
Kind Regards,
Matt