Object reference not set to an instance of an object.

W

weird0

I get the this error when i perform the operation. Any reason why? I
am trying to execute a stored procedure. Then, set its parameters in
getCustomer_Account().Call them in an aspx page;


private static Object ExecuteSP(Object[] argArray)
{
SqlCommand cmd = new SqlCommand();
Object returnValue;
int prmCount = (int)argArray[1];
cmd.CommandText = (string)argArray[2];
cmd.CommandType = CommandType.StoredProcedure;

for (int j = 0, i = 3; j < prmCount; i += 4, j++)
{
SqlParameter myPrm = new
SqlParameter((string)argArray,

(SqlDbType)argArray[i + 1],
(int)argArray[i
+ 2]);
myPrm.Value = argArray[i + 3];
cmd.Parameters.Add(myPrm);
}

SqlConnection sqlConnection1 = new
SqlConnection(connectionSring);
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

if ((int)argArray[0] == 0)
returnValue = cmd.ExecuteNonQuery();
else if ((int)argArray[0] == 1)
returnValue = cmd.ExecuteScalar();
else
returnValue =
cmd.ExecuteReader(CommandBehavior.CloseConnection);

sqlConnection1.Close();

return returnValue;
}

public static bool getCustomerAccount_Status(string userName)
{
Object[] prms = new Object[7];

prms[0]=1;
prms[1] = 1;
prms[2] = "GET_ACCOUNT_STATUS";
prms[3]="@username";
prms[4]=SqlDbType.VarChar;
prms[5] =16;
prms[6]=userName;

Object returnValue = ExecuteSP(prms);
bool myvar=(bool)returnValue; // Line of Error
return myvar;
}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

weird0 said:
I get the this error when i perform the operation. Any reason why? I
am trying to execute a stored procedure. Then, set its parameters in
getCustomer_Account().Call them in an aspx page;
snip ---8<

The result returned from the stored procedure is empty, so ExecuteScalar
returns a null reference.
 
W

weird0

snip ---8<

The result returned from the stored procedure is empty, so ExecuteScalar
returns a null reference.

The boolean variable myvar is false which i checked in debugging which
is perfectly right and that is what i want the function to return.
But, the error stucks up the execution.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

weird0 said:
The boolean variable myvar is false which i checked in debugging which
is perfectly right and that is what i want the function to return.
But, the error stucks up the execution.

There is only one possible cause for that error on that line, and that
is that the returnValue contains a null reference. You are sending in an
array where the first item is one into the ExecuteSP method, which will
make it use the ExecuteScalar method to run the stored procedure. The
only time that the ExecuteScalar method returns a null reference is when
the result of the query is empty.
 

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

Top