Thursday, March 22, 2012

Dataset not filled if sql-query generates error

Hi,

I've a dataset which is filled using a stored procedure in either MSSQL2005 or 2000.

The procedure may return sql-errors (f.ex 208 if a table is not found), but the procedure will anyway return data which shall populate the dataset, independent on 208 errors.

The procedure works fine if running it from f.ex. SqlMgtStudio, and the dataset is filled if the procedure triggers no errors.

However, if the procedure triggers an error like 208, then I get an exception in the application and the dataset is not populated with the data returned by the procedure.

Is there a way of telling the DataSet/SqlDataAdapter to fill the dataset even if the sql-code genererates som errors?

Regards, Guttorm

Perhaps you could wrap your statements to avoid errors. For instance, if you have a table not found error you could wrap the line that generates it with if object_id('<table name>') is not null or some other sanity check. This would just silently work through the problem if there are errors though, you could add an else of course to handle when tables don't exist to create them and such.

Hope this helps,

John (MSFT)

|||

Thank you for your response.

Your outlined workaround might have been an option if not for the fact that I do not have much control of the sql-code.

The sqlcode is indirectly built by the user (and then saved in database) and verifying it would require me to first parse it ( to find tables, views) then name resolution etc, a rather time and resource consuming process.

My current workaround is to divide the task of filling the dataset into two tasks,

1) execute a command (using SqlCommand.ExecuteNonQuery) to populate a temp-table with the result

2) fill dataset (DataSet.Fill) by selecting the result from the temp-table

This works fine but makes to code a bit more complicated and requires an intermediate store in the database.

Guttorm

sql