Saturday, March 24, 2012

Value cannot be null. Parameter name: dataReader

Hello everybody!

Do anybody meet exception with following message: "Value cannot be null. Parameter name: dataReader"?

Exception appears when Dataset object is filled with data from server. Here is code example:

public static DataSet ExecDataSet(SqlCommand sqlCommand)
{
DataSet set;

try
{
SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
set = new DataSet();
adapter.Fill(set);
}
catch (Exception exception)
{
throw new SqlCommandException(exception, sqlCommand);
}
return set;
}

Here is exception stack trace:

at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)

Exception is thrown only from time to time in unpredictable manner. Most of times code above works without errors and retrieves data from server, but sometimes fails.

Sql queries that are used in sqlCommand object are correct.

Data base is located on MS SQL Server 2005.

Thank you,

Alexei

I feel your pain, intermittent exceptions can be so hard to debug.

Your code looks good, however the only variable I see that is outside the scope of this code is the connection for sqlCommand. Perhaps sometimes it is not open for some reason. Also, try instead to catch for specific exceptions. It seems like you are assuming the exception is going to be a command exception, but it could be anything.

Just my $0.02


Based on error message it looks like your SQL String has parameters, but code does not pass them. You need to post the code that creates your sqlCommand

My search on Google for your error message only returned one result, this post. That is usually a bad sign. My suggestion to you is for debugging purposes put a breakpoint on

throw new SqlCommandException(exception, sqlCommand);

and then when it stops on that line explore the exception variable, It may give you a better idea of the problem.

HTH!


Seems like u have used Parameterized Query in your sqlcommand

Can u please provide the CommandText of the SqlCommand.


Is this only being called from one thread?

No comments:

Post a Comment