First Last

First Last

  • 992
  • 648
  • 67.4k

Value returned from SQL Server stored procedure not converting

Aug 28 2020 3:15 PM
I have a row returned from SQL Server and an integer field value that is not converting properly to boolean in C#.
 
I want to convert an integer 1 to boolean true. But it does not. It converts the 1 to false or thinks it is 0 and converts to false.
  1. blogPublishedByBlogId.LikeDisabled = Convert.ToBoolean(getblogPublishedByBlogIdReader["LikeDisabled"]);  
Here is the row returned from the stored procedure:
 
 
The `LikeDisabled` column is a 1, however it gets converted to a 0 in C#. Why? A simple thing like this should work. It does not make sense. The integer column LikeCount works fine.
 
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 set of code to see the integer value, so I convert to integer which is returned by the store procedure as 1, yet I get a 0.
 
 
 
Here is the fully populated model a few lines down. The booleans are both false when the `LikeDisabled` one should be = true.
 
 
 
Here is the model class: 
  1. using System;  
  2. using System.ComponentModel.DataAnnotations;  
  3.   
  4. namespace GbngWebApi2.Models  
  5. {  
  6.     public class BlogPublishedByBlogId  
  7.     {  
  8.         public int BlogId { getset; }          
  9.         public string BlogTitle { getset; }  
  10.         public string BlogContent { getset; }  
  11.         public int LikeCount { getset; }  
  12.         public int DisLikeCount { getset; }  
  13.         public DateTime ModifiedDateTime { getset; }  
  14.         public DateTime CreatedDateTime { getset; }  
  15.         public bool LikeDisabled { getset; }  
  16.         public bool DisLikeDisabled { getset; }  
  17.     }  
  18. }  
Here is the call to the stored procedure and it is the screen shots above:
  1. public BlogPublishedByBlogIdResults GetBlogPublishedByBlogId(string userName, string ipAddress, int blogId, int userId)  
  2. {  
  3.     BlogPublishedByBlogIdResults blogPublishedByBlogIdResults = new BlogPublishedByBlogIdResults();  
  4.   
  5.     SqlDataReader getblogPublishedByBlogIdReader = null;  
  6.   
  7.     try  
  8.     {  
  9.         dbFunc.OpenDB();  
  10.   
  11.         SqlCommand cmd = new SqlCommand("dbo.GetBlogPublishedByBlogId", dbFunc.objConn);  
  12.         cmd.CommandType = CommandType.StoredProcedure;  
  13.   
  14.         cmd.Parameters.Clear();  
  15.         cmd.Parameters.AddWithValue("@a_UserName", userName);  
  16.         cmd.Parameters.AddWithValue("@a_IpAddress", ipAddress);  
  17.         cmd.Parameters.AddWithValue("@a_BlogId", blogId);  
  18.         cmd.Parameters.AddWithValue("@a_UserId", userId);  
  19.   
  20.         getblogPublishedByBlogIdReader = cmd.ExecuteReader();  
  21.   
  22.         // There will be only 1 entry.  
  23.         while (getblogPublishedByBlogIdReader.Read())  
  24.         {  
  25.             BlogPublishedByBlogId blogPublishedByBlogId = new BlogPublishedByBlogId();  
  26.   
  27.             blogPublishedByBlogIdResults.Status = Convert.ToInt32(getblogPublishedByBlogIdReader["Status"]);
  28.             blogPublishedByBlogId.BlogId = Convert.ToInt32(getblogPublishedByBlogIdReader["BlogId"]);
  29.             blogPublishedByBlogId.BlogTitle = getblogPublishedByBlogIdReader["BlogTitle"].ToString();
  30.             blogPublishedByBlogId.BlogContent = getblogPublishedByBlogIdReader["BlogContent"].ToString();
  31.                 blogPublishedByBlogId.LikeCount = Convert.ToInt32(getblogPublishedByBlogIdReader["LikeCount"]);  
  32.                 blogPublishedByBlogId.DisLikeCount = Convert.ToInt32(getblogPublishedByBlogIdReader["DisLikeCount"]);  
  33.                 blogPublishedByBlogId.ModifiedDateTime = Convert.ToDateTime(getblogPublishedByBlogIdReader["ModifiedDateTime"]);  
  34.                 blogPublishedByBlogId.CreatedDateTime = Convert.ToDateTime(getblogPublishedByBlogIdReader["CreatedDateTime"]);  
  35.   
  36.                 // Test code to see what the value is before trying to convert to boolean below.  
  37.                 int likeDisabled = Convert.ToInt32(getblogPublishedByBlogIdReader["LikeDisabled"]);  
  38.                 int DislikeDisabled = Convert.ToInt32(getblogPublishedByBlogIdReader["DisLikeDisabled"]);  
  39.   
  40.                 blogPublishedByBlogId.LikeDisabled = Convert.ToBoolean(getblogPublishedByBlogIdReader["LikeDisabled"]);  
  41.                 blogPublishedByBlogId.DisLikeDisabled = Convert.ToBoolean(getblogPublishedByBlogIdReader["DisLikeDisabled"]);  
  42.   
  43.                 blogPublishedByBlogIdResults.BlogPublishedByBlogId = blogPublishedByBlogId;  
  44.             }  
  45.   
  46.             return blogPublishedByBlogIdResults;  
  47.         }  
  48.         catch (SqlException sqlex)  
  49.         {                  
  50.             throw sqlex;  
  51.         }  
  52.         catch (Exception ex)  
  53.         {  
  54.         }  
  55.         finally  
  56.         {  
  57.             if (getblogPublishedByBlogIdReader != null)  
  58.             {  
  59.                 getblogPublishedByBlogIdReader.Close();  
  60.             }  
  61.   
  62.             dbFunc.CloseDB();  
  63.         }  
  64.     } 

Answers (6)