1
I am trying to insert a form with various data, some booleans, radiobuttons, datetime etc. I need to know if everything is correct, I am picking them up 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"]
};
Mounting of the command for Design:
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.
I think this might interest you: http://answall.com/questions/16089/diferencas-entre-parse-vs-tryparse/16203#16203
– Maniero