I have this slice of code that works sometimes and sometimes not. The query that is being executed has not been written by myself, but I am told that all I have to do is run it against the database to get the results I need. Here is my code:
try {
using (SqlDataReader dr = cmd.ExecuteReader()) {
while (dr.Read()) {
try {
list.Add(dr.GetString(3) + " " + dr.GetInt32(4).ToString() + " " + dr.GetString(5) + " " + dr.GetDecimal(8).ToString());
} catch (Exception e) {
list.Add("fail");
}
}
}
} catch (SqlException sqlException) {
Debug.WriteLine(sqlException.Message);
}
The error that I sometimes receive is that I cannot drop a table because I do not have permission or the table does not exist. Other times the query executes without any problems and I am able to retrieve and store my results in a list.
When I run the query in SQL Server 2008, sometimes I do indeed get errors but the results still show up and are what I expect. So my question is: How can I grab these results regardless of the error that seems to come up whenever it feels like?
Here is a tiny piece of the query that is causing me my troubles:
IF EXISTS (SELECT * from tempdb..sysobjects where name like '#TABLE%')
DROP #TABLE
There are many of these if statements in the query that i am running and it's unpredictable which one is going to cause an error. So what i have done for right now is surround the DROP #TABLE in a try-catch block so that at least i can still get retrieve the results in my silverlight program. I will get with my higher up and ask him why the query is returning these errors spontaneously..