-1

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?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

1

From your comments, I see this message:

SQL Server CE does not support parallel transactions.

I'm guessing your code is calling this check while you are inside a transaction. You may need to do your error checking separately from your transactions.

I used transactions for a while, then realized they weren't really needed for simple read/write operations to the database ...since I wasn't dealing with a banking institution. I'd bet it would be safe to remove your transaction statements, but that's just side comments.

I notice a lot of your code uses these specialized statements:

  • checkConnection(),
  • objCon.CreateCommand() and
  • isValidField.

Are these really helping you?

How many places are you calling these routines?

The error message makes it sound like you have already started another transaction somewhere else in your code before you called IsValidColumn.

Perhaps you could modify the code to check IsValidColumn early on (like when the program starts) so that you already know the column is valid before starting your transactions.

  • 1
    No, none of this code is helping me; it's a garbagetruckload of rancid spaghetti that I look upon with wide-eyed trembling. I just want to rush in to this hall of horrors, grab something, run out, and slam the door, hopefully never to return. If you look up "whack-a-mole" in the dictionary, it refers you to this project. What this code needs most is an exorcist. – B. Clay Shannon-B. Crow Raven Apr 02 '13 at 20:30
  • You've definitely got a following of people who don't don't like you, though! –  Apr 03 '13 at 00:35