Saturday, March 24, 2012

Strange exception.

I am using the PetShop example's ExecuteReader function:
public static SqlDataReader ExecuteReader(string connectionString,
CommandType cmdType, string cmdText, params SqlParameter[] commandParameters
)
{...}
However, I alwasy get the following exception:
Outter exception:
The type initializer for threw an exception.
Inner exception:
Object reference not set to an instance of an object.
One of the calling is:
SqlDataReader reader =
SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction,
CommandType.Text, SQL_SELECT_EVENTS_BETWEEN_DATES, null)
Which SqlHelper.ConnectionStringLocalTransaction has a valid connection
string, CommandType.Text is built-in type, SQL_SELECT_EVENTS_BETWEEN_DATES i
s
a valid string constant SQL statement. I tried to pass valid parameters too
and got the same exception?Matt,
Judging on the error description it looks like static constructor or the
class or initializer of one of it's static variables causes the error and no
t
that specific method - calling this method causes the class type to
instantiate in memory.
HTH
"Matt" wrote:

> I am using the PetShop example's ExecuteReader function:
> public static SqlDataReader ExecuteReader(string connectionString,
> CommandType cmdType, string cmdText, params SqlParameter[] commandParamete
rs)
> {...}
> However, I alwasy get the following exception:
> Outter exception:
> The type initializer for threw an exception.
> Inner exception:
> Object reference not set to an instance of an object.
> One of the calling is:
> SqlDataReader reader =
> SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction,
> CommandType.Text, SQL_SELECT_EVENTS_BETWEEN_DATES, null)
> Which SqlHelper.ConnectionStringLocalTransaction has a valid connection
> string, CommandType.Text is built-in type, SQL_SELECT_EVENTS_BETWEEN_DATES
is
> a valid string constant SQL statement. I tried to pass valid parameters to
o
> and got the same exception?
>
I use the file SQLHelper.cs the way exactly as the PetShop 4.
SQLHelper.cs:
namespace PetShop.DBUtility {
public abstract class SqlHelper {
.....
public static SqlDataReader ExecuteReader(string connectionString,
CommandType cmdType, string cmdText, params SqlParameter[] commandParameters
)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
try {
PrepareCommand(cmd, conn, null, cmdType, cmdText,
commandParameters);
SqlDataReader rdr =
cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch {
conn.Close();
throw;
}
}
....
}
"Sergey Poberezovskiy" wrote:
> Matt,
> Judging on the error description it looks like static constructor or the
> class or initializer of one of it's static variables causes the error and
not
> that specific method - calling this method causes the class type to
> instantiate in memory.
> HTH
> "Matt" wrote:
>
Matt,
I do not beleive that you can step into this method while debugging, do you?
As I mentioned in my previous post, I has this type of exception when the
class static constructor throws an error - so seeing the method code does no
t
help much :-(
If you can post the class constructor (in case one is present) and all
static initializers, such as following:
private static object _myCustom = new Custom();
Alternatively, if you can post here the link to download the petshot
application, I will try to investigate it further - otherwise there is not
enough info for me to make any conclusions...
I would prefer the first option though - less time for me :-)
"Matt" wrote:
> I use the file SQLHelper.cs the way exactly as the PetShop 4.
> SQLHelper.cs:
> namespace PetShop.DBUtility {
> public abstract class SqlHelper {
> .....
> public static SqlDataReader ExecuteReader(string connectionString,
> CommandType cmdType, string cmdText, params SqlParameter[] commandParamete
rs)
> {
> SqlCommand cmd = new SqlCommand();
> SqlConnection conn = new SqlConnection(connectionString);
> try {
> PrepareCommand(cmd, conn, null, cmdType, cmdText,
> commandParameters);
> SqlDataReader rdr =
> cmd.ExecuteReader(CommandBehavior.CloseConnection);
> cmd.Parameters.Clear();
> return rdr;
> }
> catch {
> conn.Close();
> throw;
> }
> }
> ....
> }
> "Sergey Poberezovskiy" wrote:
>

0 comments:

Post a Comment