Solved

Updating Boolean field in MSSQL using LINQ and Entity Framework

Posted on 2011-02-28
6
535 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
  • 4
6 Comments
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
Comment Utility
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
Comment Utility
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 40

Expert Comment

by:Sharath
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:LaBowski
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now