Wednesday, September 24, 2014

ASP.NET MVC Razor Unrequired property keeps getting data-val-required attribute

If you have the problem that your text field validation gets generated althoug there is no Required attribute set on property, use the next code in the Application_Start to force the compiler to validate only if there is a required attribute on the property:

// Whit this line of code you will remove data-val-required DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

// With these two lines you will remove data-val-number and data-val ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new DataAnnotationsModelValidatorProvider());


Thursday, September 11, 2014

ASP.NET MVC Razor Override "id" and “name” attribute with TextBoxFor

Use the code below as example to override the id or/and name attributes with TextBoFor, something really important to keep in mind is that "id" and "Name" is case sensitive.
    
   @Html.TextBoxFor(x => x.Data, new { Name = Model.Name + "_Custom", id = Model.ID + "_Custom" })


Thursday, August 7, 2014

How to link to a root controller from area controller in MVC

Sometimes you need to call a root controller from inside of an area, usually when you are inside of an Admin console for example.
The process is really simple, all you have to do is specify the area as blank in the RenderAction or the RedirectToAction functions.



        <% Html.RenderAction("Action", "Controller", new { area = "" }); %>


        public ActionResult redirect()
        {
            return RedirectToAction("Action", "Controller", new { area = "" });

            return RedirectToAction("Action", "Controller", new { area = "", id = 5 });

        }


Wednesday, August 6, 2014

IIS An application with this virtual path already exist

If you get the the error "An application with this virtual path already exist": 


when creating an application in IIS is because an application was previously created pointing to that path, some people get confuse because they don't see the application in the connections panel, to remove the virtual path follow the next steps in IIS.

Open IIS Manager

In the Connections pane, expand the Sites node, then click to select the site in which the application is running



In the Actions pane, click View Applications




On the Applications , select the application you want to remove, then click Remove link in the Actions pane.



Click Yes to confirm that you want to remove the application.
Now you are going to be able to create your application!

Get the size of the tables in SQL

With the next query you can get the size of all the tables in a database.
  
--
-- This query will return the information of the tables
--

SELECT 
    t.NAME AS table_name,
    s.Name AS [schema_name],
    p.rows AS row_count,
    SUM(a.total_pages) * 8 AS space_KB, 
    SUM(a.used_pages) * 8 AS used_space_KB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS unused_space_KB
FROM 
    sys.tables t
    INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
    INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
    INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
    LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME NOT LIKE 'dt%' AND 
 t.is_ms_shipped = 0   AND 
 i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    t.table_name DESC

Thursday, July 3, 2014

MVC razor decode / encode HTML

Razor has the ability to automatically encoded html.
In order to avoid encoding use the HTML helper: @Html.Raw(text).



@{ var br = "
";} This is the line number 1 @Html.Raw(br) This is the line number 2 @Html.Raw(br) This is the line number 3 @Html.Raw(br)

Thursday, June 26, 2014

SQL Date Format Examples

Date Format T-SQL Code Output
Mon DD YYYY
HH:MIAM (or PM)
SELECT CONVERT(VARCHAR(20), GETDATE(), 100) Feb 23 2014 3:00PM
MM/DD/YY SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] 02/23/14
MM/DD/YYYY SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY] 02/23/2014
YY.MM.DD SELECT CONVERT(VARCHAR(8), GETDATE(), 2) AS [YY.MM.DD] 14.02.23
YYYY.MM.DD SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD] 2014.02.23
DD/MM/YY SELECT CONVERT(VARCHAR(8), GETDATE(), 3) AS [DD/MM/YY] 23/02/14
DD/MM/YYYY SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] 23/02/2014
DD.MM.YY SELECT CONVERT(VARCHAR(8), GETDATE(), 4) AS [DD.MM.YY] 23.02.14
DD.MM.YYYY SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY] 23.02.2014
DD-MM-YY SELECT CONVERT(VARCHAR(8), GETDATE(), 5) AS [DD-MM-YY] 23-02-14
DD-MM-YYYY SELECT CONVERT(VARCHAR(10), GETDATE(), 105) AS [DD-MM-YYYY] 23-02-2014
DD Mon YY SELECT CONVERT(VARCHAR(9), GETDATE(), 6) AS [DD MON YY] 23 Feb 14
DD Mon YYYY SELECT CONVERT(VARCHAR(11), GETDATE(), 106) AS [DD MON YYYY] 23 Feb 2014
Mon DD, YY SELECT CONVERT(VARCHAR(10), GETDATE(), 7) AS [Mon DD, YY] Feb 23, 14
Mon DD, YYYY SELECT CONVERT(VARCHAR(12), GETDATE(), 107) AS [Mon DD, YYYY] Feb 23, 2014
HH:MM:SS SELECT CONVERT(VARCHAR(8), GETDATE(), 108) 05:15:10
Mon DD YYYY HH:MI:SS:MMMAM (or PM) SELECT CONVERT(VARCHAR(26), GETDATE(), 109) Feb 23 2014 10:15:10:214AM
MM-DD-YY SELECT CONVERT(VARCHAR(8), GETDATE(), 10) AS [MM-DD-YY] 02-23-14
MM-DD-YYYY SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY] 02-23-2014
YY/MM/DD SELECT CONVERT(VARCHAR(8), GETDATE(), 11) AS [YY/MM/DD] 14/02/23
YYYY/MM/DD SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD] 2014/02/23
YYMMDD SELECT CONVERT(VARCHAR(6), GETDATE(), 12) AS [YYMMDD] 140223
YYYYMMDD SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD] 20140223
DD Mon YYYY HH:MM:SS:MMM(24h) SELECT CONVERT(VARCHAR(24), GETDATE(), 113) 23 Feb 2014 11:30:25:100
HH:MI:SS:MMM(24H) SELECT CONVERT(VARCHAR(12), GETDATE(), 114) AS [HH:MI:SS:MMM(24H)] 10:15:20:211
YYYY-MM-DD HH:MI:SS(24h) SELECT CONVERT(VARCHAR(19), GETDATE(), 120) 2014-02-23 16:00:10
YYYY-MM-DD HH:MI:SS.MMM(24h) SELECT CONVERT(VARCHAR(23), GETDATE(), 121) 2014-02-23 08:15:25.120
YYYY-MM-DDTHH:MM:SS:MMM SELECT CONVERT(VARCHAR(23), GETDATE(), 126) 2014-02-23T10:25:30:120
DD Mon YYYY HH:MI:SS:MMMAM SELECT CONVERT(VARCHAR(26), GETDATE(), 130) 23 Feb 2014 10:25:30:120AM
DD/MM/YYYY HH:MI:SS:MMMAM SELECT CONVERT(VARCHAR(25), GETDATE(), 131) 23/02/2014 10:25:30:120AM

Wednesday, June 18, 2014

JQuery - Detect a button click

The following example shows how to detect a button click.
With JQuery detect when the document is ready
Load for the button by it's id
Call the Click event of the button
Make an empty function with the code to be executed when the click event happens
 

// Java Script Function
$(document).ready(function () {
    $('#btn').click(function () {
        alert("The button was clicked.");
    });
});


    Click this button:

    

Thursday, June 12, 2014

Steps to set startup page for debugging in asp.net mvc aplication

To set the start page in Visual Studio for a MVC application, follow the next steps:

  1. Got to Projects >> Properties...
  2. Click the Web Tab
  3. Select the Specific Page radio button
  4. Type the URL in the Specific Page text box


Wednesday, June 11, 2014

How to get the last ID inserted in SQL


To get the last ID after an insertion you can use the @@Identity system function, if there was not insertion you will get a NULL value:

INSERT INTO People(First) VALUES ('Joseph');
SELECT ID FROM People WHERE ID = @@Identity;


You can also use the MAX function to get the last record inserted:

SELECT MAX(ID) AS ID FROM People

Another way to get it, is by the ident_current function:

SELECT ident_current(People) AS ID

Is it safe to delete files in the Windows Installer folder?

Over time the folder C:\Windows\Installer can grow a lot, many computers or laptops with small HD can have some problems.

The C:\Windows\Installer folder is really important because is where some applications uninstall files and folders are stored, if you delete the files you are not going to be able to uninstall correctly the applications.

Something you can do is backup this folder and when an application needs to be removed you can restore the files from the backup, the uninstall process should work just fine.

Monday, June 9, 2014

ModelState.AddModelError to add a simple validation

Use ModelState.AddModelError to add a simple validation and make the ModelState.isValid with errors.



   // Validation logic
   if (MyData.Email.Trim().Length == 0){
        ModelState.AddModelError("Email", "Invalid Email");
   
        return View(MyData);
   }


Tuesday, June 3, 2014

Using @Html.ActionLink as image link

If you need to serve image with the link using Razor, you can use HtmlHelpers like Html.ActionLink() to make it possible, this is how you can do it. First define the style with the image:

a.link_logo {
    background:url(/images/logo.jpg) no-repeat top left;
    width: 355px;
    height: 75px;
    display: block;
    text-indent: -9999px; /* In order to hide the text */
}



Then use the HtmlHelper and add the style:

// --------------------------------
@Html.ActionLink("Home", "Index", "Home", null, null, null, null, new { @class = "link_logo" })


// --------------------------------
@Html.ActionLink("Home", "Index", null, new { @class = "link_logo" })


//---------------------------------

Wednesday, May 28, 2014

How to change the Instance Name of SQL Server

SQL Server uses its own name, which could be different than the network server name, in order to know the name of your SQL server run the next command:

sp_helpserver
select @@servername


In order to change the name of the server you need to use the next commands:

sp_dropserver 'old_name'
go
sp_addserver 'new_name','local'
go

Don't get scare with the name of the commnad "DROP SERVER", it is just making reference to the SQL name.

After you ran the commands all you have to do is restart the SQL Server service, use the command @@servername to verify the new name of your server.

You cannot create a local SQL Server as a linked server

If you try to create a linked server to itself you will get the next error:


There is  a trick to do it, it is called loop back linked server.

In the Linked server textbox instead of providing the name of the SQL Server instance, type "." (only dot). This will indicate the linked server is a loopback linked server.



On the Security tab, choose "Be made using the login's current security context" select option.


Click the OK button to finish adding linked server to SQL Server which points to itself.

In Object Explorer window, you will see the new loopback SQL Server linked server with "." as the name.


Thursday, April 24, 2014

Exclusive access could not be obtained because the database is in use SQl 2005


If you get the error:
Exclusive access could not be obtained because the database is in use:



It is because when you restore the a database you should have Exclusive lock, you can kill the process which are using database. otherwise use the command showed below.
  
--
-- This should give you Exculusive lock
--
  
USE MASTER
ALTER DATABASE  MyDatabase   SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

-- Do your work

-- Reenable the database
ALTER DATABASE MyDatabase SET MULTI_USER WITH ROLLBACK IMMEDIATE;

--
--

Friday, February 21, 2014

How to get the ID of a database in SQL

To get the id of a database just execute the following built-in function:
  
--
-- This query will return the id of the specific database
--
  
SELECT db_id('my_database_name')