Solved

Updating Boolean field in MSSQL using LINQ and Entity Framework

Posted on 2011-02-28
6
544 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 500 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

636 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