Home All Groups Group Topic Archive Search About

Not returning boolean correctly

Author
4 Oct 2005 10:27 PM
tshad
I have class method that is passing back a boolean, but it always returns a
false;

The class is:
***************************************************************************
  public bool Delete()
  {
   int rowsAffected = 0;
   DbObject myDbObject = new DbObject();
   SqlParameter[] parameters = {
    new SqlParameter("@CompanyID",SqlDbType.Int),
    new SqlParameter("@RoleID",SqlDbType.VarChar,50)};

   if (companyID != 0)
    parameters[0].Value = companyID;
   else
    parameters[0].Value = DBNull.Value;
   parameters[1].Value = roleID;

   try
   {
    rowsAffected = myDbObject.RunProcedure("DeleteRole", parameters, ref
rowsAffected);
   }
   catch (SqlException sqle)
   {
     throw sqle;
   }
   status = rowsAffected;
   return (rowsAffected == 1);
  }
******************************************************************************

In my program if I do:

bTest = theRole.delete(12);

bTest will be 0 (false) whether or not theRole is 1 or 0 (which is the
number of records that were deleted).

I know that rowsAffected is 1, since theRole.status is 1.

Am I missing something?

Thanks,

Tom

Author
4 Oct 2005 10:35 PM
Bruce Wood
There is no substitute for firing up the Visual Studio debugger and
stepping through your code.

If Delete is returning false, then obviously rowsAffected is _not_ 1
when that return statement is executed. The only way to really see what
is going on is to step through your code line by line.

If the debugger is out of the question, then I fall back on
Console.WriteLine, or writing log lines to a file. Either way, things
are not what you think, so you need more information.
Are all your drivers up to date? click for free checkup

Author
4 Oct 2005 10:45 PM
tshad
"Bruce Wood" <brucew***@canada.com> wrote in message
news:1128465304.408747.121200@g49g2000cwa.googlegroups.com...
> There is no substitute for firing up the Visual Studio debugger and
> stepping through your code.
>

I agree.

But unless the return is destroying it or changing it - I would assume (and
I could be wrong here) that rowsAffected is equal to 1, since the next
statement after the call shows theRole.status = 1.

I actually ran the code twice and the 2nd time theRole.status = 0 (as I
would expect since I deleted the role the first time I ran this).  The
method returns 0(false) both times.

> If Delete is returning false, then obviously rowsAffected is _not_ 1
> when that return statement is executed. The only way to really see what
> is going on is to step through your code line by line.
>
> If the debugger is out of the question, then I fall back on
> Console.WriteLine, or writing log lines to a file. Either way, things
> are not what you think, so you need more information.

I agree.

But I am using a web page and my object was not built in VS.

I just build it by hand and compile it with the command line.  So not sure
how to get the debugger to see it.

Thanks,

Tom
Author
4 Oct 2005 10:51 PM
Bruce Wood
Throw in a stitch of code to open a text file, and then write some
debug output to the text file. Keep inserting debug output until you
trace the problem.

Sometimes the old, cheesy ways of debugging work best. :-)
Author
4 Oct 2005 11:31 PM
tshad
"Bruce Wood" <brucew***@canada.com> wrote in message
news:1128466289.668046.151820@f14g2000cwb.googlegroups.com...
> Throw in a stitch of code to open a text file, and then write some
> debug output to the text file. Keep inserting debug output until you
> trace the problem.
>
> Sometimes the old, cheesy ways of debugging work best. :-)

I agree.

This is in essence what I am doing with my trace.warn statement ( as I
explain in my other post - which you hadn't gotten yet because I just sent
it).  But that is still outside of my class.  So your suggestion would have
been my next step.

I hate it when the old Brain Fade kicks in. :)

Thanks,

Tom
Author
4 Oct 2005 10:44 PM
Jon Skeet [C# MVP]
tshad <tscheider***@ftsolutions.com> wrote:
> I have class method that is passing back a boolean, but it always returns a
> false;

<snip>

> In my program if I do:
>
> bTest = theRole.delete(12);
>
> bTest will be 0 (false) whether or not theRole is 1 or 0 (which is the
> number of records that were deleted).
>
> I know that rowsAffected is 1, since theRole.status is 1.
>
> Am I missing something?

It sounds very unlikely that it's actually failing to do the comparison
properly. I do note that you're calling theRole.delete(12) when your
method apparently doesn't take any parameters. That suggests a cut and
paste error, or that you're calling a different method. If it's a cut
and paste error, please post the *actual* code.


I suggest you try liberally sprinkling the code with debug output
(either Console.WriteLine or something similar, depending on the
situation) or execute the code within a debugger to see the value of
rowsAffected at different times.

By the way, your try/catch currently isn't doing anything useful - it's
just truncating the stack trace if an exception is thrown.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
4 Oct 2005 11:28 PM
tshad
Show quote Hide quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1dad1614f25648a98c867@msnews.microsoft.com...
> tshad <tscheider***@ftsolutions.com> wrote:
>> I have class method that is passing back a boolean, but it always returns
>> a
>> false;
>
> <snip>
>
>> In my program if I do:
>>
>> bTest = theRole.delete(12);
>>
>> bTest will be 0 (false) whether or not theRole is 1 or 0 (which is the
>> number of records that were deleted).
>>
>> I know that rowsAffected is 1, since theRole.status is 1.
>>
>> Am I missing something?
>
> It sounds very unlikely that it's actually failing to do the comparison
> properly. I do note that you're calling theRole.delete(12) when your
> method apparently doesn't take any parameters. That suggests a cut and
> paste error, or that you're calling a different method. If it's a cut
> and paste error, please post the *actual* code.
>
Sorry.

You're right.

I actually converted my vb.net code and actually did it incorrectly.

Here is the vb.net code.  The trace.warn is from my asp.net page.

Dim bTest as Boolean
Dim theRole = new Role(16)
bTest = theRole.Delete()
trace.warn("after delete status = " & theRole.status & "     bTest = " &
bTest)

I was just looking at it and found that the test was incorrect.  It is now
always coming back 0 for rowsAffected, which is why the result was false.

I had moved my debugging around and can't figure out why I was getting 1
(because I definately was).  It could have been that I had compiled the
class a couple of times and forgot to move the dll into the bin folder

What I did find was that the RunProcedure line was messing up my
rowsAffected variable.  It was, if you remember:

    rowsAffected = myDbObject.RunProcedure("DeleteRole", parameters, ref
rowsAffected);

I was passing the rowsAffected back in the variable from the parameter list
(ref).  But then I was using the rowsAffected again as the return value
(which in this case was being passed an error code - which was always 0 in
these cases).

So the return value was wiping out the rowsAffected value.  After I changed
it to:

    rtnValue = myDbObject.RunProcedure("DeleteRole", parameters, ref
rowsAffected);

It works fine.

>
> I suggest you try liberally sprinkling the code with debug output
> (either Console.WriteLine or something similar, depending on the
> situation) or execute the code within a debugger to see the value of
> rowsAffected at different times.

I am doing that with my trace.warn statement.

>
> By the way, your try/catch currently isn't doing anything useful - it's
> just truncating the stack trace if an exception is thrown.

You're right.

Actually, it will jump to my general Web Error page.

I haven't actually decided what I am going to do with it yet.  I am just
creating this Roles object and I copied much of this code from my other
methods.

The other method - the Add method,  where it does something has the catch
doing:

    if (sqle.Number == 2627)
     return -1;
    else
     throw sqle;

2627 being that a record exists - which is just saying that the email
entered already exists.

If I see a -1 on the return of the this method, I will give a message to the
user that the file exists.

Thanks,

Tom
Author
5 Oct 2005 7:24 AM
Scott Coonce
Maybe the problem is more simple:  check out your line of code:

rowsAffected = myDbObject.RunProcedure("DeleteRole", parameters, ref
rowsAffected);

First you pass the rowsAffected (int) as a ref parameter to the RunProcedure
method (i assume this is the value filled), but then, immediately on the
method return, you set the return value to rowsAffected.  so even if the
"ref rowsAffected" has the proper value of 1, the return value of
RunProcedure overwrites it.

It seems a bit redundant to me to pass the value by ref and also set it as
the return value, unless there is something I'm not thinking of this early
in the morning...

Scott


Show quoteHide quote
"tshad" <tscheider***@ftsolutions.com> wrote in message
news:eC2xZLTyFHA.908@tk2msftngp13.phx.gbl...
>I have class method that is passing back a boolean, but it always returns a
>false;
>
> The class is:
> ***************************************************************************
>  public bool Delete()
>  {
>   int rowsAffected = 0;
>   DbObject myDbObject = new DbObject();
>   SqlParameter[] parameters = {
>    new SqlParameter("@CompanyID",SqlDbType.Int),
>    new SqlParameter("@RoleID",SqlDbType.VarChar,50)};
>
>   if (companyID != 0)
>    parameters[0].Value = companyID;
>   else
>    parameters[0].Value = DBNull.Value;
>   parameters[1].Value = roleID;
>
>   try
>   {
>    rowsAffected = myDbObject.RunProcedure("DeleteRole", parameters, ref
> rowsAffected);
>   }
>   catch (SqlException sqle)
>   {
>     throw sqle;
>   }
>   status = rowsAffected;
>   return (rowsAffected == 1);
>  }
> ******************************************************************************
>
> In my program if I do:
>
> bTest = theRole.delete(12);
>
> bTest will be 0 (false) whether or not theRole is 1 or 0 (which is the
> number of records that were deleted).
>
> I know that rowsAffected is 1, since theRole.status is 1.
>
> Am I missing something?
>
> Thanks,
>
> Tom
>
Author
5 Oct 2005 3:29 PM
tshad
Show quote Hide quote
"Scott Coonce" <sdcoonce@gmail.HEY_YOU.com> wrote in message
news:uP58e3XyFHA.460@TK2MSFTNGP15.phx.gbl...
> Maybe the problem is more simple:  check out your line of code:
>
> rowsAffected = myDbObject.RunProcedure("DeleteRole", parameters, ref
> rowsAffected);
>
> First you pass the rowsAffected (int) as a ref parameter to the
> RunProcedure method (i assume this is the value filled), but then,
> immediately on the method return, you set the return value to
> rowsAffected.  so even if the "ref rowsAffected" has the proper value of
> 1, the return value of RunProcedure overwrites it.
>
> It seems a bit redundant to me to pass the value by ref and also set it as
> the return value, unless there is something I'm not thinking of this early
> in the morning...

You're right.

That was the problem I was running into.

Thanks,

Tom
Show quoteHide quote
>
> Scott
>
>
> "tshad" <tscheider***@ftsolutions.com> wrote in message
> news:eC2xZLTyFHA.908@tk2msftngp13.phx.gbl...
>>I have class method that is passing back a boolean, but it always returns
>>a false;
>>
>> The class is:
>> ***************************************************************************
>>  public bool Delete()
>>  {
>>   int rowsAffected = 0;
>>   DbObject myDbObject = new DbObject();
>>   SqlParameter[] parameters = {
>>    new SqlParameter("@CompanyID",SqlDbType.Int),
>>    new SqlParameter("@RoleID",SqlDbType.VarChar,50)};
>>
>>   if (companyID != 0)
>>    parameters[0].Value = companyID;
>>   else
>>    parameters[0].Value = DBNull.Value;
>>   parameters[1].Value = roleID;
>>
>>   try
>>   {
>>    rowsAffected = myDbObject.RunProcedure("DeleteRole", parameters, ref
>> rowsAffected);
>>   }
>>   catch (SqlException sqle)
>>   {
>>     throw sqle;
>>   }
>>   status = rowsAffected;
>>   return (rowsAffected == 1);
>>  }
>> ******************************************************************************
>>
>> In my program if I do:
>>
>> bTest = theRole.delete(12);
>>
>> bTest will be 0 (false) whether or not theRole is 1 or 0 (which is the
>> number of records that were deleted).
>>
>> I know that rowsAffected is 1, since theRole.status is 1.
>>
>> Am I missing something?
>>
>> Thanks,
>>
>> Tom
>>
>
>



Post Thread options