Im trying to get a simple SQL statement in my code, and get a DataTable, and for some reason I get this weird exception :
Invalid column name
This is my code :
public DataTable GetAllVideoID(string stringId)
{
dt = new DataTable();
dataObj = new DataObj();
sql = "SELECT * FROM TBL_VIDEOS WHERE TBL_VIDEOS.ID=" + stringId;
return dt = dataObj.SelectDataTable(sql);
}
public DataTable SelectDataTable(string sql)
{
try
{
conn = new SqlConnection(conString);
conn.Open();
adapter = new SqlDataAdapter(sql, conn);
dt = new DataTable();
adapter.Fill(dt);
return dt;
}
catch (Exception e)
{
throw e;
}
finally { conn.Close(); }
}
When I run this on my management tool, just the statemnet – it works perfectly.
So I dunno ..
Structure of my DB :
ID,TITLE,V_DESCIPTION,UPLOAD_DATE,V_VIEW,USERNAME,RATING,V_SOURCE,FLAG
thanks 🙂
changing your sql to
could fix this. That puts a single quote around the value of stringId e.g.
.ID = '10'If stringId = ‘user’ your query becomes
but user is not a valid variable in sql, so you get invalid column name