I am making an windows application in which I have multiple controls that displays the value from different sql database tables. I want to make an update function that takes table name, columnName as a parameter but its not working.
here is the code
void updateThis(string tableName, string columnName, string textBoxValue)
{
using (SqlConnection conn = new SqlConnection("Server=.\\SQLEXPRESS;Database=FIR_db; User Id = sa; Password = 9889922527"))
{
//try
{
cmd = new SqlCommand("update @t set @a = @b where profile_id= @c", conn);
conn.Open();
cmd.Parameters.AddWithValue("@t", tableName);
cmd.Parameters.AddWithValue("@a", columnName);
cmd.Parameters.AddWithValue("@b", textBoxValue);
cmd.Parameters.AddWithValue("@c", lblprofil.Text);
cmd.ExecuteNonQuery();
}
//catch (SqlException exc)
//{
// DialogResult dr = MessageBox.Show("Error in server. Could not load Designation.", "Error in server", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
}
}
when I use this function it shows an sqlException "Must declare the table variable "@t". " By the way is this possible or not.