将数据插入数据库(MVC) [英] Insert data into database(MVC)
问题描述
我正在尝试将数据插入到我的数据库的Appointment表中。我做了项目的注册部分工作得很好。有2个表,患者和约会。登录后患者可以预约。患者号码是这样的< br $>
MyUser.PatientNo = Guid.NewGuid()。GetHashCode();
对于预约日期和描述来自textbox.And我想将PatientNo从Patient table插入Appointment table.For我看起来已经完成但是当我选择日期和写描述但我得到错误这一行app.PatientNo = patient.PatientNo;
DentAppSys.dll中出现System.NullReferenceException类型的异常,但未在用户代码中处理
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Make(Models.AppModel用户)
{
if (会话[ UserEmail]!= null )
{
使用( var db = new MaindbModelDataContext())
{
var patient = db.Patients.FirstOrDefault(u = > u.Email ==(字符串)会话[ 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, 约会);
}
}
else
{
return RedirectToAction( 索引, 用户);
}
}
}
}
这是注册部分运行良好
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 =哈希;
MyUser.PatientNo = Guid.NewGuid()。GetHashCode();
db.Patients.InsertOnSubmit(MyUser);
db.SubmitChanges();
会话[ UserEmail] = User.RegisterModel.Email;
return RedirectToAction( Index, Patient,User.RegisterModel);
} else {
ModelState.AddModelError( , 有一位用户使用此电子邮件。请输入另一封电子邮件! 跨度>);
return View();
}}}
else {ModelState.AddModelError( , 数据不正确!!!); }
else
{ if (ModelState.IsValid&& IsValid (User.LoginModel.Email,User.LoginModel.Password))
{
var TempUser = new Models.RegisterModel();
会话[ UserEmail] = User.LoginModel.Email;
使用( 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( < span class =code-string>索引, 患者,TempUser) ;
}
else
{
ModelState.AddModelError( , 查看您的电子邮箱或密码,然后再试一次!!!);
}
}
return 查看();
I am trying to insert datas to Appointment table of my database.I did registeration part of my project which works well.there are 2 tables ,Patient and Appointment.After Login patients can make an appointment.Patient number comes like this
MyUser.PatientNo = Guid.NewGuid().GetHashCode();
For appointment date and description comes from textbox.And i want to insert PatientNo from Patient table to Appointment table.For me it looks done but when i choose date and write description but i got error on this line " app.PatientNo = patient.PatientNo;"
An exception of type 'System.NullReferenceException' occurred in DentAppSys.dll but was not handled in user code
[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");
}
}
}
}
and this is registration part which is working well
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();
这篇关于将数据插入数据库(MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!