How To Fix Archives - Page 5 of 12 - Digital Tool Factory blog

The Digital Tool Factory Blog

How to fix “The system cannot find the path specified” error in asp.net mvc 4

The Problem:

So, you’re coding away, and you run your asp.net mvc 4 site and you get the following error: The system cannot find the path specified

The Cause:

Most likely you have not already specified the database in the web.config, it’s a simple fix.

The Solution:

Just add this code into the web.config

<add name=”BlogProphetsiteContext” connectionString=”Server=.SQLEXPRESS;Database=BlogProphetsite;Trusted_Connection=true;MultipleActiveResultSets=true” providerName=”System.Data.SqlClient” />

and all will be good. For some reason it does not automatically generate one for you.


08
Jun 12


Written By Steve French

 

How to create a Cron job with MySqlDump, and use GZip Compression

'Wordpress cron file' photo (c) 2010, Harsh Agrawal - license: http://creativecommons.org/licenses/by/2.0/Full Disclosure: I am a long-term Microsoft Windows user and developer and I haven’t used any sort of Cron job in about 12 years, so all this was new to me.

What is a Cron job?

It’s just something that runs chronically, i.e. an automated procedure on the server that runs every day or hour.

What is MySqlDump?

It’s a nice simple way to do a backup of a MySql Database

What is GZip Compression?

It is the Linux version of zipping a file, using whatever open source algorithm they use.

How do Cron Jobs, MySqlDump and GZip Compression come together?

In my case, I needed to backup a very large multisite WordPress database.  After some further discover I discovered that it would behoove me to compress the database as well.   Without compression, the backup file was 250 megs, with compression it was 25 megs.

What is the syntax for a Cron Job with MySqlDump and GZip Compression?

Just use this

mysqldump –opt -Q -u[YourUserName] -p[YourPassword] [YourDatabaseName] |  gzip >/path/to/desired/backup/location/`date`.sql.gz

That will create a file named after the current date and time that is a zipped version of your database.  It runs surprisingly quickly.

What is the catch?

The reason this particular little task took me some extra time to do is because I had forgotten that Unix system use the back tick character (`) and not the single quote (‘).  FYI – the back tick character is the one located underneath the escape key.


25
May 12


Written By Steve French

 

How to fix problems running Windows Azure web applications locally

The Problem:

You have an asp.net mvc 3 web project, and you are running it as part of a windows azure web application.   In my case it worked initially, but after I installed some Visual Studio plugins, run Windows Update, and installed a new router, it mysteriously stopped working.  A few of the error messages had me thinking it was caused by Sql Server, but I was mistaken.

The Cause:

This stumped me for several hours, but after much trial and error I discovered that the problem was with port mapping.

In the output window, I had the following messages:

Windows Azure Tools: Warning: Remapping public port 80 to 87 to avoid conflict during emulation.
Windows Azure Tools: Warning: Remapping public port 3389 to 3390 to avoid conflict during emulation.
Windows Azure Tools: Warning: Remapping private port 80 to 89 in role ‘MyCoolSite’ to avoid conflict during emulation.
Windows Azure Tools: Warning: Remapping private port 3389 to 3390 in role ‘MyCoolSite’ to avoid conflict during emulation.

I had enabled a homegroup somewhere along the way, which changed my network location, which somehow made changes in the firewall.

The Solution

This was more blind luck, but I got a windows firewall popup that asked me if I wanted to allow the Visual Studio Process to use lo these many ports.

So, in sum, check to make sure all of your ports are open.


15
May 12


Written By Steve French

 

How to programatically set the sql server database used by entity framework

The Problem:

'Entity Framework' photo (c) 2009, SondreB - license: http://creativecommons.org/licenses/by/2.0/For one reason or another you need to set the database used by entity framework programatically.  The more common way of doing such a thing is to set the database in your web.config class, but for whatever reason you need to set it in actual C# code.

The Cause:

There is no cause really, it is merely the way that entity framework and sql server work together.

The Solution:

The answer lies in setting the constructor properly.

Example Code Using Entity Framework and Data Context

using System.Data.Entity;

namespace MyDBServerExperiments.Models
{
public class DTFContext : DbContext
{
public DTFContext() : base(“Data Source=MyDBServer.DBServer.com;Initial Catalog=MyEntityFrameworkDB;Persist Security Info=True;user id=MyUsername; Password=MyPassword;”) { }

public DbSet<User> Users { get; set; }
public DbSet<BusinessObjects> BusinessObjects { get; set; }
public DbSet<Property> Properties { get; set; }

}

}

The above would be your data context file that uses the DBContext file  in your models folder, just fyi (that is the common place for Entity Framework models in the standard  Visual Studio 2010 web solution structure).

Note, everything happens in the constructor.  This give you many interesting opportunities to programatically swap out Sql Server database names and users, compared to the web.config option, which tends to be far more static.  It allows far more extensive use of business objects and


24
Apr 12


Written By Steve French

 

How to make a random name generator in C#

I recnetly came up with a simple random name generator to do an initial seed of a database for testing using real names,a nd I thought I would share. Here is my NameGenerator.cs file. It’s fairly simple, it just picks a name at random out of the top 100 first names and the top 100 last names for the year 2011. Enjoy!

The code for the random name generator in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyNamespace
{
public class NameGenerator
{
public static Random rnd = new Random();

 

public static string GenRandomLastName()
{
List<string> lst = new List<string>();
string str = string.Empty;
lst.Add(“Smith”);
lst.Add(“Johnson”);
lst.Add(“Williams”);
lst.Add(“Jones”);
lst.Add(“Brown”);
lst.Add(“Davis”);
lst.Add(“Miller”);
lst.Add(“Wilson”);
lst.Add(“Moore”);
lst.Add(“Taylor”);
lst.Add(“Anderson”);
lst.Add(“Thomas”);
lst.Add(“Jackson”);
lst.Add(“White”);
lst.Add(“Harris”);
lst.Add(“Martin”);
lst.Add(“Thompson”);
lst.Add(“Garcia”);
lst.Add(“Martinez”);
lst.Add(“Robinson”);
lst.Add(“Clark”);
lst.Add(“Rodriguez”);
lst.Add(“Lewis”);
lst.Add(“Lee”);
lst.Add(“Walker”);
lst.Add(“Hall”);
lst.Add(“Allen”);
lst.Add(“Young”);
lst.Add(“Hernandez”);
lst.Add(“King”);
lst.Add(“Wright”);
lst.Add(“Lopez”);
lst.Add(“Hill”);
lst.Add(“Scott”);
lst.Add(“Green”);
lst.Add(“Adams”);
lst.Add(“Baker”);
lst.Add(“Gonzalez”);
lst.Add(“Nelson”);
lst.Add(“Carter”);
lst.Add(“Mitchell”);
lst.Add(“Perez”);
lst.Add(“Roberts”);
lst.Add(“Turner”);
lst.Add(“Phillips”);
lst.Add(“Campbell”);
lst.Add(“Parker”);
lst.Add(“Evans”);
lst.Add(“Edwards”);
lst.Add(“Collins”);
lst.Add(“Stewart”);
lst.Add(“Sanchez”);
lst.Add(“Morris”);
lst.Add(“Rogers”);
lst.Add(“Reed”);
lst.Add(“Cook”);
lst.Add(“Morgan”);
lst.Add(“Bell”);
lst.Add(“Murphy”);
lst.Add(“Bailey”);
lst.Add(“Rivera”);
lst.Add(“Cooper”);
lst.Add(“Richardson”);
lst.Add(“Cox”);
lst.Add(“Howard”);
lst.Add(“Ward”);
lst.Add(“Torres”);
lst.Add(“Peterson”);
lst.Add(“Gray”);
lst.Add(“Ramirez”);
lst.Add(“James”);
lst.Add(“Watson”);
lst.Add(“Brooks”);
lst.Add(“Kelly”);
lst.Add(“Sanders”);
lst.Add(“Price”);
lst.Add(“Bennett”);
lst.Add(“Wood”);
lst.Add(“Barnes”);
lst.Add(“Ross”);
lst.Add(“Henderson”);
lst.Add(“Coleman”);
lst.Add(“Jenkins”);
lst.Add(“Perry”);
lst.Add(“Powell”);
lst.Add(“Long”);
lst.Add(“Patterson”);
lst.Add(“Hughes”);
lst.Add(“Flores”);
lst.Add(“Washington”);
lst.Add(“Butler”);
lst.Add(“Simmons”);
lst.Add(“Foster”);
lst.Add(“Gonzales”);
lst.Add(“Bryant”);
lst.Add(“Alexander”);
lst.Add(“Russell”);
lst.Add(“Griffin”);
lst.Add(“Diaz”);
lst.Add(“Hayes”);

str = lst.OrderBy(xx => rnd.Next()).First();
return str;
}
public static string GenRandomFirstName()
{
List<string> lst = new List<string>();
string str = string.Empty;
lst.Add(“Aiden”);
lst.Add(“Jackson”);
lst.Add(“Mason”);
lst.Add(“Liam”);
lst.Add(“Jacob”);
lst.Add(“Jayden”);
lst.Add(“Ethan”);
lst.Add(“Noah”);
lst.Add(“Lucas”);
lst.Add(“Logan”);
lst.Add(“Caleb”);
lst.Add(“Caden”);
lst.Add(“Jack”);
lst.Add(“Ryan”);
lst.Add(“Connor”);
lst.Add(“Michael”);
lst.Add(“Elijah”);
lst.Add(“Brayden”);
lst.Add(“Benjamin”);
lst.Add(“Nicholas”);
lst.Add(“Alexander”);
lst.Add(“William”);
lst.Add(“Matthew”);
lst.Add(“James”);
lst.Add(“Landon”);
lst.Add(“Nathan”);
lst.Add(“Dylan”);
lst.Add(“Evan”);
lst.Add(“Luke”);
lst.Add(“Andrew”);
lst.Add(“Gabriel”);
lst.Add(“Gavin”);
lst.Add(“Joshua”);
lst.Add(“Owen”);
lst.Add(“Daniel”);
lst.Add(“Carter”);
lst.Add(“Tyler”);
lst.Add(“Cameron”);
lst.Add(“Christian”);
lst.Add(“Wyatt”);
lst.Add(“Henry”);
lst.Add(“Eli”);
lst.Add(“Joseph”);
lst.Add(“Max”);
lst.Add(“Isaac”);
lst.Add(“Samuel”);
lst.Add(“Anthony”);
lst.Add(“Grayson”);
lst.Add(“Zachary”);
lst.Add(“David”);
lst.Add(“Christopher”);
lst.Add(“John”);
lst.Add(“Isaiah”);
lst.Add(“Levi”);
lst.Add(“Jonathan”);
lst.Add(“Oliver”);
lst.Add(“Chase”);
lst.Add(“Cooper”);
lst.Add(“Tristan”);
lst.Add(“Colton”);
lst.Add(“Austin”);
lst.Add(“Colin”);
lst.Add(“Charlie”);
lst.Add(“Dominic”);
lst.Add(“Parker”);
lst.Add(“Hunter”);
lst.Add(“Thomas”);
lst.Add(“Alex”);
lst.Add(“Ian”);
lst.Add(“Jordan”);
lst.Add(“Cole”);
lst.Add(“Julian”);
lst.Add(“Aaron”);
lst.Add(“Carson”);
lst.Add(“Miles”);
lst.Add(“Blake”);
lst.Add(“Brody”);
lst.Add(“Adam”);
lst.Add(“Sebastian”);
lst.Add(“Adrian”);
lst.Add(“Nolan”);
lst.Add(“Sean”);
lst.Add(“Riley”);
lst.Add(“Bentley”);
lst.Add(“Xavier”);
lst.Add(“Hayden”);
lst.Add(“Jeremiah”);
lst.Add(“Jason”);
lst.Add(“Jake”);
lst.Add(“Asher”);
lst.Add(“Micah”);
lst.Add(“Jace”);
lst.Add(“Brandon”);
lst.Add(“Josiah”);
lst.Add(“Hudson”);
lst.Add(“Nathaniel”);
lst.Add(“Bryson”);
lst.Add(“Ryder”);
lst.Add(“Justin”);
lst.Add(“Bryce”);

//—————female

lst.Add(“Sophia”);
lst.Add(“Emma”);
lst.Add(“Isabella”);
lst.Add(“Olivia”);
lst.Add(“Ava”);
lst.Add(“Lily”);
lst.Add(“Chloe”);
lst.Add(“Madison”);
lst.Add(“Emily”);
lst.Add(“Abigail”);
lst.Add(“Addison”);
lst.Add(“Mia”);
lst.Add(“Madelyn”);
lst.Add(“Ella”);
lst.Add(“Hailey”);
lst.Add(“Kaylee”);
lst.Add(“Avery”);
lst.Add(“Kaitlyn”);
lst.Add(“Riley”);
lst.Add(“Aubrey”);
lst.Add(“Brooklyn”);
lst.Add(“Peyton”);
lst.Add(“Layla”);
lst.Add(“Hannah”);
lst.Add(“Charlotte”);
lst.Add(“Bella”);
lst.Add(“Natalie”);
lst.Add(“Sarah”);
lst.Add(“Grace”);
lst.Add(“Amelia”);
lst.Add(“Kylie”);
lst.Add(“Arianna”);
lst.Add(“Anna”);
lst.Add(“Elizabeth”);
lst.Add(“Sophie”);
lst.Add(“Claire”);
lst.Add(“Lila”);
lst.Add(“Aaliyah”);
lst.Add(“Gabriella”);
lst.Add(“Elise”);
lst.Add(“Lillian”);
lst.Add(“Samantha”);
lst.Add(“Makayla”);
lst.Add(“Audrey”);
lst.Add(“Alyssa”);
lst.Add(“Ellie”);
lst.Add(“Alexis”);
lst.Add(“Isabelle”);
lst.Add(“Savannah”);
lst.Add(“Evelyn”);
lst.Add(“Leah”);
lst.Add(“Keira”);
lst.Add(“Allison”);
lst.Add(“Maya”);
lst.Add(“Lucy”);
lst.Add(“Sydney”);
lst.Add(“Taylor”);
lst.Add(“Molly”);
lst.Add(“Lauren”);
lst.Add(“Harper”);
lst.Add(“Scarlett”);
lst.Add(“Brianna”);
lst.Add(“Victoria”);
lst.Add(“Liliana”);
lst.Add(“Aria”);
lst.Add(“Kayla”);
lst.Add(“Annabelle”);
lst.Add(“Gianna”);
lst.Add(“Kennedy”);
lst.Add(“Stella”);
lst.Add(“Reagan”);
lst.Add(“Julia”);
lst.Add(“Bailey”);
lst.Add(“Alexandra”);
lst.Add(“Jordyn”);
lst.Add(“Nora”);
lst.Add(“Carolin”);
lst.Add(“Mackenzie”);
lst.Add(“Jasmine”);
lst.Add(“Jocelyn”);
lst.Add(“Kendall”);
lst.Add(“Morgan”);
lst.Add(“Nevaeh”);
lst.Add(“Maria”);
lst.Add(“Eva”);
lst.Add(“Juliana”);
lst.Add(“Abby”);
lst.Add(“Alexa”);
lst.Add(“Summer”);
lst.Add(“Brooke”);
lst.Add(“Penelope”);
lst.Add(“Violet”);
lst.Add(“Kate”);
lst.Add(“Hadley”);
lst.Add(“Ashlyn”);
lst.Add(“Sadie”);
lst.Add(“Paige”);
lst.Add(“Katherine”);
lst.Add(“Sienna”);
lst.Add(“Piper”);

str = lst.OrderBy(xx => rnd.Next()).First();
return str;
}
}
}


23
Apr 12


Written By Steve French

 

How to use UI Hints and Display Templates in ASP.net MVC 3

'wtf - code quality measurement' photo (c) 2008, smitty42 - license: http://creativecommons.org/licenses/by-nd/2.0/Surprisingly there is very little information about UI Hints and Display templates for ASP.net MVC 3, so I thought I would share what I have learned.

What are Display Templates?

Display Templates are lovely features of ASP.net MVC 3 that allow you to have a common formatting for certain properties of your objects.  For example, take the belowp roperty (which should be located in your models folder)

[DataType(DataType.ImageUrl)]
[UIHint(“ImageUrl”)]
[Display(Name = “Photo”)]
public string Photo { get; set; }

Everything looks like a standard property, UIHint property.  It would correspond to the Display Template (which is below, it would be name “ImageUrl.cshtml” and would be located in your Views/Shared/DisplayTemplates folder)

@model string
<img style=”display: block;” src=”@Model” alt=”” align=”right” />

What does the UI Hint do?

The UI Hint tells ASP.net MVC 3 to always display the Photo property using the formatting in the ImageUrl.cshtml file.  That way everything is nice, clean and using much less code.

How do you call UI Hints and Display Templates?

Just use the Html.DisplayFor command, for example the Display template below would look like this in Visual Studio 2010

@Html.DisplayFor(xx => xx.Photo)

and would render as

<img style=”display: block;” src=”/img/Photos/Photo123.png” alt=”” align=”right” />

in the browser, allowing you to keep formatting and style consistent across your entire site with very little effort.

 


10
Apr 12


Written By Steve French

 

How to fix the Initialization method TestInitialize threw exception problem in In Visual Studio 2010 Unit Testing

The Problem

You are making an ASP.net MVC 3 web application, and being a good person, you are unit testing as much as possible.  You have properly separated your database from your business objects and every other good practice.  However, when you unit test your controllers, you throw in a fake DBContext (I am assuming you are using the Entity Framework and Moq) and all of a sudden, you get the following error

<b>Failed 1 tests.</b>
Initialization method [YourTestClass].TestInitialize threw exception. System.ArgumentNullException: System.ArgumentNullException: Value cannot be null.
Parameter name: source.

Which certainly makes for one of the least helpful error messages of all time.

The Cause

The problem seems to be lazy loading and the DBSet Feature of Entity Framework.  For example the below passage in your source code below will throw the error

public virtual DbSet<Setting> Settings { get; set; }
public virtual DbSet<AccountSource> AccountSources { get; set; }

This bit of code will not thow the error

public DbSet<Setting> Settings { get; set; }
public DbSet<AccountSource> AccountSources { get; set; }

The problem seems to be entirely in the initialization method.

The Solution

Rather irritating isn’t it?  Especially since Lazy Loading is the design pattern to use, and works great in every other case.  However, if you just delete the “virtual” keyword everything works as intended.

 


03
Apr 12


Written By Steve French

 

How to get a list of the pages Google has indexed on your site

The Problem

'Google Search Coupon: 1 FREE Google Search' photo (c) 2008, Bram Van Damme - license: http://creativecommons.org/licenses/by/2.0/For whatever reason you decide you need to get a list of all of the urls you have on your website.  In my case, I am looking to move a major site to a new platform and the urls will change.  I need a list of all of the pages on the site so I can get a jump on my 301 redirects.  You turn to Google,  specifically Google Webmaster Tools and Google Analytics.  You look around for it forever, but do not find it, even though this is exactly what you think Google Webmaster Tools and Google Analytics should do.  It’s a task you need done, and clearly Google does have a list of all of the urls it has linked on your site.

The Cause

It’s not there.  It should be, but it isn’t.  No idea why they left out that feature.  It would be a vast boon to the world of  internet marketing, search engine optimization and keyword research if they did such a thing, but they do not.

The Solution

You can run a special query, specifically the one listed below which will simply return a paginated list (sadly not in excel spreadsheet form) of every page on your site.

site:digitaltoolfactory.net inurl:digitaltoolfactory.net -.pdf

That’s it!  It’s not the most convenient thing in the world, but it gets the job done.

In the interest of full disclosure, I did not search Bing or Open Site Explorer to look for a list.


30
Mar 12


Written By Steve French

 

How to fix problems with asp.net mvc 3 and jQuery UI AutoComplete

The Problem:

You’re tooling along, using the jQuery library to write some great search feature, and you write some great Linq to Sql code.  Being an awesome coder your use the extension method syntax.  Your action result (in the appropriate controller) looks like this

public ActionResult QuickSearch(string term)
{
var lst = context.Contacts.Select(xx => new {FullName= xx.FirstName +” ” + xx.LastName }).Where(xx => xx.label.ToUpper().Contains(term.ToUpper())).ToList();
return Json(lst,JsonRequestBehavior.AllowGet);
}

And for no good reason, you get no results when you try to search for a contact.  All you get is a blank drop down.  Why has jQuery UI failed you?  Why must javascript code always be so finicky?

The Cause:

For good reason Jquery UI Autocomplete uses JSon for remote data.  It looks for a predefined format, and it wants that name to be called “label”.  It is case sensitive.

The Solution:

Just change your ActionResult to look like this

public ActionResult QuickSearch(string term)
{
var lst = context.Contacts.Select(xx => new {label= xx.FirstName +” ” + xx.LastName }).Where(xx => xx.label.ToUpper().Contains(term.ToUpper())).ToList();
return Json(lst,JsonRequestBehavior.AllowGet);
}

and you will be golden.  Please note I changed FullName to label.  Jquery UI Autocomplete is a very nice and useful tool, but it demands to work on it’s own terms.  Beyond that I have never had a problem with this wonderful bit of ajax goodness.


29
Mar 12


Written By Steve French

 

How to fix the “Could not read metadata, possibly due to insufficient access rights” error in Sql Server 2008

The Problem

You are doing the responsible thing and make a backup before you make any crazy changes to your Sql Server 2008 database.  You choose the option that gives you the most flexibility and decide to script your backup.  Who knows where this backup will eventually go?

You go through the Tasks>Script Database, and you get the following error

Microsoft.SqlServer.Management.Smo.SmoException: Could not read metadata, possibly due to insufficient access rights. at Microsoft.SqlServer.Management.SqlScriptPublish.GeneratePublishPage.worker_DoWork(Object sender, DoWorkEventArgs e) at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e) at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

The Cause

If I had to guess, I would say that the scripting engine is assuming too much in terms of login.  In any case, just try the option below.

The Solution

Instead of having the “Script Entire Database and Database Objects” radio button checked, instead select “Select specific database options” and just check all three options.  That gives you a large text file containing your database scheme and all of the data that your database contains.


21
Mar 12


Written By Steve French

 




Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.