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.

March 04 2014

ASP.net summary of return Action Result type

Tagged Under :

asp.net
There are different type of results that are available in ASP.net MVC. When create new controller, they will come with actions by default. The Empty controller includes an Index action with a return value of type ActionResult. ActionResul class is the base class for all action results.

There are different ActionResult types explained below.

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 21 2014

How to enable details error information in IIS and ASP.NET

Tagged Under :

asp.net
By default, IIS and ASP.NET hide detailed error information to prevent revealing sensitive information about your web application.
Sometimes we need to see error details (think shared hosting) to debug our coding.

To display the error message. Add these entries to your web.config file to disable generic errors:

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.

December 30 2013

ASP.net add class to App_Code folder

Tagged Under :

asp.net
In ASP MVC4 Appliction I can add a cshtml file in app_code folder like below so that I can use my custom helper method in another cshtml.

App_Code/MyHelpers.cshtml
@helper LabelExtensions(string input) {
    "<pre>@input</pre>"
}

Using the Helper in a Page.
<p>This is some paragraph text.</p>
@MyHelpers.LabelExtensions("My Test Label.")
<p>This is some paragraph text.</p>

Output:
<p>This is some paragraph text.</p>
<pre>My Test Label.</pre>
<p>This is some paragraph text.</p>

But if the helper is classes (cs file) then you are not allow call the helper function like above. Below example show you how to use it in a Page.

December 08 2013

ASP.net MVC 4 Razor WebGrid auto increase row number

Tagged Under :

asp.net
Below example showing you how to add the row number in WebGrid.

You just need add the following script inside the grid columns.
grid.Column(header: "No.", 
                format: item => item.WebGrid.Rows.IndexOf(item) + 1 + Math.Round(Convert.ToDouble(grid.TotalRowCount / grid.PageCount) / grid.RowsPerPage) * grid.RowsPerPage * grid.PageIndex)

December 06 2013

ASP.net MVC Razor if else statement display output

Tagged Under :

asp.net
Not all content container blocks start with a tag element tag, though, and there are scenarios where the Razor parser cannot implicitly detect a content block.

Razor addresses this by enabling you to explicitly indicate the beginning of a line of content by using the @: character sequence within a code block. The @: sequence indicates that the line of content that follows should be treated as a content block:
@if (someCondition) {
  @: Some content to display...
}

For more practical example, the below snippet demonstrates how we could output a "Not Stock" message to our product if numbers of product is zero.
@model.product_name
@if (model.unit_stock == 0) {
  @: Not Stock
}

Because I am not wrapping the "Not Stock" message in an HTML tag element, Razor can’t implicitly determine that the content within the @if block is the start of a content block. We are using the @: character sequence to explicitly indicate that this line within our code block should be treated as content.