This code:
public bool IsValidColumn(string tableName, string columnName)
{
//return true;// <-- when I uncomment this, I don't get the err msg (but I don't know whether it's a valid column or not, either)
bool validColumn = false;
string tblQuery = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tableName AND COLUMN_NAME = @columnName";
checkConnection();
try
{
SqlCeCommand cmd = objCon.CreateCommand();
cmd.CommandText = tblQuery;
SqlCeParameter tblNameParam = new SqlCeParameter("@tableName", SqlDbType.NVarChar, 128);
tblNameParam.Value = tableName;
cmd.Parameters.Add(tblNameParam);
SqlCeParameter colNameParam = new SqlCeParameter("@columnName", SqlDbType.NVarChar, 128);
colNameParam.Value = tableName;
cmd.Parameters.Add(colNameParam);
//int i = (int)cmd.ExecuteScalar();
int i = 0;
object obj = cmd.ExecuteScalar();
if ((obj != null) && (obj != DBNull.Value))
{
i = Int32.Parse(obj.ToString());
validColumn = i > 0;
}
else
{
MessageBox.Show("NULL returned from ExecuteScalar. Remove this line.");
}
}
catch (SqlCeException sqlceex)
{
return false;
}
catch (Exception ex)
{
return false;
}
return validColumn;
}
...was adapted from/based on an earlier answer here: How can I determine whether a column exists in a SQL Server CE table with C#?
And yet, it causes me to get, "There was an error parsing the query. [Token line number, Token line offset,, Token in error,,]" (which I complained about here: Why is there an err parsing this DDL, and why can't the engine be more specific about locating it? and here:
SQL Server CE's DDL parser is very picky but secretive about what it is finding fault with).
In my attempts to get to the bottom of what was causing this err msg, I've actually refactored the legacy code a fair amount (threw a lot of rancid spaghetti out of the fridge), so it hasn't been a waste of time, but now I find that what was causing that err msg was something in the code above. So the question is: How can I determine whether a column is valid without generating this err msg?