Strange exception.

G

Guest

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 is
a valid string constant SQL statement. I tried to pass valid parameters too
and got the same exception?
 
G

Guest

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
 
G

Guest

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 said:
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 said:
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 is
a valid string constant SQL statement. I tried to pass valid parameters too
and got the same exception?
 
G

Guest

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 not
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 said:
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 said:
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 said:
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 is
a valid string constant SQL statement. I tried to pass valid parameters too
and got the same exception?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top