HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.

WordPress and Windows Server – Permission Problems

In the land of one-click installers, where any web host can get a WordPress site running with little-to-no user interaction, sometimes installing WordPress on a Windows server is an exercise in frustration.

While at it’s base level, you can use the Web Platform Installer to install WordPress but I’ve found that it is rarely that simple. With a lot of our clients, they decide that they want a blog to go with their existing non-WordPress site while keeping it on their main domain. This is understandable. I would probably want the same thing.

But what I’ve had to deal with ends up being a fight between user permissions and application permissions just to get WordPress running. While many may not have this problem, but it comes up frequently for me and for some reason I always forget what I did to resolve all my errors. So this time around, this post is not only meant to share with the world, it’s my own reminder when I have to do it again.

The issue arises when I install WordPress in a sub-folder of a parent website. This is because they usually want whatevertheirdomainis.com/blog to be the URL, and I like keeping things organized so I am ok with this.

I usually come across two different problems, and frequently they will both show up depending on my attempts to fix it.

The first error is an actual IIS error that is thrown in regards to the web.config file not having sufficient permissions. Often this error will appear first, and then after I had what I figured was the correct permissions, I end up with the second problem.

The second one is an infinitely loading page. I could leave it for hours and it will never time out, never throw an error, but just make an attempt to load the page forever.

HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.

HTTP Error 500.19 – Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.

The most likely resolution is to make sure that your IIS_IUSRS account has read access to the folder where the web.config is located. for the parent site. While this may not seem to resolve the issue, I found that I had to stop and restart the Application Pool in order for it to recognize the changes.

Some people suggested that you actually need to give the Application Pool created by the blog proper permissions, which you can access in the permissions settings by using IIS ApplicationPool\<applicationPoolName> but I haven’t found this to be the actual problem either time I’ve run into this but it is worth noting.

Microsoft also suggests this error can be caused by a malformed applicationHost.config file but this is highly unlikely to be caused by simply installing WordPress. Especially if you have other sites running already.

Just remember not to give the IIS_IUSRS account write access. And being on Windows, the first time you try to install a plugin or update WordPress, it will most likely ask for FTP credentials as well.

It’s amazing how easily you can forget something as simple as giving proper user permissions. Cheers!

50 States in a control that will work in an ASCX file

Just in case you ever happen to need this (hey, some times the situation arises). Nothing else really to this post. Have a good day!

<div>
 <label>State</label>
 <%= Html.DropDownList(Model + "Region", new SelectList(new[] {
 new { Value = "AL", Text = "Alabama" },
 new { Value = "AK", Text = "Alaska" },
 new { Value = "AZ", Text = "Arizona" },
 new { Value = "AR", Text = "Arkansas" },
 new { Value = "CA", Text = "California" },
 new { Value = "CO", Text = "Colorado" },
 new { Value = "CT", Text = "Connecticut" },
 new { Value = "DE", Text = "Delaware" },
 new { Value = "FL", Text = "Florida" },
 new { Value = "GA", Text = "Georgia" },
 new { Value = "HI", Text = "Hawaii" },
 new { Value = "ID", Text = "Idaho" },
 new { Value = "IL", Text = "Illinois" },
 new { Value = "IN", Text = "Indiana" },
 new { Value = "IA", Text = "Iowa" },
 new { Value = "KS", Text = "Kansas" },
 new { Value = "KY", Text = "Kentucky" },
 new { Value = "LA", Text = "Louisiana" },
 new { Value = "ME", Text = "Maine" },
 new { Value = "MD", Text = "Maryland" },
 new { Value = "MA", Text = "Massachusetts" },
 new { Value = "MI", Text = "Michigan" },
 new { Value = "MN", Text = "Minnesota" },
 new { Value = "MS", Text = "Mississippi" },
 new { Value = "MO", Text = "Missouri" },
 new { Value = "MT", Text = "Montana" },
 new { Value = "NE", Text = "Nebraska" },
 new { Value = "NV", Text = "Nevada" },
 new { Value = "NH", Text = "New Hampshire" },
 new { Value = "NJ", Text = "New Jersey" },
 new { Value = "NM", Text = "New Mexico" },
 new { Value = "NY", Text = "New York" },
 new { Value = "NC", Text = "North Carolina" },
 new { Value = "ND", Text = "North Dakota" },
 new { Value = "OH", Text = "Ohio" },
 new { Value = "OK", Text = "Oklahoma" },
 new { Value = "OR", Text = "Oregon" },
 new { Value = "PA", Text = "Pennsylvania" },
 new { Value = "RI", Text = "Rhode Island" },
 new { Value = "SC", Text = "South Carolina" },
 new { Value = "SD", Text = "South Dakota" },
 new { Value = "TN", Text = "Tennessee" },
 new { Value = "TX", Text = "Texas" },
 new { Value = "UT", Text = "Utah" },
 new { Value = "VT", Text = "Vermont" },
 new { Value = "VA", Text = "Virginia" },
 new { Value = "WA", Text = "Washington" },
 new { Value = "WV", Text = "West Virginia" },
 new { Value = "WI", Text = "Wisconsin" },
 new { Value = "WY", Text = "Wyoming" },
 new { Value = "DC", Text = "District of Columbia" },
 }, "Value", "Text", Model), new { @class = "stateList"})%>
</div>

How to use Stored Procedures

I’ve met quite a few folks who knew what stored procedures were but never actually had to write code to call one themselves. So this is just a quick run-down of what you need to do to call a stored procedure from your database. You’ll see it’s not so bad once you get into it.

What we are going to do is create a simple button that when clicked will call a stored procedure and return results based of a parameter that we supplied through a textbox.

We have some basic elements on an aspx page like this:

<asp:TextBox ID="testData" runat="server"></asp:TextBox>

<asp:Button runat="server" ID="Submit" Text="Submit" 
    OnClick="CallStoredProcedure" />

In your C# file you need to remember to add these references at the top:

using System.Data;
using System.Data.SqlClient;

That way we have access to the SQL tools we need to do this.

Let me just throw out the entirety of the code we will be using and then we will go over each element.

public void CallStoredProcedure(Object sender, EventArgs e)
 {
 // Connection String to Test Database
 String connectionString = "Data Source=localhost;Initial Catalog=Web_Test;UID=testUsername;Password=testPassword";

SqlConnection myConnection = new SqlConnection(connectionString);
 myConnection.Open();
 SqlCommand myProcedureTest = new SqlCommand("my_Stored_Procedure", myConnection);
 myProcedureTest.CommandType = CommandType.StoredProcedure;

 // Add user variables to Stored Procedure Execution
 myProcedureTest.Parameters.Add(new SqlParameter("@Test_Data", testData.Text));

// Execute the Stored Procedure
 SqlDataReader rdr = myProcedureTest.ExecuteReader();
 while (rdr.Read())
 {
    var returnedValue = rdr[0];
    // Do something with returnedValue
 }
}
 String connectionString = "Data Source=localhost;Initial Catalog=Web_Test;UID=testUsername;Password=testPassword";

So the first bit we are looking at is where we define our connection string to our database. These credentials will have to be provided to you if you are not in charge of your databases. Instead of localhost you might receive an IP address or a number of different ways to reach your database, followed by the credentials.

SqlConnection myConnection = new SqlConnection(connectionString);
 myConnection.Open();

The next section is where we create the SQL connection using the connection string we just created. It uses the credentials and information you provided to actually establish a connection to said database. The Open command establishes a connection to the database.

 SqlCommand myProcedureTest = new SqlCommand("my_Stored_Procedure", myConnection);
 myProcedureTest.CommandType = CommandType.StoredProcedure;

Here we are defining the SQL query we want to run and then tell it which database connection we want to use (in case we have more than one). We then clarify that what we are using as a SQL command is actually a stored procedure on the server.

 myProcedureTest.Parameters.Add(new SqlParameter("@Test_Data", testData.Text));

In this example, we are assuming that our Stored Procedure takes a single parameter which we pass in from our text box. In a real application, we would want to be more careful with blindly accepting user input but since this is just an example, we will just roll with it.

 SqlDataReader rdr = myProcedureTest.ExecuteReader();

This creates an object to actually “listen” for data being returned from the stored procedure after we execute everything.

 while (rdr.Read())
 {
    var returnedValue = rdr[0];
    // Do something with returnedValue
    myConnection.Close();
 }

Finally we do something with the returned data. This bit of code essentially says “while there is data to work with, do whatever code we put here and then close the connection to the database.” We are assuming that the stored procedure is only returning a single piece of information (or that we only care about the first piece) based off the “rdr[0]” code. You are responsible for knowing what to do with the returned data and use it how you see fit.

Obviously this tutorial wasn’t exhaustive on the topic of stored procedures by any stretch of the imagination but if you need to begin working with them, this should at least help you get on the ground running.

If you have any questions or comments, feel free to leave them below!

Using 32-bit DLLs on a 64-bit server

The old servers at my work used a number of DLLs that were either generated in-house or purchased/downloaded from a third-party. After upgrading our servers to 64-bit machines we still had the need to support these DLLs for the large number of sites still using code from these DLLs.

After registering the DLLs with the server, we were still having problems getting things to work properly. It just so happened that I stumbled across someone mention the issue with 32-bit DLLs on a 64-bit server and how to get IIS to support them.

Ultimately it was as simple as opening a Command Prompt (ran as Administrator of course) and using the command:

%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.enable32BitAppOnWin64:true

The %windir% is ultimately just your Windows installation which will more often than not just be C:\Windows

So if you have to support older DLLs, keep this in mind as you will probably need to do this to get them to work properly.

The compiler failed with error code 1073741502

This is a fun error you may never see while working on a Windows Server. I came across the other day when I arrived at work and unfortunately I still can’t pinpoint what caused it to happen but I can talk about what I researched and what we ended up having to do to fix it.

We had a couple sites on our development server (thank goodness it wasn’t live) that started producing this error. The obvious first steps were to do things like recycle the application pool, restart the site, and even restart IIS. Some reports from people online said that this fixed their problem.

Alas I was not so lucky. The next step was to venture into C:\Windows\Microsoft.NET\Framework\ and into the appropriate framework and then into the “Temporary ASP.NET Files” and clear this folder out. In this case the sites were all running on .NET Framework v2.0.50727 but it is safe to clear out this folder anyways. The files that reside in here are just the compiled files being served up to the end-user and if they don’t exist, they are simple recompiled and served up fresh!

At first I just deleted the files being referenced in my specific error but when that didn’t fix, I just cleared the folder. Definitely not something I would do on a production server in case what happened actually happens. After I did clear out the entire temporary folder none of the sited would load and I was getting the 1073741502 error across the board.

Terrible, but it did offer a clue. None of the sites could be recompiled. Some more searching suggested that the Identity in IIS might not have sufficient permissions to run the compiler. A good thought but since this wasn’t a new server and I know it had been compiling that this wasn’t the problem (though I checked anyways for the sake of being thorough).

I then went and checked csc.exe, which happened to be the C# Compiler. Wouldn’t you know it, Windows was reporting that the file was 0 bytes in size. Ultimately we ran the .Net repair tool because it seems that our installation became corrupted. After this finished the sites worked fine, everything recompiled, and all was right in the world again.

Doing follow-ups, some people reported different ideas of what could cause it and the most prevalent one was possible hardware failing. As this is our development server only and not our back-up or production server it doesn’t always have the shiniest of hardware. We ran a back-up of everything just in case and the problem will be further investigated by the powers that be.

So if you ever come across a 1073741502 error:

  1. Restart the website in IIS
  2. Recycle the Application Pool
  3. Restart IIS
  4. Try deleting the files within the Temporary ASP.NET Files folder
  5. Be sure that if you are using a custom identity that it has proper permissions to run things like csc.exe
  6. Check for signs of a corrupted installation (files with no size, etc)

There are probably more effective ways to check this information but solid suggestions about how to handle this error were few and far between so it made for an eventful morning!

Quick Tutorial on using Session Variables

This is a down and dirty lesson on how to use Session Variables in C#. It won’t take long in your programming life that you will want an easy way to pass data from one page to another. Instead of using query strings (which also have their place in the world) or other methods, we can use these variables.

In your code behind file, wherever you want to add to the current session, you can simple add the line:

Session["yourVariableName"] = valueOfYourVariable; 

The “valueOfYourVariable” can pretty much be whatever you want. Strings, integers, variables, and so many other kinds of data.

On the page that is going to receive the data, pulling that information is as simple as using the following code:

string loadedData = (string)Session["yourVariableName"];

Obviously you need to cast your variable on the receiving page as the same data type as you saved it on the previous page, but other than that, it really is that simple to pass data between pages.

Any other complex data types you would just treat as you would normally once you load them.

 

Combining Advanced CSS Selectors

If you are trying to familiarize yourself with all the cool tricks that CSS3 can offer, I cannot recommend another post as highly as this one from Smashing Magazine.

One cool thing that you can do with advanced CSS selectors is chain them together to create amazingly advanced selections within your documents. This post is just a quick overview of selection tricks you can do to make your life a bit easier at times when you come across difficult styling issues.

We are going to use this code as a sample:

<div class="second third fourth">Words 1</div>
<div class="first second third">Words 2</div>
<div class="first second third">Words 3</div>
<div class="first second third fourth">Words 4</div>

As a quick example, if we write the following CSS:

div[class*="fourth"]{
background-color:#666666;
}

This CSS rule will select any div that has the class “fourth.” Our result will look like this:

Using one styling ruleIf we modify that rule to look like the following:

div[class*="fourth"][class^="second"]{
background-color:#666666;
}

We will get the results that look like this:

A CSS rule with 2 selectors

Using the ^ symbol as we did above is the rule condition that states that is must being with the specified content. In this case, the class of the chosen element must begin with the word “second.”

I won’t make this post very long, but you can easily chain these advanced selectors when you need to hit specific elements in your page. Especially if elements are dynamically created.

Questions or comments, ask below!

Creating a WordPress Plug-in to customize the Dashboard

I’ve used many different plug-ins to accomplish the simple goal of modifying the dashboard to match the look and feel with the rest of the site. Some worked, some didn’t. And some were needlessly complicated. I thought “I bet this would be much easier if I just wrote my own custom CSS.”

But I didn’t want to mess with any core files, so I figured “Hey, if these plug-ins were loading additional CSS, I can too.” So I spent a bit time looking into how you write a plug-in and found it was really easy to load a CSS file.

This walk-through will go through the simple steps of creating your own WordPress plug-in that loads your own CSS file to the dashboard.

The first step we need to do is to create the folder for your plug-in and the default file. I created my plug-in for The Recipe Folder, so my folder is called “recipe-folder-admin-theme” and the default file is called, you guessed it, “recipe-folder-admin-theme.php”.

The contents of this file is similar to creating your own theme. In a php code tag add something like this:

/*Plugin Name: The Recipe Folder Admin Theme
Plugin URI: http://www.therecipefolder.com
Description: Custom Recipe Folder Admin Theme
Author: Daniel Gray
Version: 1.0
Author URI: http://demo.therecipefolder.com*/

Adjust to match your details. This adds the necessary information for WordPress to identify your new plug-in. To make the Dashboard load your custom CSS file, add the following lines (extra line breaks added to keep the post within the column of this post):

function my_wp_admin_css() {
echo 'css"
type="text/css" />';
}
add_action('admin_head','my_wp_admin_css');

As you can imagine, the CSS file can live pretty much wherever you want. You could host it remotely if you want to distribute changes automatically, though if you release it as a plug-in to people, they may not be fond of loading content from another source.

The add_action with the flag “admin_head” tells the plug-in where the function should be executed. In this case, the my_wp_admin_css function simply echos a link to a CSS file in the header of the admin page, which is the dashboard.

After that, it’s creating your CSS file and adjusting style to your heart’s content. Power up Firebug and start going to town.

A customized dashboard

Any questions or comments, be sure to leave them here!

Columns

Faux Mega Menu in WordPress

Sometimes you have so many items in your WordPress blog’s navigation that you feel that it’s far too long. But if they are in the proper logical division splitting them into other menus is not be an option.

There are fantastic tools to create “Mega Menus” with jQuery and CSS such as this one. But on the standard WordPress menu you find in themes like 2010 or Thematic, implementing can be a challenge sometimes.

Luckily for us we can use CSS3 for a quick fix to make your drop down menu split itself into columns. The most obvious answer is to use the new “Columns” property but unfortunately it is not widely supported, even among modern browsers.

For reference, in this example I am styling a drop down menu in Thematic.

.sub-menu{display:inline-block; width:500px !important;}

.sub-menu li{width:50% !important;}

.sub-menu li a:hover{color:#FFFFFF;}

.sub-menu li:nth-child(even){float:right;}

All we are telling it to do is to make the sub-menu 500px wide, make every list item 50% of that, and using the nth-child property, tell every even-numbered element to float right.

Making it 3 columns would be as easy as dropping the li width to 33% if we wanted. Applying styles is easy as well. If we wanted a border dividing the left and right column (if mine didn’t already have borders) we could further edit the nth-child(even) and give it a left border, or make a new rule for nth-child(odd) and give it a right border.

Super easy!

Widgetizing the Footer

Widgetizing the Footer

I really like this theme for WordPress and wanted something not so graphically intensive as Behemoth Gaming. However I wanted to add some widgets to the footer and this theme by default does not have a widgetized section.

Luckily for us that is easily solved. So how do you add a widget-ready section to your WordPress theme? Let’s show you how to do it!

The first thing I would recommend is make a child theme so that if your original theme gets updated your changes are not lost. But we aren’t covering that in this post so we will assume you either have a child theme or are confident you won’t overwrite your changes.

Using your choice of PHP editor (you can also easily do this from the Appearance Editor menu in WordPress if you wish) navigate to your functions.php file. What you are looking for in this file is some code that looks like “register_sidebar”. This tells your WordPress them what widget sections you have. In this theme the code I was looking for looked like this:

register_sidebar(array(
'id' => 'right-sidebar',
'name' =&gt; 'Right Sidebar',
'before_widget' => '<aside>',
'after_widget' => '</aside>',
'before_title' => '<h3>',
'after_title' => "</h3>",
));

This adds my sidebar so I can use the handy drag-and-drop widgets. In that same block of code (actually I did mine in my child theme but either way) I added this chunk of code which is very similar:

register_sidebar(array(
'name' => 'Footer',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));

We have a couple options here. First is the name, which is the name of the widget section that we will be adding to the rest of our code in a minute. When you look to add widgets to this section, this is the name of the container as you drag them around.

The before_widget is any chunk of HTML you would like to be placed before your widget. As you can see, this is usually some opening tag but can also include anything you want. Want an image before every widget? You can do that too.

The other three options should be self-explanatory as well. These give you fairly robust control over the appearance of your widgets.

Now that we have defined the style and name of our widgetized section, we actually need to add the location for it to live within our template files. Within my footer.php I added the code:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Footer") ) : ?>
<?php endif; ?>

Widgetizing the Footer

Change the word “Footer” to whatever you named your new section in your functions.php file and save it. Provided all the code is correct you can now browse over to your Widgets menu and start adding!

Let me know if you have any problems or corrections!