T-SQL: Cannot drop the index because it does not exist or you do not have permission.

2010 February 1
by r.claypool

database

Quick tip:   The table name and index name are required to drop an index, otherwise SQL Server will return an error.

e.g.

DROP INDEX [dbo].[IX_MyIndex]

fails with …

Msg 3701, Level 11, State 6, Line XX
Cannot drop the index 'dbo.IX_MyIndex', 
because it does not exist or you do not have permission.

However,

DROP INDEX [dbo].[MyTable].[IX_MyIndex]

will work fine if it exists and you have sufficient permissions. (Use Select user_name() to determine your user context, which will help to verify the permissions being used.)

Intellisense for NAnt Build Files (VS 2005 and 2008)

2010 January 17
by r.claypool

nant

Step 1. Make VS recognize a .build file as an XML file.

  1. Right-click a .build file in the “Solution Explorer” and select “Open With …”
  2. In the “Open With” dialog, select “XML Editor” and “Set as Default”

vs-2005-2008-open-with-dialog

Step 2: Enable intellisense for .build files.

Get the schema file for NAnt:

Copy it to the “xml\Schemas” subdirectory of your VS installation:

  • Note that you can check this path via Tools > Options > Text Editor > XML > Miscellaneous

schema-location-option

Specify the schema to use within Visual Studio:
  • Open the file so that it appears in a Visual Studio editor window.
  • Click the file’s editor window (to activate it) and specify the schema to use (within the “Properties” window).
    change-schema-properties
  • Note that this dialog has changed from VS 2005 to 2008:

Visual Studio 2005
(VS 2005)

Visual Studio 2008
(VS 2008)

If all of that is not enough,  make sure to enter an xmlns and make sure it is equal to the xmlns specified in the XSD schema

!!!!! Even though the schema is available at http://nant.sf.net/release/0.86-beta1/nant.xsd and http://nant.sourceforge.net/release/0.86-beta1/nant.xsd, you must reference the former url in your build file because it is the one referenced in nant.xsd. !!!!!

Here is an example of what I mean:

<?xml version="1.0" ?>
<project xmlns="http://nant.sf.net/release/0.86-beta1/nant.xsd" name="MyProject">
  <!-- stuff here -->
</project>

Hope that helps someone.  Happy Programming!


Attribution: The ant photo in this post is used with permission (CC BY-SA 2.0) by Jeff Kubina

Getting DataTables to work with ASP.Net MVC

2009 December 3
by r.claypool

Want to get jQuery DataTables to work in an ASP.Net MVC project ???

Here is a demo project in C# that should get you started.  If I find the time, I’ll post details on how this was put together (but I think most developers will benefit more from running and stepping through the source code anyway).

Hope this helps someone.  Happy Programming!

Viewing the Activity Log for VS 2010

2009 November 1
tags:
by r.claypool

Are you repeatedly getting a less than helpful error dialog in Visual Studio 2010?

Go to the command line and run devenv.exe with logging, reproduce the error in the IDE and then open ActivityLog.xml:

devenv

C:\>cd "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE"
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE>devenv /Log
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE>cd %APPDATA%
C:\Documents and Settings\username\Application Data>cd "Microsoft\VisualStudio\10.0"
C:\Documents and Settings\username\Application Data\Microsoft\VisualStudio\10.0>dir ActivityLog.xml /B ActivityLog.xml
C:\Documents and Settings\username\Application Data\Microsoft\VisualStudio\10.0>notepad ActivityLog.xml

Look for entries with type=Error, e.g.

<entry>
<record>263</record>
<time>2009/10/24 22:50:09.218</time>
<type>Error</type>
<source>Editor or Editor Extension</source>
<description>System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.VisualStudio.Editor.Implementation.VsFontsAndColorsInformation.GetEditorFontForItem(IVsColorTable colorTable, Int32 itemIndex, FONTCOLORPREFERENCES2 fontColorPreferences)
at Microsoft.VisualStudio.Editor.Implementation.VsFontsAndColorsInformation.TryGetItemValue(String itemKey, ResourceDictionary& itemValue)
at Microsoft.VisualStudio.Text.Classification.Implementation.EditorFormatMap.CreateResourceDictionaryFromProvision(String key)
at Microsoft.VisualStudio.Text.Classification.Implementation.EditorFormatMap.GetProperties(String key)
at Microsoft.VisualStudio.Text.Classification.Implementation.ClassificationFormatMap.GetDefaultProperties()
at Microsoft.VisualStudio.Text.Classification.Implementation.ClassificationFormatMap..ctor(ICollection`1 formats, IClassificationTypeRegistryService classificationTypeRegistry, IEditorFormatMap formatMap)
at Microsoft.VisualStudio.Text.Classification.Implementation.ClassificationFormatMapService.GetClassificationFormatMap(String category)
at Microsoft.VisualStudio.Text.Classification.Implementation.ViewSpecificFormatMap.AttachToFormatMaps()
at Microsoft.VisualStudio.Text.Classification.Implementation.ViewSpecificFormatMap..ctor(IClassificationFormatMapService classificationFormatMapService, IEditorFormatMapService editorFormatMapService, ITextView textView)
at Microsoft.VisualStudio.Text.Classification.Implementation.EditorFormatMapService.<>c__DisplayClass1.<GetEditorFormatMap>b__0()
at Microsoft.VisualStudio.Utilities.PropertyCollection.GetOrCreateSingletonProperty[T](Object key, Func`1 creator)
at Microsoft.VisualStudio.Text.Classification.Implementation.EditorFormatMapService.GetEditorFormatMap(ITextView textView)
at Microsoft.VisualStudio.Text.Editor.Implementation.OutliningAdornmentManagerFactory.CreateTagger[T](ITextView textView, ITextBuffer buffer)
at Microsoft.VisualStudio.Text.Tagging.Implementation.TagAggregator`1.GatherTaggers(ITextBuffer textBuffer)</description>
</entry>

Castle Windsor 2.0 in ASP.Net MVC 1.0

2009 October 23
by r.claypool

Consider yourself warned:  I’m just getting started with ASP.Net MVC, Dependency Injection (DI) and Inversion of Control (IoC), so this post is more for my benefit than yours.

warning-castle-windsor

ASP.Net MVC 1.0

ASP.Net MVC 1.0 was released in March 2009. Read this tutorial to start learning it. Note that ASP.Net MVC is bin deployable which is very nice if you don’t want to install anything on the production web server.

Inversion of Control and Castle Windsor

Castle Windsor 2.0 was just released this Summer after a very long development period. To start using it, go get the dlls and reference them in your project.

If you are completely new to the IoC pattern, go read James Kovacs’ article and watch these videos.  They will get a typical .Net programmer up to speed pretty fast. You should also go to the canonical source for IoC, but frankly I learned more from the other links and I think you will too.

To learn how to configure the Windsor container in your project, read the project’s documentation and continue with this article for an example in ASP.Net MVC.

Putting them Together

castle-plus-aspnet-mvc

Getting Windsor to work in an ASP.Net MVC application is easy if you know what to do. Most things are that way, huh?

For a bare bones integration, just follow these steps:

  1. Install ASP.NET MVC 1.0.  The Web Platform Installer is great for this and a ton of other projects.  Try it.  You’ll be glad you did.
  2. Open VS 2008,  go to File->New->Project… and select the “ASP.NET MVC Web Application” project template.
  3. Select “No” on the proceeding dialog. Do not create a unit test project.
  4. Download Castle Windsor 2.0
  5. Copy the dlls (Castle.DynamicProxy2, Castle.MicroKernel, Castle.Windsor and Castle.Core) to your solution’s folder or a sub folder.
  6. Add them as references to the web project. (Right-click the “References” folder, select “Add Reference…”, select the “Browse” tab and navigate to the dlls to add them.)
  7. Add a new XML file to the web project.  Name it “windsor.config” and enter the following:
  8. <?xml version="1.0" encoding="utf-8" ?>
    <!--Note: Since this is a web application, all components must have a lifesytle of 'transient' or 'preWebRequest' -->
    <configuration>
      <components>
        <!--This is where we could declaratively add components to the Windsor container if we wanted to.-->
      </components>
    </configuration>
  9. Create an interface IMessageHolder and a class MessageHolder.  (We will use this code to illustrate injecting an instance of MessageHolder into the HomeController.)
  10. public interface IMessageHolder
    {
    	string GetMessage();
    }
     
    public class MessageHolder : IMessageHolder
    {
    	#region IMessageHolder Members
     
    	public string GetMessage()
    	{
    		return "Welcome to ASP.NET MVC using Castle Windsor 2.0!";
    	}
     
    	#endregion
    }
  11. Create a controller factory for Windsor:
  12. public class WindsorControllerFactory : DefaultControllerFactory
    {
    	private readonly IWindsorContainer _container;
     
    	public WindsorControllerFactory(IWindsorContainer container)
    	{
    		_container = container;
    	}
     
    	protected override IController GetControllerInstance(Type controllerType)
    	{
    		if (controllerType == null)
    		{
    			// Force an exception message from the base class.
    			// This will be more informative than what we would get otherwise,
    			// see: http://stackoverflow.com/questions/719678/custom-controller-factory-dependency-injection-structuremap-problems-with-asp/721313#721313
    			return base.GetControllerInstance(controllerType);
    		}
    		// Exception here ????
    		// Check the windsor configuration.
    		// Make sure that Windsor can match the component to load.
    		return _container.Resolve(controllerType) as IController;
    	}
     
    	public override void ReleaseController(IController controller)
    	{
    		_container.Release(controller);
    	}
    }
  13. Open Global.asax.cs and replace it with the following:
  14. public class MvcApplication : HttpApplication
    {
    	/// <remarks>
    	/// This has to be static, because more than one instance
    	/// of the HttpApplication can potentially be created.
    	/// </remarks>
    	private static IWindsorContainer _container;
     
    	public static void RegisterRoutes(RouteCollection routes)
    	{
    		routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     
    		routes.MapRoute(
    			"Default", // Route name
    			"{controller}/{action}/{id}", // URL with parameters
    			new {controller = "Home", action = "Index", id = ""} // Parameter defaults
    			);
    	}
     
    	protected void Application_Start()
    	{
    		RegisterRoutes(RouteTable.Routes);
    		InitializeWindsor();
     
    		// Setup the MVC framework to use our Windsor controller factory.
    		ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));
    	}
     
    	protected virtual void InitializeWindsor()
    	{
    		if (_container == null)
    		{
    			_container = Build("windsor.config");
    		}
    	}
     
    	public static IWindsorContainer Build(string configPath)
    	{
    		var resource = new FileResource(configPath);
    		var interperter = new XmlInterpreter(resource);
    		var container = new WindsorContainer(interperter);
     
    		// Register controllers
    		container.Register(AllTypes
    					   .Of<Controller>()
    					   .FromAssembly(Assembly.GetExecutingAssembly())
    		/* Modify the name with which the component is registered
    		See: http://blog.andreloker.de/post/2009/03/28/ASPNET-MVC-with-Windsor-programmatic-controller-registration.aspx */
    					   .Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower())));
     
    		// Register all other types.
    		container.Register(
    			Component.For<IMessageHolder>().ImplementedBy<MessageHolder>().LifeStyle.Transient);
     
    		return container;
    	}
    }
  15. Add a constructor to the HomeController and give it an IMessageHolder parameter.
  16.     [HandleError]
        public class HomeController : Controller
        {
            private readonly IMessageHolder _holder;
     
            public HomeController(IMessageHolder holder)
            {
                _holder = holder;
            }
     
            public ActionResult Index()
            {
                ViewData["Message"] = _holder.GetMessage();
     
                return View();
            }
     
            public ActionResult About()
            {
                return View();
            }
        }

    Since IMessageHolder is registered with Windsor, the container will resolve IMessageHolder to an instance of MessageHolder at run-time. This is called dependency injection because the HomeController dependency is injected through the constructor. Of course, you could do this without an IoC container such as Windsor, but in larger (real world) applications the container will greatly simplify the process of managing dependencies, which means you have a better chance of keeping things loosely coupled and falling into the pit of success.

    Notice also that we display the message in Index() to verify that the Windsor container is giving us a valid instance of the type registered for IMessageHolder. This is just a simple way to see the result of what occurred.

  17. Run the application and you should see the message on the home page. Click here to download the sample project.
  18. aspnet-and-windsor-integration

Project Source Code for this post.

I hope that this walk-through helps someone get started with Windsor. It really is an incredible project to learn and if used correctly it will make your programs much better. Have fun with it and Happy Programming!

Creative Commons Attribution 3.0 United States
Creative Commons Attribution 3.0 United States