Insertion error in the database, how to solve this case?

1

I'm trying to insert a form with various data, some booleans, radiobuttons, datetime etc. I need to know if everything is correct, I'm catching them via FormColleciont .

Controller:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(FormCollection form)
        {
            bool convertedSex,convertedSmoke,convertedPacemaker,convertedDonor = false;
            bool.TryParse(form["Sex"], out convertedSex);
            bool.TryParse(form["IsSmoker"], out convertedSmoke);
            bool.TryParse(form["IsPacemaker"], out convertedPacemaker);
            bool.TryParse(form["Isdonor"], out convertedDonor);

            DateTime dateConverted = new DateTime();
            DateTime.TryParse(form["borndate"], out dateConverted);

            var pSimpleUser = new SimpleUserr()
            {
                IdSimpleUser = 0,
                Name = form["Name"],
                BornDate = dateConverted,
                Sex = convertedSex,
                BloodType  = form ["BloodType"],
                Weight = Convert.ToDouble(form["Weight"]),
                Height = Convert.ToDouble(form ["Height"]),
                IsDonor =convertedDonor,
                IsPaceMaker = convertedPacemaker,
                IsSmoker = convertedSmoke,
                City = form["City"],
                State = form ["State"],
                HaveDesease =form ["HaveDesease"],
                UseMedication = form ["UseMedication"],
                IsFractured = form ["IsFractured"],
                Email = form ["Email"],
                PasswordAccess = form ["PasswordAccess"],
                HealthPlan = form ["HealthPlan"]
            };

Assembly of the command for procedure:

using (_context)
                {
                    SqlCommand cmd = new SqlCommand("SaveSimpleUser");
                    cmd.Parameters.AddWithValue("@idSimpleUser", pSimpleUser.IdSimpleUser);
                    cmd.Parameters.AddWithValue("@name", pSimpleUser.Name);
                    cmd.Parameters.AddWithValue("@data", pSimpleUser.BornDate);
                    cmd.Parameters.AddWithValue("@sex", pSimpleUser.Sex);
                    cmd.Parameters.AddWithValue("@bloodtype", pSimpleUser.BloodType);
                    cmd.Parameters.AddWithValue("@weight", pSimpleUser.Weight);
                    cmd.Parameters.AddWithValue("@height", pSimpleUser.Height);
                    cmd.Parameters.AddWithValue("@isDonor", pSimpleUser.IsDonor);
                    cmd.Parameters.AddWithValue("@isPaceMaker", pSimpleUser.IsPaceMaker);
                    cmd.Parameters.AddWithValue("@isSmoker", pSimpleUser.IsSmoker);
                    cmd.Parameters.AddWithValue("@city", pSimpleUser.City);
                    cmd.Parameters.AddWithValue("@state", pSimpleUser.State);
                    cmd.Parameters.AddWithValue("@haveDesease", pSimpleUser.HaveDesease);
                    cmd.Parameters.AddWithValue("@useMedication", pSimpleUser.UseMedication);
                    cmd.Parameters.AddWithValue("@isFractured", pSimpleUser.IsFractured);
                    cmd.Parameters.AddWithValue("@email", pSimpleUser.Email);
                    cmd.Parameters.AddWithValue("@password", pSimpleUser.PasswordAccess);
                    cmd.Parameters.AddWithValue("@healthPlan", pSimpleUser.HealthPlan);
                    cmd.CommandType = CommandType.StoredProcedure;
                    this._context.ExecuteProcedure(cmd);
                    return true;
                }

S. Procedure:

ALTER PROCEDURE [dbo].[SaveSimpleUser]
    (
    @idSimpleUser int,
    @name varchar(50),
    @data date,
    @sex bit,
    @bloodtype bit,
    @weight float,
    @height float,
    @isDonor bit,
    @isPaceMaker bit,
    @isSmoker bit,
    @city varchar(50),
    @state varchar(50),
    @haveDesease varchar(50),
    @useMedication varchar(50),
    @isFractured varchar(50),
    @email varchar(50),
    @password varchar(20),
    @healthPlan varchar(50)

    )
AS
BEGIN

    if(@idSimpleUser >0)
        update SimpleUser set Name = @name, BornDate = @data, SexType = @sex, BloodType = @bloodtype, 
        Weight = @weight,Height = @height,IsDonor = IsDonor,IsPacemaker = @isPaceMaker,IsSmoker  = @isSmoker,
        City = @city,State=@state,HaveDesease=@haveDesease,UseMedication = @useMedication,
        IsFractured = @isFractured,Email = @email,PasswordAccess = @password,HealthPlan = @healthPlan where IdSimpleUser = @idSimpleUser
    else
        insert into SimpleUser values (@name,@data,@sex,@bloodtype,@weight,@height,@isDonor,@isPaceMaker,@isSmoker,@city,@state,@haveDesease,
        @useMedication,@isFractured,@email,@password,@healthPlan)



END

Error:

Error converting data type nvarchar to bit.
    
asked by anonymous 17.05.2014 / 04:59

1 answer

3

The bloodtype field should be changed to a character type, such as varchar.

Notice the following line:

BloodType  = form ["BloodType"],

And the type line in the database:

@bloodtype bit,

The value of the field comes directly from the form. Values of possible blood types are: A, B, AB, O, regardless of blood factor.

In the database, the blootype field has the bit type. The bit type only allows values 0, 1 and NULL.

    
17.05.2014 / 07:08