TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
First Last
NA
648
72.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.
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:
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();
}
}
Reply
Answers (
6
)
How to convert Word docx to either Html or PDF from MVC application
How to open pdf file new tab in browser in ASP.NET C#