I have the following code which gets the input from a form and calls a stored procedure to store it in a database.
[HttpPost]
public ActionResult Index(GuestMessage model)
{
if (ModelState.IsValid)
{
using(IDbConnection conn = DatabaseAcess.OpenConnection())
{
const string storedProcedure = "dbo.InsertMessage";
conn.Execute(storedProcedure, new GuestMessage { Name = model.Name, Email = model.Email, Message = model.Message }, null, null, CommandType.StoredProcedure);
return View("ThankYou");
}
}
return View();
}
There are three fields, Name
, E-mail
and Message
that have to be filled in by the user. In the database I store the data that the user enters, from these three fields, and the time of the entry using a stored function called dbo.Insert Message.
BEGIN
BEGIN TRAN
SET NOCOUNT ON;
DECLARE @GuestID int;
INSERT INTO Guest(Name, Email) VALUES (@Name, @Email);
SELECT @GuestID = scope_identity();
INSERT INTO Message (EntryDate, GuestID, Message, Status)
VALUES (GETDATE(), @GuestID, @Message, 2);
COMMIT TRAN
END
Notice that I do not pass the entry date and time as an input parameter to the stored procedure since there is no need for that. I use the built-in function GETDATE()
in SQL Server to determine the time a user has entered a message.
However, whenever I try to enter a message I get the following error:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00AM and 12/31/9999 11:59:59PM