Here’s my code. I am not able to set TableName variable, it is throwing an exception
Must declare the table variable “@TableName”
public DataTable getAllDataFromTable(String TableName)
{
cmd.CommandText = "select * from @TableName";
cmd.Parameters.AddWithValue("@TableName", TableName);
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
}
You cannot use a parameter in that fashion. The best way to implement what you are looking for is to use a string.Format() method to create your select statement. The only real draw back is the fact you open the method up to SQL Injection.
public DataTable getAllDataFromTable(String TableName)
{
Here is a similar thread.
Table name and table field on SqlParameter C#?