I think my question is quite straight forward.
Assume I have a gridview, a textbox and a button control in my .aspx page. What I want to do is pasting (or typing) the “table name of my database” into the textbox, and when the button is clicked the gridview shall display all columns from the respective table name only.
Thanks and regards
Update:
1. I forget to tell that I also need to allow editing, deleting and selecting command in the gridview.
2. Thank’s for all the comments, I have tried few solutions. However I found out that the most practical way is to use DataSource (allow auto generation of insert, update, delete command). So far based on my research on Google, I haven’t found any tutorials that can really teach using this method comprehensively.
My Current Code is like this:
protected void btnFind_Click(object sender, EventArgs e)
{
// sdsDynamic is the sql data source
// gvDynamic is the gridview
// txtTable is the textbox where the table name is contained
SqlConnection connection = new SqlConnection(myConnectionString);
string sqlText = "SELECT * FROM " + txtTable.Text;
sdsDynamic.SelectCommand = sqlText;
SqlDataAdapter adapter = new SqlDataAdapter(sqlText, connection);
adapter.SelectCommand.CommandType = CommandType.Text;
connection.Open();
gvDynamic.DataSource = sdsDynamic;
gvDynamic.DataBind();
connection.Close();
}
Below code block will satisfy your requirement:
In .aspx page, the gridview code may look like:
In code behind the code may may use: