Thursday, May 20, 2010

TableAdapter Wizard Query Parsing Quibble

I was working on a little project in Visual Studio 2008 using an SQL database. I went to add a TableAdapter to add some databound features on my form. I was having trouble with my query, but I had written similar queries in other TableAdapters and wanted to leave the wizard to have a peek. Seeking to save myself some typing, I left my query incomplete and I OK'ed my way through the wizard and ignored the parsing errors. I copied some code from another query and re-opened the wizard. The query worked like a charm now. The wizard didn't complain about anything - that is, until I hit the Finish button. I got an error message saying "There was an error parsing the query" followed by the token, line number and offset.

"Hey, what gives? My query is fine!" I was ready to slap a kid, Masahiro Chono-style.

But I figured it out.

For some reason, when you OK through the wiz on an incomplete or bad query, it sticks in the wizard's memory. Now, when you go back to the wiz and type in any query, it will still attempt to parse the original incomplete or bad query stuck in memory.

I was able to reproduce the error by leaving out the column name of the last line of my query, a GROUP BY statement. The token pointed at the BY keyword when I OK'ed through the wiz. This is understandable since you need to give a column name when using the GROUP BY statement. I then reopened the wiz and removed the GROUP BY statement, making it a healthy query. Guess what? The token pointed at the BY keyword still, even though there was no longer a "BY" in my query!

I worked around this by simply copying the query I wanted, deleting the erroneous TableAdapter, creating a new one, and pasting the query in during the wizard.

Lesson learned: Never OK through the TableAdapter wizard with an incomplete query!

No comments:

Post a Comment