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;

--
--