Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Updating Boolean field in MSSQL using LINQ and Entity Framework

Posted on 2011-02-28
6
Medium Priority
?
548 Views
Last Modified: 2012-05-11
Hello All,

I'm having a really frustrating time updating any fields in my database that are of type Boolean!!!

For instance I have:
ClassManagementEntities db = new ClassManagementEntities();
UserInformation user = new UserInformation();
user.Email = model.Email;
user.Roles = "Student";
user.Username = model.UserName;
user.UserInformationId = (Guid)memUser.ProviderUserKey;
user.IsApproved = true;
db.UserInformation.Add(user);
db.SaveChanges();

Open in new window


I then check in SQL Server Mangaement Studio and the value for IsApproved is set to 0. So frustrating. I've run out of ideas. I feel like this should be a simple task... does the type need to be bool vs Boolean?
0
Comment
Question by:LaBowski
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 2000 total points
ID: 35004171
No, it should be just as easy as you figured.  I have boolean columns (of tyep Boolean) in virtually every table, and it's a simple to update as any other, and just as you have.

Any chance you have triggers or code somewhere else in your model that might be resetting the value?
Is the property mapped properly?
0
 

Author Comment

by:LaBowski
ID: 35004220
no triggers I don't believe. I don't even know how to set tiggers :\

I'm using the entity framework (CTP 5). I define the UserInformation class as:
public class UserInformation
    {
        public string Username { get; set; }
        public Boolean IsApproved { get; set; }
        public string Email { get; set; }
        public Guid UserInformationId { get; set; }
        public string Roles { get; set; }
        public string SecretQuestion { get; set; }
        public string SecretAnswer { get; set; }
        public string ReturnUrl { get; set; }
        public int SemesterID { get; set; }
        public virtual Grade Grade { get; set; }
        public virtual Absent Absent { get; set; }
    }

Open in new window


I really appreciate your help!
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35004265
instead of assigning value true, did you check with 1?
ClassManagementEntities db = new ClassManagementEntities();
UserInformation user = new UserInformation();
user.Email = model.Email;
user.Roles = "Student";
user.Username = model.UserName;
user.UserInformationId = (Guid)memUser.ProviderUserKey;
user.IsApproved = 1;
db.UserInformation.Add(user);
db.SaveChanges();

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:LaBowski
ID: 35004292
Weird... so now it's working properly... weird... Problem now is that the public string[] Roles { get; set;} is not being added to the database... so I have:
public class UserInformation
    {
        public string Username { get; set; }
        public Boolean IsApproved { get; set; }
        public string Email { get; set; }
        public Guid UserInformationId { get; set; }
        public string[] Roles { get; set; }
        public string SecretQuestion { get; set; }
        public string SecretAnswer { get; set; }
        public string ReturnUrl { get; set; }
        public int SemesterID { get; set; }
        public virtual Grade Grade { get; set; }
        public virtual Absent Absent { get; set; }
    }

Open in new window


I think cause it referencing Roles as Web.Security.Roles... which is unfortunate because I want to query users who have the role student from this table... do I need a public virtual Roles Roles get set or???
0
 

Author Comment

by:LaBowski
ID: 35004311
To Clarify in:

UserInformation user = new UserInformation();
                    user.Email = model.Email;
                    user.Roles = new string[] { "Student" };
                    user.Username = model.UserName;
                    user.UserInformationId = (Guid)memUser.ProviderUserKey;
                    user.IsApproved = true;

                    ProfileInformation profile = ProfileInformation.GetProfile(model.UserName);
                    profile.SemesterID = currentSemesterID;
                    user.SemesterID = Int32.Parse(currentSemesterID);
                    db.UserInformation.Add(user);
                    db.SaveChanges();

Open in new window


The line:
user.Roles = new string[] { "Student" };

Open in new window


Is not represented in my database.... as in it's not being save AND it's not even a column of the Userinformation Table...
0
 

Author Comment

by:LaBowski
ID: 35004330
I guess since I have the boolean part working I could create a field isStudent and set it to true if they're a student... but then I'll have to update another method when the user is set to faculty or admin isStudent is set to false... which is doable, but is it best practice... I don't think so... sorry just thinking out loud...
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question