MSSQL store DateTime format is standard. But sometimes we need change the format to display it to user, let them more easy read and understand which Datetime format was set in our Application.
If use ASP.net retrieve the MSSQL DateTime Data to you application, it will display like below format.
But, when validate the DateTime with ASP.net it will return an error.
Because the DateTime format set in ASP.net was “06-03-2014 12:00:00 AM”. But now we just need to display Date only not need time.
Because I want to show default value in my create page. and in the create page code was used Html.TextBox and model to generate the input box.
Since it was used model to generate the code and we also can use model to set the default value as well.
Below example code are showing how set the default value in model:
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime CreateDate { get; set; }
}
I am making a little code to test the functionality server with MSSQL Database connection. The test code was develop with asp.net C#.
If the script successful connect with MSSQL Database it will display message like below:
TESTS COMPLETED SUCCESSFULLY!.
Else, it will display
TESTS FAILED!
Server script:
After add extra field inside the Model. I get an error message when I run the application.
The error message as below:
The model backing the 'MyContext' context has changed since the database was created.
It was because the Model was different with Database field. And now, you need rebuild the Database structure again.
The solution is:
1. Open Package Manager Console from Tools -> Libraray Package Manager -> Package Manager Console
2. PM> Enable-Migrations -ContextTypeName <TheDatabaseName>
3. Set AutomaticMigrationsEnabled=true, from Migrations folder of Configuration file
4. PM> Add-Migration InitialCreate
5. PM> Update-Database
Now try run the application again. it will work fine and updated to the new Model.
Or you can use
another way to solve the problem.
Because I did delete the DB file and now I get an error message when I run the application. I expected it was the DB file was deleted by me and it didn’t help me rebuild the DB.
To solve this issue, we need stop and delete the Database with follwing step:
1. Open Package Manager Console from Tools -> Libraray Package Manager -> Package Manager Console
2. Stop local DB with command: PM > sqllocaldb.exe stop v11.0
3. and then delete it PM > sqllocaldb.exe delete v11.0
Now try run the application again. it will work fine again.
How to format
datetime value or column into a specific date format. Is the most frequently asked question by the newbie. Below table is summary of the different date formats that come from SQL Server with the
CONVERT function.
Please note that the output of these date formats are
VARCHAR data type, not DATETIME data type. With this in mind, any date comparisons performed after the DATETIME value has been formatted are using the VARCHAR value of the date and time and not its original DATETIME value.
Posted by
chevrons in
MSSQL