将数据插入数据库(MVC)

时间:2014-05-01 07:57:30

标签: asp.net-mvc database insert record

我正在尝试将数据插入到我的数据库的Appointment表中。我做了我的项目的注册部分,运作良好。有2个表,患者和预约。登录后患者可以预约。患者号码就像这样

 MyUser.PatientNo = Guid.NewGuid().GetHashCode();

对于约会日期和描述来自文本框。我想将PatientNo从Patient表插入Appointment表。对我来说它看起来已经完成,但是当我选择日期并写出描述但我在这一行app.PatientNo = patient.PatientNo;

时出错
  

DentAppSys.dll中出现“System.NullReferenceException”类型的异常,但未在用户代码中处理

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Make(Models.AppModel User)
        {
            if (Session["UserEmail"] != null)
            {
                using (var db = new MaindbModelDataContext())
                {


                    var patient = db.Patients.FirstOrDefault(u => u.Email == (String)Session["UserEmail"]);
                    var app = new Appointment();
                    app.Date = User.Date;
                    app.Description = User.Description;
                    app.Status = "true";
                    app.PatientNo = patient.PatientNo;
                    db.Appointments.InsertOnSubmit(app);
                    db.SubmitChanges();
                    return RedirectToAction("Make", "Appointment");
                }

            }
            else
            {
                return RedirectToAction("Index", "User");
            }
        }

    }
}

这是注册部分,运作良好

public ActionResult RegAndLogin(Models.RegAndLog User)
        {
            if (User.RegisterModel != null)
            {
                if (ModelState.IsValid)
                {
                    using (var db = new MaindbModelDataContext())
                    {
                        var Person = db.Patients.FirstOrDefault(u => u.Email == User.RegisterModel.Email);
                        if (Person == null)
                        {
                            string Hash = BCrypt.Net.BCrypt.HashPassword(User.RegisterModel.Password);
                            var MyUser = new Patient();
                            MyUser.Name = User.RegisterModel.Firstname;
                            MyUser.Surname = User.RegisterModel.Lastname;
                            MyUser.Birthday = User.RegisterModel.Birthday;
                            MyUser.Email = User.RegisterModel.Email;
                            MyUser.Password = Hash;
                            MyUser.PatientNo = Guid.NewGuid().GetHashCode();
                            db.Patients.InsertOnSubmit(MyUser);
                            db.SubmitChanges();

                            Session["UserEmail"] = User.RegisterModel.Email;
                            return RedirectToAction("Index", "Patient", User.RegisterModel);
                        }
                        else
                        {
                            ModelState.AddModelError("", "There is a user with this Email. Please enter another Email !!!");
                            return View();
                        }

                    }
                }
                else
                {
                    ModelState.AddModelError("", "Data is incorrect !!!");
                }

            }
            else
            {
                if (ModelState.IsValid && IsValid(User.LoginModel.Email, User.LoginModel.Password))
                {
                    var TempUser = new Models.RegisterModel();
                    Session["UserEmail"] = User.LoginModel.Email;
                    using (var db = new MaindbModelDataContext())
                    {
                        var person = db.Patients.FirstOrDefault(u => u.Email == User.LoginModel.Email);
                        TempUser.Firstname = person.Name;
                        TempUser.Lastname = person.Surname;
                        //TempUser.RegisterModel.Birthday = (DateTime)person.BirthDate;
                        TempUser.Email = person.Email;


                    }
                    return RedirectToAction("Index", "Patient", TempUser);


                }
                else
                {
                    ModelState.AddModelError("", "Check your E-mail or Password then try again !!!");
                }
            }
            return View();

1 个答案:

答案 0 :(得分:0)

如果您在行

上获得空例外
app.PatientNo = patient.PatientNo;

这将是因为应用程序或患者在执行时都为空。我怀疑是耐心的。

检查在线

处是否找到了正确的患者
var patient = db.Patients.FirstOrDefault(u => u.Email == (String)Session["UserEmail"]);

如果未找到,患者将无效。