Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,,, frnds i am having the column names in the dropdown list when i can select one column name means that column data should be display in the gridview 


What I have tried:

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList2.SelectedIndex != 0)
    {
        cmd = new SqlCommand("select * from " + DropDownList2.SelectedItem.Value, con);
        da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
Posted
Updated 6-Jun-17 1:08am
Comments
jimmson 6-Jun-17 6:23am    
Your question is missing a question.
F-ES Sitecore 6-Jun-17 7:18am    
You have to be about the third person to ask this question. Can you guys not just all get together after class and solve the problem among yourselves?
ZurdoDev 6-Jun-17 7:42am    
Why 3 commas? It's very distracting.

1 solution

try
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
       {

           if (DropDownList2.SelectedIndex != 0)
           {
               string columnName = DropDownList2.SelectedItem.Value;
               string tableName ="Your_Table_Name";
               string query = string.Format("select {0} from {1}", columnName, tableName);
               cmd = new SqlCommand(query, con);
               da = new SqlDataAdapter(cmd);
               DataTable dt = new DataTable();
               da.Fill(dt);
               GridView1.DataSource = dt;
               GridView1.DataBind();
           }
       }


Note: Take care of SQL Injection[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900