1

I create a database in sqlserver2017 and tried to use entity framework6 to connect the database. it's connected and I've no any errors when compiling, but I'm having some error messages like : Database.CurrentTransaction at runtime when want to create a record in the database.

screenshot of the problem

/*inside the Controller*/

        [HttpGet]
        public ActionResult AddStudent()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddStudent(Student new_stud)
        {                        
            if(ModelState.IsValid)
            {
                BLStudent bls = new BLStudent();
                if(bls.Add(new_stud))
                {
                    ViewBag.message = "It's recorded.";
                    ViewBag.color = "aqua";
                }
                else
                {
                    ViewBag.message = "It's not recorded.";
                    ViewBag.color = "red";
                }
            }
            else
            {
                ViewBag.message = "PLS enter the information correctly.";
                ViewBag.color = "red";
            }

            return View();
        }
/*inside the BLStudent Class*/

        UniversityEntities db = new UniversityEntities();

        public bool Add(Student new_stud)
        {
            try
            {
                db.Students.Add(new_stud);
                return Convert.ToBoolean(db.SaveChanges());
            }
            catch (Exception)
            {

                return false;
            }
        }

Cath Exceptions :

System.Data.Entity.Infrastructure.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.'

Inner Exceptions:

1 - UpdateException: An error occurred while updating the entries. See the inner exception for details.

2 - SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.

how can fix this?

MPERSIA
  • 187
  • 1
  • 15

1 Answers1

1

As per the full error message

" SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated."

It seems you have a type conversion issue in your database.

I recommend changing DateTime columns to DateTime2 in your database.

Apparently, you use DateTime type in your schema, which is not recommended, and has conversion issues (probably for the default value in C# which is year 0001 etc.., that cannot be converted to a DateTime)

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • how can I send datetime to sql instead of datetime2? – MPERSIA Aug 16 '19 at 12:33
  • 1
    @IMPERSIA some links to check : https://stackoverflow.com/questions/1334143/datetime2-vs-datetime-in-sql-server, https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/date-and-time-data, https://stackoverflow.com/questions/3498817/difference-between-datetime-and-datetime2 – Pac0 Aug 16 '19 at 13:06
  • 1
    Basically, remember that in C#, the default value for a `DateTime` is something like '0001-01-01 00:00:00' (like the ddefault value for an `int` is 0). This value is not possible for SQL Server `datetime` (values start at year ~1753). So ensure you have proper dates in the valid range of SQLServer, and it should work. I believe it's some kind of school assignment, using outdated exercices? Keep in mind that I don't see any good reason to use SQL datetime anymore since 2008, instead of datetime2, apart from working on a legacy database. – Pac0 Aug 16 '19 at 13:09