I am building a WCF application, with multiple layers, and I am given this exception while trying to add new object to the DB. I will provide you code samples, please try to help me out here. Error is on the line scenaristBLL.Insert(s);
Here is my method within which I am trying to create a new Scenarist object in ScenaristController
class:
[HttpPost]
public ActionResult Create(ScenaristViewModel fvm, List<int> scenarijId)
{
if (fvm != null)
{
if (String.IsNullOrEmpty(fvm.Scenarist.NameSurname) == true)
{
TempData["ValidationErrorIme"] = "validation err1";
return RedirectToAction("Create");
}
if (fvm.Scenarist.DateOfBirth == null)
{
TempData["ValidationErrorGodina"] = "validation err2";
return RedirectToAction("Create");
}
Scenarist s = new Scenarist();
s.NameSurname = fvm.Scenarist.NameSurname;
s.DateOfBirth = fvm.Scenarist.DateOfBirth;
s.dateOfDeath = fvm.Scenarist.dateOfDeath;
scenaristBLL.Insert(s); // PROBLEM!
s = scenaristBLL.Fetch(s.Id);
foreach (int scenarij in scenarijId.Where(z => z != 0))
{
Scenarij scn = scenarijBLL.Fetch(scenarij);
if (scn == null)
{
return new HttpNotFoundResult("Nepostojeći scenaristId: " + scenarij.ToString());
}
scenarijBLL.AddToScenarist(scn, s);
}
}
return RedirectToAction("Index");
}
Also, here is the View class for it:
<table>
<tr>
<td>Name and surname:</td>
<td>@Html.TextBoxFor(modelItem => Model.Scenarist.NameSurname) @TempData["ValidationError1"]</td>
</tr>
<tr>
<td>Date of birth:</td>
<td>@Html.TextBoxFor(modelItem => Model.Scenarist.DateOfBirth) @TempData["ValidationError2"]</td>
</tr>
<tr>
<td>Datum of Death:</td>
<td>@Html.TextBoxFor(modelItem => Model.Scenarist.DateOfDeath) @TempData["ValidationError3"]</td>
</tr>
</table>
I think the DateOfBirth and DateOfDeath might be the problem, since those are in DateTime format, and I am not sure how to get a DateTime format from user input. Just a guess, really not sure.
Furtherly, here are other layers for it... BLL:
public void Insert(Scenarist scenarist)
{
string error;
if (scenarist.Validate(out error) == true)
{
string[] nameSurname = scenarist.NameSurname.Split(' ');
string name = nameSurname[0];
string surname = nameSurname[1];
dal.Insert(name, null, surname, scenarist.DateOfBirth, scenarist.DateOfDeath);
}
else
{
throw new ArgumentException("Validation error: " + error);
}
}
DAL:
public void Insert(string name, string middleName, string surname,
DateTime? dateOfB, DateTime? dateOfD)
{
using (RPPP111Entities ctx = new RPPP111Entities())
{
Scenarist scenarist = new Scenarist();
scenarist.Person.Name = name;
scenarist.Person.Middlename = middleName;
scenarist.Person.surname = surname;
scenarist.Person.DateOfB = dateOfB;
scenarist.Person.DateOfD = dateOfD;
ctx.Scenarist.Add(scenarist);
ctx.SaveChanges();
}
}