Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define the Bit datatype in a method parameter in C#?

I have a SQL table on a database which has a column in it with the Bit datatype. I'm trying to define a method in my C# application which takes two of the columns from the table and uses them as parameters.

Here is the code

public void Edit_NAA_ApplicationsFirm(int ApplicationId, string UniversityOffer, bit Firm)
    {
        //This line declares a variable in which we store the actual values from NAA_Applications based on the associating ID
        NAA_Applications AppFirm = Get_Applicant_Application(ApplicationId);

        //Here we tell the application that the values edited are equal to the values within the table
        AppFirm.Firm = Firm;

        //Any of these changes are then saved.
        _context.SaveChanges();

    }

The only issue is the the program keeps trying to convert bit to BitConverter. When I change it to bit it has issues accepting it as a datatype.

It should be worth noting I'm building the application in an ASP.Net Framework solution.

Could anyone tell me what it is I'm doing wrong? Am I just referring to the datatype wrong?

like image 285
Henry Green Avatar asked Dec 18 '22 01:12

Henry Green


2 Answers

Looks like you're using Entity Framework.

It'll use a .NET boolean to represent a T-SQL bit. That would be a sensible way to do it for any other data access method as well. boolean true will convert to 1 in the bit field, and false to 0.

In fact it's even documented, more than once, that this is the correct .NET CLR type to use. See https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/sql-clr-type-mapping , https://msdn.microsoft.com/en-us/library/cc716729(v=vs.100).aspx and probably others.

So in your case bool Firm would be appropriate.

like image 190
ADyson Avatar answered Feb 22 '23 02:02

ADyson


The correct datatype for bit in c# is boolean. so

public void Edit_NAA_ApplicationsFirm(int ApplicationId, string UniversityOffer, bool Firm)
like image 30
Jens Stragier Avatar answered Feb 22 '23 01:02

Jens Stragier