Tuesday, 26 June 2012

Cannot open database "[databasename]" requested by the login. The login failed. Login failed for user 'PC\User'.

While working with a combination of ASP.Net/Visual Studio/IIS/SQL Server, etc, you may occasionally come across something like: 

Cannot open database "AdventureWorks" requested by the login. The login failed. Login failed for user 'My-PC\vic'.


Where 'My-PC' is the PC or Workstation name and 'vic' is a valid user login name on the PC. 


This error usually occurs when an asp.net application is configured to use IIS, and the application is trying to gain access (or login) to a SQL Server database that has not been configured with sufficient permissions for the current user of that application.


Potential fix as follows:


Under the 'Logins' node of the 'Security' node in SSMS, right-click on the user and select Properties. 



Then on the Login properties dialogue click on 'User Mapping' in the 'Select a page' left window, tick the database that has the login issue in the 'Users mapped to this login:' in the right window, and the in the bottom window under 'Database role membership for: [AdventureWorks]', make sure 'db_owner' is ticked.   


In most cases this should help resolve the issue.


Wednesday, 20 June 2012

SQL Sever Isolation Levels

These isolation levels allow different types of behavior.

Isolation
level
Dirty
read
Nonrepeatable
read
Phantom
Read uncommittedYes Yes Yes
Read committedNo Yes Yes
Repeatable read No No Yes
Serializable No No No

Transactions must be run at an isolation level of repeatable read or higher to prevent lost updates that can occur when two transactions each retrieve the same row, and then later update the row based on the originally retrieved values. If the two transactions update rows using a single UPDATE statement and do not base the update on the previously retrieved values, lost updates cannot occur at the default isolation level of read committed.

msdn.microsoft.com

Controlling a Transaction's Isolation Level

In a database, a record is referred to as dirty if it has changed (modified) since the last time its table (or view) was opened. When creating a transaction, you can give instructions to the database engine about how to commit, or whether to dismiss, a transaction with regards to a dirty record. To support this, you start with the following formula:
SET TRANSACTION ISOLATION LEVEL
    { READ UNCOMMITTED
    | READ COMMITTED
    | REPEATABLE READ
    | SNAPSHOT
    | SERIALIZABLE
    }
[ ; ]
You start with the SET TRANSACTION ISOLATION LEVEL expression and follow it with a value:
  • READ UNCOMMITTED: This value asks that the database engine read the records that are dirty but were not yet committed
  • READ COMMITTED: This value indicates that the dirty records should not be read
  • REPEATABLE READ: This value indicates that the current transaction should ignore dirty records from other transactions and the other transactions don't have access to the records of this transaction
  • SNAPSHOT
  • SERIALIZABLE

Web Site project vs Web Application project


How does a Web Site project differ from a Web Application project:

A common question by asp.net developers is what project model to use for an asp.net application? Web Site project (which was introduced in VS 2005) or Web Application project (which was delivered as an add-in for VS 2005 and built-in to VS 2005 SP1).


The main differences between 2 of them are as follows.

Web Application project model:

  • Provides the same Web project semantics as Visual Studio .NET 2003 Web projects.
  • Has a project file (structure based on project files).
  • Build model - all code in the project is compiled into a single assembly.
  • Supports both IIS and the built-in ASP.NET Development Server.
  • Supports all the features of Visual Studio 2005 (refactoring, generics, etc.) and of ASP.NET 2.0 (master pages, membership and login, site navigation, themes, etc).
  • Using FrontPage Server Extensions (FPSE) are no longer a requirement.



Web Site project model:

  • No project file (Based on file system).
  • New compilation model.  
  • Dynamic compilation and working on pages without building entire site on each page view.
  • Supports both IIS and the built-in ASP.NET Development Server.
  • Each page has it's own assembly.
  • Defferent code model.  (Read here for more details)
  • Ok, all is great, but you want to create your web site now. Which model should you use?



If you:
  • need to migrate large Visual Studio .NET 2003 applications to VS 2005: use the Web Application project.

  • need to add pre-build and post-build steps during compilation :  use Web Application project.

  • need to build a Web application using multiple Web projects :  use Web Application project.

  • want to generate one assembly for each page :  use Web Site project.

  • want to open and edit any directory as a Web project without creating a project file :  use Web Site project.

  • prefer dynamic compilation and working on pages without building entire site on each page view :  use Web Site project.

  • prefer single-page code model to code-behind model :  use Web Site project.

see: blogs.msdn.com
also: blogspot.co.uk

Tuesday, 19 June 2012

How to manually Register a DLL File in Windows

A DLL (aka Dynamic Link Library) is a file that may be used by various Windows applications to call various built-in functions to perform various actions.

You may sometimes get in a situation where a required DLL has not been registered automatically into the Windows registry by a dependent application and you may have to register the DLL manually. You can do this from the 'Run...' dialogue of the Start Menu (or alternatively from a command prompt); type regsvr32 followed by the DLL name or the fullpath to the DLL file followed by the file name,

for example regsvr32 C:\Program Files\MyApp\MyApp.dll

Be sure to leave a space between the regsvr32 command and the path+filename.

From Start Menu:



From Command Prompt:


You should get a confirmation dialogue box telling you the DLL was registred succesfully, for example "DllRegisterServer MyApp.dll succeeded"

Saturday, 16 June 2012

Remove Programs from the 'Open With' Menu in Explorer

Clean up the 'Open With' menu in Windows Explorer...remove program entries you no longer want, or that you've accidentally added to this menu; Here's how >>.

Wednesday, 6 June 2012

Turn Navigation Pane On or Off (Expand to Current Folder option) in Windows 7

When you navigate to a folder via Windows Explorer in Windows 7, "Automatically expand to current folder" is Turned Off by default. I find it rather annoying to always have to 'manually' expand the Navigation Pane on the left, each time I navigate to, or open a new folder in the right hand pane.




Fear not...fix is the way...just right-click on an empty space in the Navigation Pane on the left, and click on "Expand to current folder", this is off by default and clicking on it will turn it on, so when you reload the context menu it will now have a tick next to it, indicating the option is now on:



Check out more on this story here >> : How to Turn Navigation Pane On or Off (Expand to Current Folder) in Windows 7

Friday, 1 June 2012

Taking ownership of Windows files

Registry 'hack' for taking ownership of Windows files. A simpler work-around for the usually cumbersome series of steps you normally otherwise have to take to achieve the same results: Copy and paste the following script into notepad and save the file with a .reg extension. (for example, TakeOwnership.reg) You will then be able to double-click on the file and have the registry auto-updated with the necessary settings.

You'll most likely get a prompt asking "Adding information can unintentionally change or delete values...blah blah blah...etc, ...Are you sure you want to continue". Just click 'Yes' if you're sure you want the 'Take Ownership' feature installed.

This is the menu option (highlighted in yellow) that this registry update will add to your right-click context menu in Windows Explorer:


Copy Code below:

[HKEY_CLASSES_ROOT\*\shell\runas]
@="Take Ownership"
"NoWorkingDirectory"=""
[HKEY_CLASSES_ROOT\*\shell\runas\command]
@="cmd.exe /c takeown /f \"%1\" && icacls \"%1\" /grant administrators:F"
"IsolatedCommand"="cmd.exe /c takeown /f \"%1\" && icacls \"%1\" /grant administrators:F"
[HKEY_CLASSES_ROOT\Directory\shell\runas]
@="Take Ownership"
"NoWorkingDirectory"=""
[HKEY_CLASSES_ROOT\Directory\shell\runas\command]
@="cmd.exe /c takeown /f \"%1\" /r /d y && icacls \"%1\" /grant administrators:F /t"
"IsolatedCommand"="cmd.exe /c takeown /f \"%1\" /r /d y && icacls \"%1\" /grant administrators:F /t"