I've a TextBox
where a user will enter a table's name and it'll be passed into the query. The query will pull data from database and display it on the data table.
But I can't seem to do it with SqlAdapter
, I always get this error like executescalar
when I can easily add the paremeters and pass it in.
Incorrect syntax near '@a'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '@a'.
Source Error:
Line 58: adapt.SelectCommand.Parameters.AddWithValue("@a", "%" + Selectdb.Text + "%"); Line 59: DataTable dt = new DataTable(); Line 60: adapt.Fill(dt); Line 61:
if (dt.Rows.Count > 0) Line 62: {
protected void btn_selectdb_Click(object sender, EventArgs e)
{
Label1.Text = "ready";
con = new SqlConnection(constr);
con.Open();
string a = Convert.ToString(Selectdb.Text);
string cmdstr = "Select userid,username,email,eventname from @a";
SqlCommand cmd = new SqlCommand(cmdstr, con);
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
adapt.SelectCommand.Parameters.AddWithValue("@a", "%" + Selectdb.Text + "%");
DataTable dt = new DataTable();
adapt.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}