I have a row returned from SQL Server and an integer column value that is not converting properly to boolean in C#.

I want to convert an integer 1 to boolean true. But it does not do that. It converts the 1 to false or thinks it is 0 and converts to false.

```
blogPublishedByBlogId.LikeDisabled = Convert.ToBoolean(getblogPublishedByBlogIdReader["LikeDisabled"]);

```

Here is the row returned from the stored procedure via SSMS:

[![enter image description here](https://i.stack.imgur.com/RSSMn.png)](https://i.stack.imgur.com/RSSMn.png)

The `LikeDisabled` column is a an integer and has a value = 1, however it gets converted to a value of 0 in C#. Why? A simple thing like this should work. It does not make sense.

Here is the code that shows it after the stored procedure is called and taking the data and putting it into a model.

I put in 2 lines of 'test code' to see the integer value. It is returned by the stored procedure as a integer value = 1, yet after the convert function- Convert.ToInt32, I get a 0. See below.

[![enter image description here](https://i.stack.imgur.com/1IAXS.png)](https://i.stack.imgur.com/1IAXS.png)

Here is the fully populated model a few lines down. The booleans are both false when the `LikeDisabled` one should be = true.

[![enter image description here](https://i.stack.imgur.com/N106m.png)](https://i.stack.imgur.com/N106m.png)

Here is the stored procedure. It gets the data needed from the database table and also sets the `LikeDisabled` and `DisLikeDisabled` based upon `@LikeOrDislikeIndicator = 'L'` which is currently and an 'L'.

```
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GetBlogPublishedByBlogId]
    @a_UserName   varchar(250) = NULL,
    @a_IpAddress  varchar(250) = NULL,
    @a_BlogId     int = NULL,
    @a_UserId     int = NULL
AS 
BEGIN
    DECLARE @RowCount                  int,
            @ReturnCode                int,
            @CurrentDateTime           datetime,  
            @Count                     int,
            @UserType                  varchar(25) = '',
            @LikeOrDislikeIndicator    char(1) = '',   -- 'L' for like, 'D' for dislike.
            @BlogId                    int = 0,   
            @BlogTitle                 varchar(250) = '',
            @BlogContent               varchar(max) = '',
            @LikeCount                 int = 0,  
            @DisLikeCount              int = 0,  
            @ModifiedDateTime          datetime = NULL,
            @CreatedDateTime           datetime = NULL,
            @LikeDisabled              int = 0, 
            @DisLikeDisabled           int = 0,
            @Message                   varchar(max),
            @ApiMessageOut             varchar(max),
            @ApiAccessSwitchOut        bit

SELECT @CurrentDateTime = GETDATE()

DECLARE @ErrorLine      AS INT;
DECLARE @ErrorMessage       AS VARCHAR(2048);
DECLARE @ErrorNumber        AS INT; 
DECLARE @ErrorSeverity      AS INT; 
DECLARE @ErrorState     AS INT; 
DECLARE @DatabaseName       AS VARCHAR(255);
DECLARE @ServerName     AS VARCHAR(255);
DECLARE @ErrorDescription   AS VARCHAR(MAX);
DECLARE @CRLF           AS VARCHAR(2);

BEGIN TRY

   SET NOCOUNT ON;

   IF ( ( @a_UserName  = '' OR @a_UserName  IS NULL ) OR ( @a_IpAddress = '' OR @a_IpAddress IS NULL ) 
   OR ( @a_BlogId IS NULL ) OR ( @a_UserId IS NULL ) )
      BEGIN
         SELECT @Message = 'Warning - invalid parameters. They cannot be null or empty.'    

         IF ( @a_UserName = '' OR @a_UserName IS NULL )
             BEGIN
                SET @a_UserName = 'No "user name" parameter provided.'
             END

         IF ( @a_IpAddress = '' OR @a_IpAddress IS NULL )
             BEGIN
                SET @a_IpAddress = 'No "ip address" parameter provided.'
             END

        RAISERROR (@Message, 16, 1)
      END
   ELSE
      BEGIN
           -- Do the API security check. If this user is valid, you can continue with further processing.
           SELECT @ReturnCode = -1
           EXECUTE @ReturnCode = dbo.GetApiAccess
                     @CurrentDateTime,
                     @a_UserName,
                     @a_IpAddress,
                     @a_ApiAccessSwitchFromGet = @ApiAccessSwitchOut OUTPUT,
                     @a_ApiMessageFromGet = @ApiMessageOut OUTPUT

           IF @ReturnCode = -1
                BEGIN 
                   RAISERROR ('Critical Error - procedure GetBlogPublishedByBlogId failed during execute of procedure GetApiAccess', 16, 1 )
                END

           -- Web api access was granted. 
           IF @ApiAccessSwitchOut = 1
               BEGIN
                   SELECT @BlogId = Blogid
                      ,@BlogTitle = BlogTitle
                      ,@BlogContent = BlogContent
                      ,@LikeCount = LikeCount
                      ,@DisLikeCount = DisLikeCount
                      ,@ModifiedDateTime = ModifiedDateTime 
                                  ,@CreatedDateTime = CreatedDateTime       
                   FROM dbo.Blog
                   WHERE ( BlogId = @a_BlogId AND ActiveSwitch = 1 )

                   SELECT @ReturnCode = @@ERROR,
                                  @RowCount = @@ROWCOUNT

                   IF @ReturnCode <> 0
                       BEGIN 
                          SELECT @Message = 'Critical Error - procedure GetBlogPublishedByBlogId during the 1st select.'
                          RAISERROR (@Message, 16, 1)
                       END  

                    IF @RowCount = 0
                        BEGIN 
                               SELECT 2 as Status
                              ,0 as Blogid
                              ,'' as BlogTitle
                              ,'' as BlogContent
                              ,0 as LikeCount
                              ,0 as DisLikeCount
                              ,NULL as ModifiedDateTime 
                                              ,NULL as CreatedDateTime
                              ,@LikeDisabled as LikeDisabled
                                      ,@DisLikeDisabled as DisLikeDisabled
                        END 
                    ELSE
                        BEGIN 
                        SELECT @LikeOrDislikeIndicator = LikeOrDislikeIndicator
                        FROM dbo.[UserBlogPreference]
                        WHERE ( BlogId = @a_BlogId AND UserId = @a_UserId )

                        SELECT @ReturnCode = @@ERROR,
                               @RowCount = @@ROWCOUNT

                            IF @ReturnCode <> 0
                               BEGIN 
                                SELECT @Message = 'Critical Error - procedure GetBlogPublishedByBlogId during the 2nd select.'
                                RAISERROR (@Message, 16, 1)
                               END  

                            IF ( @RowCount = 0 )
                                BEGIN
                                    SELECT @LikeDisabled = 0 
                                                           ,@DisLikeDisabled = 0                                
                                END
                            ELSE
                                BEGIN
                                IF @LikeOrDislikeIndicator = 'L'
                                   BEGIN
                                    SELECT @LikeDisabled = 1 
                                                               ,@DisLikeDisabled = 0 
                                   END
                                ELSE
                                   BEGIN
                                      SELECT @LikeDisabled = 0 
                                                             ,@DisLikeDisabled = 1 
                                   END
                                END

                            -- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      
                            -- Return data. 
                            --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                         SELECT 1 as Status
                                     ,@Blogid as Blogid
                                     ,@BlogTitle as BlogTitle
                                     ,@BlogContent as BlogContent
                                     ,@LikeCount as LikeCount
                                     ,@DisLikeCount as DisLikeCount
                                     ,@ModifiedDateTime as ModifiedDateTime
                                                             ,@CreatedDateTime as CreatedDateTime
                                     ,@LikeDisabled as LikeDisabled
                                                     ,@DisLikeDisabled as DisLikeDisabled
                        END 
               END
           ELSE
               BEGIN

                  RAISERROR (@ApiMessageOut, 16, 1 )
              END
      END

   RETURN 0

END TRY

BEGIN CATCH
    ---- code not shown.       
END CATCH
END

```

---

Here's the UserBlogPreference table that has the indicator used to determine the setting of the LikeDisabled and DisLikeDisabled. It is set as an 'L'.:

[![enter image description here](https://i.stack.imgur.com/IyHlU.png)](https://i.stack.imgur.com/IyHlU.png)

Here is the model class:

```
using System;
using System.ComponentModel.DataAnnotations;

namespace GbngWebApi2.Models
{
    public class BlogPublishedByBlogId
    {
        public int BlogId { get; set; }        
        public string BlogTitle { get; set; }
        public string BlogContent { get; set; }
        public int LikeCount { get; set; }
        public int DisLikeCount { get; set; }
        public DateTime ModifiedDateTime { get; set; }
        public DateTime CreatedDateTime { get; set; }
        public bool LikeDisabled { get; set; }
        public bool DisLikeDisabled { get; set; }
    }
}

```

Here is the call to the stored procedure and it is the screen shots above:

```
public BlogPublishedByBlogIdResults GetBlogPublishedByBlogId(string userName, string ipAddress, int blogId, int userId)
{
    BlogPublishedByBlogIdResults blogPublishedByBlogIdResults = new BlogPublishedByBlogIdResults();

    SqlDataReader getblogPublishedByBlogIdReader = null;

    try
    {
        dbFunc.OpenDB();

        SqlCommand cmd = new SqlCommand("dbo.GetBlogPublishedByBlogId", dbFunc.objConn);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@a_UserName", userName);
        cmd.Parameters.AddWithValue("@a_IpAddress", ipAddress);
        cmd.Parameters.AddWithValue("@a_BlogId", blogId);
        cmd.Parameters.AddWithValue("@a_UserId", userId);

        getblogPublishedByBlogIdReader = cmd.ExecuteReader();

        // There will be only 1 entry.
        while (getblogPublishedByBlogIdReader.Read())
        {
            BlogPublishedByBlogId blogPublishedByBlogId = new BlogPublishedByBlogId();

            blogPublishedByBlogIdResults.Status = Convert.ToInt32(getblogPublishedByBlogIdReader["Status"]);
            blogPublishedByBlogId.BlogId = Convert.ToInt32(getblogPublishedByBlogIdReader["BlogId"]);
            blogPublishedByBlogId.BlogTitle = getblogPublishedByBlogIdReader["BlogTitle"].ToString();
            blogPublishedByBlogId.BlogContent = getblogPublishedByBlogIdReader["BlogContent"].ToString();
            blogPublishedByBlogId.LikeCount = Convert.ToInt32(getblogPublishedByBlogIdReader["LikeCount"]);
            blogPublishedByBlogId.DisLikeCount = Convert.ToInt32(getblogPublishedByBlogIdReader["DisLikeCount"]);
            blogPublishedByBlogId.ModifiedDateTime = Convert.ToDateTime(getblogPublishedByBlogIdReader["ModifiedDateTime"]);
            blogPublishedByBlogId.CreatedDateTime = Convert.ToDateTime(getblogPublishedByBlogIdReader["CreatedDateTime"]);

            // Test code to see what the value is before trying to convert to boolean below.
            int likeDisabled = Convert.ToInt32(getblogPublishedByBlogIdReader["LikeDisabled"]);
            int DislikeDisabled = Convert.ToInt32(getblogPublishedByBlogIdReader["DisLikeDisabled"]);

            blogPublishedByBlogId.LikeDisabled = Convert.ToBoolean(getblogPublishedByBlogIdReader["LikeDisabled"]);
            blogPublishedByBlogId.DisLikeDisabled = Convert.ToBoolean(getblogPublishedByBlogIdReader["DisLikeDisabled"]);

            blogPublishedByBlogIdResults.BlogPublishedByBlogId = blogPublishedByBlogId;
        }

        return blogPublishedByBlogIdResults;
    }
    catch (SqlException sqlex)
    {                
        throw sqlex;
    }
    catch (Exception ex)
    {
    }
    finally
    {
        if (getblogPublishedByBlogIdReader != null)
        {
            getblogPublishedByBlogIdReader.Close();
        }

        dbFunc.CloseDB();
    }
}

```

---

Screen shot using Steve's code (same result - the integer 1 in the LikeDisabled returned from the stored procedure is still being converted to a value of 0):

[![enter image description here](https://i.stack.imgur.com/hpES2.png)](https://i.stack.imgur.com/hpES2.png)

---

Screen shot using Steve's code (same result - the integer 1 in the LikeDisabled returned from the stored procedure is still being converted to a value of 0). Also the stored procedure run in SSMS with debugging selects.

[![enter image description here](https://i.stack.imgur.com/jLfIy.png)](https://i.stack.imgur.com/jLfIy.png)

[![enter image description here](https://i.stack.imgur.com/fu8Cy.png)](https://i.stack.imgur.com/fu8Cy.png)