c# adapter.fill dataset not working with stored procedure even though I know the parameters are returning data?

StackOverflow https://stackoverflow.com/questions/15067342

Question

I have the strangest issue. The following works most of the time, however "now and again" it fails to return data, even though I have put a breakpoint for the adapter.fill statement, checked the params and executed them via SQL Management Studio which definitely returns the data :)

So this is a standard c# SQL client call:

SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.Connection = connection;

The connection is definitely being opened ok, and used because it is also being used via another call.. and actually subsequent calls work too.. however, again in my case the following "sometimes" fails:

command.CommandText = "myprocname";
command.Parameters.AddWithValue("@param1", param1value);
command.Parameters.AddWithValue("@param2", param2value);
command.Parameters.AddWithValue("@param3", param3value);
command.Parameters.AddWithValue("@param4", param4value);

and then to fill the dataset:

DataSet ds = new DataSet();
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
    adapter.Fill(ds);
}
command.Dispose();

So really nothing strange going on, and the above works most of the time.. on the occasion it didn't return what I expected, and put a breakpoint in just before the adapter.fill statement, checked the commandtext and each of the parameters/values being passed.. opened up sql management studio, ran the proc with those params, and sure enough the expected data was returned.

The proc is very simple, and again most of the time returned the data...

I cannot fault the proc in anyway, however this is driving my crazy as I have checked connection timeouts, timings of execution and so on.. I guess the craziest thing is that the proc name, and params are all correct (no extra params being passed by accident etc).. and running manually, it returns the data.. but via the fill statement it seems to NOT (but only occasionally!).

Any help with this madness much appreciated!!

Was it helpful?

Solution

RESOLVED: Okay well.. the strangest error, one that has really thrown me.

I didn't mention at the time of posting this question, that I was using RDLC/Reportviewer control to show the data within a set of dashboard charts.. i.e. you could then select a chart segment (a pie chart slice for example) to drilldown on that data.. the chart data was "pre-grouped" i.e. the procs for those charts didn't contain the detail to make it quick to show the dashboard..

Anyway, I digress, because I had some "bad data".. well data that had null dates, and the short story version was the grouping in the reportview chart was somehow causing the selected data to be incorrect "sometimes".. as in the params passed were/looked ok, but on occasion they were not..

So the end result is (which I had planned anyway) was to add checks into my procs for "date is not null" etc.. however it didn't harm to wrap the command object etc with "using" statements too! so many thanks for all the responses!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top