Wednesday, March 30, 2011

Index (zero based) must be greater than or equal to zero and less than the size of the argument list

Problem:
System.FormatException was unhandled by user code
  Message=Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
  Source=mscorlib
  StackTrace:
       at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
       at System.String.Format(IFormatProvider provider, String format, Object[] args)
       at System.String.Format(String format, Object arg0)
       at MultiThreadedDbSeeder.Program.<Main>b__0(Int32 i) in C:\xxxx\Program.cs:line 22
       at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.b__c()
  InnerException:

Offending Line:
SqlCommand cmd = new SqlCommand(string.Format(@"insert into table_1 (vch_value) values('{1}')", "the value of i is " + i));

Solution:
Don’t forget that string.Format uses a zero based index.
Fixed Code:
SqlCommand cmd = new SqlCommand(string.Format(@"insert into table_1 (vch_value) values('{0}')", "the value of i is " + i));

Tuesday, March 29, 2011

The project file has been moved, renamed or is not on your computer

Solution:
1) Close the solution you have open.
2) In the project folder which is giving you problems, find the .suo file and delete it.
3) Reopen your solution and add the project back.

Saturday, March 26, 2011

Quartz.ObjectAlreadyExistsException: Unable to store Job with name: '' and group: 'DEFAULT', because one already exists with this identification.

Problem:
Exception Caught: Quartz.ObjectAlreadyExistsException: Unable to store Job with name: 'updateMyStuff' and group: 'DEFAULT', because one already exists with this identification.


Code:
                 // construct job info for every 10 seconds
                JobDetail = jobDetail = new JobDetail("updateMyStuff", null, typeof(UpdateMyOneMethod));
                trig = new CronTrigger();
                trig.CronExpression = new CronExpression("0/10 * * * * ?");
                trig.Name = " updateStuff ";
                sched.ScheduleJob(jobDetail, trig);

                // job for every day 12:00 am
                jobDetail = new JobDetail("updateMyStuff", null, typeof(UpdateMyOtherMethod));
                trig = new CronTrigger();
                trig.CronExpression = new CronExpression("0 0 0 * * ?");
                trig.Name = "updateStuff";
                sched.ScheduleJob(jobDetail, trig);



Solution:
I was clearly very tired and not paying attention and made a miserable copy/paste error.  Make sure the JobDetail has a unique name and same with the trigger!  Fixed Code, changes bolded:

                 // construct job info for every 10 seconds
                JobDetail = jobDetail = new JobDetail("updateMyStuff", null, typeof(UpdateMyOneMethod));
                trig = new CronTrigger();
                trig.CronExpression = new CronExpression("0/10 * * * * ?");
                trig.Name = " updateStuff ";
                sched.ScheduleJob(jobDetail, trig);

                // job for every day 12:00 am
                jobDetail = new JobDetail("updateMyOtherStuff", null, typeof(UpdateMyOtherMethod));
                trig = new CronTrigger();
                trig.CronExpression = new CronExpression("0 0 0 * * ?");
                trig.Name = "updateOtherStuff";
                sched.ScheduleJob(jobDetail, trig);



Sunday, March 20, 2011

Should I Create a Business Logic Layer?

The Business Logic Layer (BLL) is located between the Data Access Layer and the User Interface.  This is where your data is traslated from Database objects, like DataTables and DataRows into Classes and Objects.

Sometimes I use them, and other times I don’t.  This should become obvious in your design phase, when you learn whether or not your application is manipulating data after retrieving it from a database.  If your application is CRUD (Create Retrieve Update Delete), then most likely you don’t need a BLL.  It would be a waste of your time to add an unnecessary layer just because it is a “Proper Model”.

I find that with the Entity Framework the need for a business layer is further diminished.  The reason is that the auto generated code, that is the Model, takes out the need to translate DataRow to an Object.

Now, the only reason left to have a business layer is for business logic.  This is were much of your common code may go, like generation of reports, charts, and calculations.  The last few projects I have done only had partial business logic layers.  I only created them when necessary.

Saturday, March 19, 2011

The type or namespace name 'xxxx' does not exist in the namespace 'yyyy' (are you missing an assembly reference?)

Problem:
Error    1          The type or namespace name 'xxxx' does not exist in the namespace 'yyyy' (are you missing an assembly reference?)
Screenshot:

My environment: Visual Studio 2010, .Net 4.0.   Two projects involved, 1 is a dll, other is a console app.

You know your reference is correct, but you still get that reference error.  Your using clause fails, too.
Screenshot:
 

Solution:
In this case, my console app had the Target Framework in the project properties set to .NET Framework 4 Client Profile, the dll project had it set to .NET Framework 4.  I changed the console app to use .NET Framework 4, and everything built fine.

Console App Project properties before:

Console App Project properties after:


Thursday, March 17, 2011

Building Visual Studio Solutions Out of the Box

It is terrible when you are a developer at a new place and you open Visual Studio the first time, connect to your favorite source control like TFS or SVN, and open the desired solution, click build only to see many compile errors like invalid references.

Avoid creating this situation now by organizing your solutions so that they can easily be opened and built by new people.  You need to take into consideration how your solution is layed out, where external dependencies are, and how you present external tools.

First thing you need to do is make sure you solution file is at the root of all your projects.  If your all your projects are under c:/source, like in c:/source/MovieApp, then your solution should reside in c:/source.  You want to do this because it is not good practice to have a project reach up to a parent directory of the solution file into another branch for a referenced file or project.

Your structure should look like this:
C:/source
            MyMovieSolution.sln
C:/source/MovieApp
            MovieApp.csproj
C:/source/MvcMovie
            MvcMovie.csproj

Your next consideration needs to be references to external dependencies.  It is best if you have one location for this, right off the solution folder.  I usually call mine “External Dependencies”.  Then what you need to do is create an External Dependencies folder in the Visual Studio Solution and add your files from your “External Dependences” folder.  Now, whenever you reference a dll that is external, you use those files, and it will be gotten when you have a new developer get latest!  Easy enough.


















Next thing is to provide all your external tools in your solution.  I cannot tell you the number of times I had to go find an exact version of a tool because of some quirk that version had but no others did.  Check it in with your source, don’t store it on a shared network drive.  Update it?  Great, update it in your source control.
 









Last tip is do not set your output directories to the same folder!  Never do this, please.  Ever.  Leave them in bin.  If you are doing this, you most likely have an external dependency problem in one of your projects.  Fix that.

Saturday, March 5, 2011

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Problem:

Server Error in '/' Application.


The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Source Error: 
 
Line 32:         /// Initializes a new xxxxEntities object using the connection string found in the 'xxxxEntities' section of the application configuration file.
Line 33:         /// 
Line 34:         public xxxxEntities() : base("name=xxxxEntities", "xxxxEntities")
Line 35:         {
Line 36:             this.ContextOptions.LazyLoadingEnabled = true;

Source File: C:\Users\xxxx\Documents\Visual Studio 2010\Projects\xxxx.Designer.cs    Line: 34 

Stack Trace: 
 
[ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.]
   System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) +8080056
   System.Data.EntityClient.EntityConnection..ctor(String connectionString) +81
   System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString) +42
   System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName) +16
   xxxx..ctor() in C:\Users\xxxxx.cs:34
   xxxx(Int32 id) in C:\Users\xxx.cs:25
   xxxx() in C:\Users\xxxx\Documents\Visual Studio 2010\Projects\xxxxDal.cs:36
   xxxx() in C:\Users\xxxx\Documents\Visual Studio 2010\Projects\xxxx.cs:13
   xxxx(Object sender, EventArgs e) in C:\Users\xxxxaspx.cs:145
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563



Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1


Solution:
Add the entity connection string to the web.config file in the appropriate project.

Ambiguous reference - The type or namespace name 'Quartz' could not be found (are you missing a using directive or an assembly reference?)

Problem:
Environment: VS 2010 .Net 4.0 Console app using Quartz.Net http://quartznet.sourceforge.net/ as a Project Reference.

I would get an Ambiguous reference listing Quartz.IJob twice.

I had only 1 reference to Quarts in the list of References for my Project, that showed up as Quartz.2008.

Compile errors:
Error    1          The type or namespace name 'Quartz' could not be found (are you missing a using directive or an assembly reference?)
Error    5          The type or namespace name 'JobExecutionContext' could not be found (are you missing a using directive or an assembly reference?)  

Quartz Project properties had the target framework set to .NET Framework 3.5.

Solution:
Change the Target framework on the Quartz.2008 project properties to .NET Framework 4.  I ensured my project used .NET Framework 4, also.

If you get the following error in Quartz, then you had set the Target framework to .NET Framework Client Profile instead of .NET Framework 4.

Error    6          The type or namespace name 'HttpContext' could not be found (are you missing a using directive or an assembly reference?)          xxxx\Quartz.NET-1.0.3\src\Quartz\Util\LogicalThreadContext.cs    41        4            Quartz.2008




Failed to start monitoring changes to 'path' because access is denied.

Problem:
Brought up a web page and was presented with this error:

Server Error in '/' Application.


Failed to start monitoring changes to 'C:\Users\xxxx\AppData\Local\Temp\Temporary ASP.NET Files\root\b6230deb\7677a327\hash\hash.web' because access is denied.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Failed to start monitoring changes to 'C:\Users\xxxx\AppData\Local\Temp\Temporary ASP.NET Files\root\b6230deb\7677a327\hash\hash.web' because access is denied.

Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 
 
[HttpException (0x80070005): Failed to start monitoring changes to 'C:\Users\xxxx\AppData\Local\Temp\Temporary ASP.NET Files\root\b6230deb\7677a327\hash\hash.web' because access is denied.]
   System.Web.DirectoryMonitor.AddFileMonitor(String file) +8805891
   System.Web.DirectoryMonitor.StartMonitoringFileWithAssert(String file, FileChangeEventHandler callback, String alias) +94
   System.Web.FileChangesMonitor.StartMonitoringFile(String alias, FileChangeEventHandler callback) +340
   System.Web.Compilation.BuildManager.CheckTopLevelFilesUpToDate2(StandardDiskBuildResultCache diskCache) +790
   System.Web.Compilation.BuildManager.CheckTopLevelFilesUpToDate(StandardDiskBuildResultCache diskCache) +55
   System.Web.Compilation.BuildManager.RegularAppRuntimeModeInitialize() +174
   System.Web.Compilation.BuildManager.Initialize() +261
   System.Web.Compilation.BuildManager.InitializeBuildManager() +246
   System.Web.HttpRuntime.HostingInit(HostingEnvironmentFlags hostingFlags, PolicyLevel policyLevel, Exception appDomainCreationException) +350
 
[HttpException (0x80004005): Failed to start monitoring changes to 'C:\Users\xxxx\AppData\Local\Temp\Temporary ASP.NET Files\root\b6230deb\7677a327\hash\hash.web' because access is denied.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8950644
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
   System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +258



Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1


Solution:
First, I had to close Visual Studio 2010, then I deleted c:\Users\xxxx\AppData\ocal\Temp\Temporary ASP.NET Files.
Reopened Visual Studio and ran it again in debug mode, worked fine.