March 09 2014

ASP.net display MSSQL DateTime on razor.

Tagged Under : ,

asp.net
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.
asp.net

But, when validate the DateTime with ASP.net it will return an error.
asp.net

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.

February 27 2014

ASP.net how to set model default value

Tagged Under : ,

asp.net
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; }
}

February 24 2014

ASP.net MSSQL connection test

Tagged Under : ,

asp.net
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:

February 13 2014

ASP.net MVC Model add a new field

Tagged Under : ,

asp.net
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.

February 13 2014

Cannot attach the file as database

Tagged Under : ,

asp.net
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.

January 21 2014

SQL Server Date Formats

Tagged Under : ,

mssql
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.