尝试更新数据库条目,但添加了新条目

时间:2016-07-21 08:47:38

标签: c# sql-server database

所以这是短暂的: 我的系统有两种类型的用户:公司和学生。 公司发布工作,学生申请(新的条目被添加到应用程序数据库表),公司选择应用程序并向学生提供工作,网站将他带到合同页面,公司可以在哪里看到合同看起来和学生的CPR(SSN)会去哪里。他或她填写公司的CVR(公司SSN),他们的名字和姓氏和点击提交。这将在合同数据库表中创建一个新条目。到目前为止一切都很好。

这是Contract.cs模型类:

namespace Leepio.Models
{
    public class Contract { 
        public int ContractId { get; set; }
        public string StudentId { get; set; }
        public string CompanyId { get; set; }
        public string CVR { get; set; }
        public int ApplicationId { get; set; }
        public bool IsSigned { get; set; }
        public string StudentSigningDate { get; set; }
        public string CompanySigningDate { get; set; }
        public string RepFirstName { get; set; }
        public string RepLastName { get; set; }
        public virtual ApplicationUser Student { get; set; }
        public virtual ApplicationUser Company { get; set; }
        public virtual Application Application { get; set; }


    }

此时所有内容都被填充,除了两个字段:IsSigned字段(当学生接受并提交时应该变为true)和StudentSigningDate,这只是学生接受当天字符串形式的日期。 现在,学生收到一封电子邮件,他或她转到链接,在那里他们可以看到合同预览和输入CPR的文本框,然后是提交按钮。

现在问题出现在这里。

这是提交按钮的方法:

 public ActionResult SendMailAsAStudent(string studentId, string companyId, int applicationId, string companyCVR, string studentCPR)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var student = manager.FindById(studentId);
            var company = manager.FindById(companyId);
            var application = db.Applications.Find(applicationId);
            var contractobj = db.Contracts.Find(applicationId);
            Project projectobj = db.Projects.Find(application.ProjectId);


            var myMessage = new SendGridMessage();
            myMessage.From = new MailAddress("info@leepio.dk");
            myMessage.AddTo(student.Email);
            myMessage.AddTo(company.Email);
            myMessage.Subject ="The contract for " + projectobj.Title + " is signed!";
            myMessage.Html = "Here is the final contract";


            LocalReport localReport = new LocalReport();

            localReport.ReportPath = Server.MapPath("~/Contracts/Contract.rdlc");
            ReportParameter cvrParameter = new ReportParameter("cvrParameter", companyCVR);
            ReportParameter companyNameParameter = new ReportParameter("companyNameParameter", company.CompanyName);
            ReportParameter companyAddressParameter = new ReportParameter("companyAddressParameter", company.Address);
            ReportParameter companyCityParameter = new ReportParameter("companyCityParameter", company.City);
            ReportParameter studentCityParameter = new ReportParameter("studentCityParameter", student.City);
            ReportParameter studentNameParameter = new ReportParameter("studentNameParameter", student.FirstName+" "+student.LastName);
            ReportParameter studentAddressParameter = new ReportParameter("studentAddressParameter", student.Address);
            ReportParameter studentZipCodeParameter = new ReportParameter("studentZipCodeParameter", student.ZipCode);
            ReportParameter jobStartDateParameter = new ReportParameter("jobStartDateParameter", projectobj.StartDate);
            ReportParameter jobEndDateParameter = new ReportParameter("jobEndDateParameter", projectobj.EndDate);
            ReportParameter jobDescriptionParameter = new ReportParameter("jobDescriptionParameter", projectobj.Description);
            ReportParameter jobHoursPerWeekParameter = new ReportParameter("jobHoursPerWeekParameter", projectobj.HoursPerWeek.ToString());
            ReportParameter jobHourlyRateParameter = new ReportParameter("jobHourlyRateParameter", projectobj.HourlyRate.ToString());
            ReportParameter cprParameter = new ReportParameter("cprParameter", studentCPR);
            ReportParameter studentSignDateParameter = new ReportParameter("studentSignDateParameter", DateTime.Today.ToString("dd/MM/yyyy"));
            ReportParameter companySignDateParameter = new ReportParameter("companySignDateParameter", contractobj.CompanySigningDate);
            ReportParameter companyRepNameParameter = new ReportParameter("companyRepNameParameter", contractobj.RepFirstName +" "+contractobj.RepLastName);
            ReportParameter projectWorkFromParameter = new ReportParameter("projectWorkFromParameter", projectobj.WorkFrom);


            localReport.SetParameters(new ReportParameter[] { cprParameter });
            localReport.SetParameters(new ReportParameter[] { cvrParameter });
            localReport.SetParameters(new ReportParameter[] { companyNameParameter });
            localReport.SetParameters(new ReportParameter[] { companyAddressParameter });
            localReport.SetParameters(new ReportParameter[] { studentNameParameter });
            localReport.SetParameters(new ReportParameter[] { studentAddressParameter });
            localReport.SetParameters(new ReportParameter[] { studentZipCodeParameter });
            localReport.SetParameters(new ReportParameter[] { jobStartDateParameter });
            localReport.SetParameters(new ReportParameter[] { jobEndDateParameter });
            localReport.SetParameters(new ReportParameter[] { jobDescriptionParameter });
            localReport.SetParameters(new ReportParameter[] { jobHoursPerWeekParameter });
            localReport.SetParameters(new ReportParameter[] { jobHourlyRateParameter });
            localReport.SetParameters(new ReportParameter[] { studentSignDateParameter });
            localReport.SetParameters(new ReportParameter[] { companySignDateParameter });
            localReport.SetParameters(new ReportParameter[] { companyRepNameParameter });
            localReport.SetParameters(new ReportParameter[] { projectWorkFromParameter });
            localReport.SetParameters(new ReportParameter[] { companyCityParameter });
            localReport.SetParameters(new ReportParameter[] { studentCityParameter });

            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension = "pdf";
            Warning[] warnings;
            string[] streams;
            var renderedBytes = localReport.Render(reportType, "", out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
            Response.AddHeader("content-disposition", "attatchment; filename=Contract." + fileNameExtension);

           MemoryStream stream = new MemoryStream(renderedBytes);
           myMessage.AddAttachment(stream, "Contract.pdf");


            var apiKey = "removed for this post";
            var transportWeb = new Web(apiKey);
            transportWeb.DeliverAsync(myMessage);

            Contract contract = db.Contracts.Find(contractobj.ContractId);

            contract.IsSigned = true;
            contract.StudentSigningDate = DateTime.Today.ToString("dd/MM/yyyy");



            //db.Contracts.Add(contract);
            db.SaveChanges();
            return RedirectToAction("Index", "Projects");
        }

然后发生这种情况:

enter image description here

该条目按我的意愿添加:isSigned为false且学生签名日期尚未填写。 填写后,它会在数据库中创建另一个条目,公司的签名日期,名字和姓氏为false!

是什么原因引起的?我已经注释掉了db.Contracs.Add(合同),我找到了applicationId所需的合同,因为那将是唯一的标识符。我究竟做错了什么?我希望能够添加学生登录日期并将IsSigned bool更改为true。 请注意,学生CPR未存储在数据库中,“20”是应用程序的ID

1 个答案:

答案 0 :(得分:0)

所以我找到了解决方案:

我现在将contractId作为参数传递而不是之前的所有参数:

public ActionResult SendMailAsAStudent(int contractId, string studentCPR)

然后我就这样检索合同:

Contract contract = db.Contracts.Find(contractId);

更改了参数的所有检索过程:

 ReportParameter cvrParameter = new ReportParameter("cvrParameter", contract.CVR);
            ReportParameter companyNameParameter = new ReportParameter("companyNameParameter", contract.Company.CompanyName);
            ReportParameter companyAddressParameter = new ReportParameter("companyAddressParameter", contract.Company.Address);
            ReportParameter companyCityParameter = new ReportParameter("companyCityParameter", contract.Company.City);
            ReportParameter studentCityParameter = new ReportParameter("studentCityParameter", contract.Student.City);
            ReportParameter studentNameParameter = new ReportParameter("studentNameParameter", contract.Student.FirstName+" "+ contract.Student.LastName);
            ReportParameter studentAddressParameter = new ReportParameter("studentAddressParameter", contract.Student.Address);
            ReportParameter studentZipCodeParameter = new ReportParameter("studentZipCodeParameter", contract.Student.ZipCode);
            ReportParameter jobStartDateParameter = new ReportParameter("jobStartDateParameter", contract.Application.Project.StartDate);
            ReportParameter jobEndDateParameter = new ReportParameter("jobEndDateParameter", contract.Application.Project.EndDate);
            ReportParameter jobDescriptionParameter = new ReportParameter("jobDescriptionParameter", contract.Application.Project.Description);
            ReportParameter jobHoursPerWeekParameter = new ReportParameter("jobHoursPerWeekParameter", contract.Application.Project.HoursPerWeek.ToString());
            ReportParameter jobHourlyRateParameter = new ReportParameter("jobHourlyRateParameter", contract.Application.Project.HourlyRate.ToString());
            ReportParameter cprParameter = new ReportParameter("cprParameter", studentCPR);
            ReportParameter studentSignDateParameter = new ReportParameter("studentSignDateParameter", DateTime.Today.ToString("dd/MM/yyyy"));
            ReportParameter companySignDateParameter = new ReportParameter("companySignDateParameter", contract.CompanySigningDate);
            ReportParameter companyRepNameParameter = new ReportParameter("companyRepNameParameter", contract.RepFirstName +" "+contract.RepLastName);
            ReportParameter projectWorkFromParameter = new ReportParameter("projectWorkFromParameter", contract.Application.Project.WorkFrom);

这也解决了我在其他帖子中的参数的另一个问题。 An attempt was made to set a report parameter 'studentSignDateParameter' that is not defined in this report