I have created 2 DropdownList and 1 GridView
1st DropdownList Loads and Displays DataBase Names Dynamically
2nd DropdownList Loads and Displays Table Names, Based on Database name Which selected in 1st drop down list
Based on Table Name Data has to be displayed in GridView........
I have written code which displays Database Names and working fine
private void populateDatabasename() {
SqlConnection con = new SqlConnection(@"Data Source=SAI- PC\SQLEXPRESS;Integrated Security=True");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select name,collation_name from sys.databases order by name", con);
DataSet ds = new DataSet();
da.Fill(ds, "dbname");
DropDownList1.DataSource = ds.Tables["dbname"];
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();
}
Based on Database name tables have to be displayed..... how to pass Database name(Which is selected in 1st drop down list) in the following code..... Is This a correct way to pass database name
private void populateTableName() {
SqlConnection con = new SqlConnection(@"Data Source=SAI-PC\SQLEXPRESS;Integrated Security=True");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select name from "+"@Dbname"+".sys.tables", con);
da.SelectCommand.Parameters.Add("@dbname", SqlDbType.VarChar);
da.SelectCommand.Parameters["@dbname"].Value = DropDownList1.SelectedValue;
DataSet ds = new DataSet();
da.Fill(ds, "dbname1");
DropDownList2.DataSource = ds.Tables["dbname1"];
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataBind();
}
SqlDataAdapteryou used@Dbnamewith capital D and when you assign the parameter you used@dbnamewith a small d. Don't know if it's case sensitive. - Robin V.