How to fetch values from SQL Server to C# Webform?

Viewed 28

I'm writing an ASP.NET webforms app and want to fetch SQL Server table names into a dropdown, and based on that I want to populate a gridview on my web form. But I can't find the table name values in a readable format. Also no grid view is being shown below drop down.

My code:

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=SQLD1.ad.shared;Initial Catalog=TestDeployment;Persist Security Info=True;User ID=<UserId>");
    con.Open();

    DataTable schema = con.GetSchema();

    foreach (DataRow row in schema.Rows)
    {
        DropDownList2.Items.Add(row[2].ToString());
    }

    con.Close();
}

public void fillgridview()
{
    SqlConnection con = new SqlConnection("Data Source=SQLD1.ad.shared;Initial Catalog=TestDeployment;Persist Security Info=True;User ID=<UserId>");
    con.Open();

    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM" + DropDownList2.Text,con);

    DataTable dt = new DataTable();
    da.Fill(dt);

    GridView1.DataSource = dt;
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    fillgridview();
}

My output :

Showing this only and not grid view

Added SQL datasource here as well

Any help will be appreciated. I might be making some silly mistake here.

0 Answers
Related