Archive for the ‘Programming’ Category.

Preserving the case of table names in MySql on Windows

Windows is case insensitive, so table “test” and “Test” refers to the same table. However if you want to preserve the case sensitivity of the your table names on a Windows machine, you should set the option lower_case_table_names to 2 in the MySql configuration file. You can check the value by running the following SQL command:

SHOW VARIABLES;

Scroll down to lower_case_table_names and the value will most likely be set to 1. Now to change that value, you will either have to modify my.ini or my.cfg file found in your installation of MySql (C:\Program Files\MySql\MySql Server 5.1 0r C:\Program Files (x86)\MySql\MySql Server 5.1). Try searching for lower_case_table_names and change the value to 2 or just append the following in the my.ini file:

lower_case_table_names = 2;

Now you will need to restart the MySql service for the changes to take effect. Note that tables which have previously been created won’t be affected by this change. Only new tables will preserve their case sensitivity rather than having their case lowered.

After making the change, if you still find that some tables are not preserving their case (same problem that happened to me), then you will need to do rename the table to something else and then back to its original name. Say you created a table called ‘Article’ and you find that it’s showing as ‘article’, then do the following:

RENAME TABLE `Article` to `Article2`;
RENAME TABLE `Article2` to `Article`;

This will solve the problem.

Inserting content into a webpage from iframe code behind in JQuery

Here’s the situation: A user clicks a button on a webpage and an iframe pops up (facebox which similar to lightbox). Now I want to inject some html from the asp.net code behind of the iframe into the parent window. I’m using JQuery to achieve this and this simple line of code does the trick:


parent.$("#MessageDiv").append("hello world");

That works fine except when you’re trying to insert a string which contains line breaks. I’m not talking about html line breaks here but windows line breaks as in new line and carriage return (\n\r). The problem is that because the string will span on several lines, the expression will be unterminated. Have a look at the following to understand what I’m trying to say more clearly:

hello world

how are you?

The above text is represent as “hello world\n\rhow are you?”. When that is written in a javascript script, it will become like this:

document.write(“hello world

how are you?);

To make this work, you need to remove the line breaks as follows:


string myContent = "hello world\n\rhow are you?";

myContent.Replace("\n", "").Replace("\r", "");

This will keep all your content on one line and make the javascript work as intended.

How to scroll parent window from iframe using JQuery

First off I’m using JQuery 1.4 and I’ve got the ScrollTo plugin referenced in my webpage. In order to provide a rich experience to my users, I wanted to load an iframe through facebox when someone submitted a comment (this is not implemented on this website btw) and ask them to provide their email address for follow-ups. If they supplied their email address, I wanted to close the iframe and insert a message in the parent page to tell them that an email has been sent to them.

Now the user can be at the bottom of the page when they submit the comment and the message will be appended at the top of the parent page. So I need to scroll to the top of the page so that they can see the message. I tried the following:


parent.$.scrollTo($("#FeedbackDiv", 1000);

But that didn’t work and it took me quite a while to figure out what went wrong. When on the iframe, parent.$.scrollTo successfully finds the scrollTo plugin but it doesn’t find the FeedbackDiv because it’s located on the parent page and not the iframe. The way the code is at the moment, it’s looking for a FeedbackDiv in the iframe and not the parent window. So to achieve what I wanted, all you have to do is the following:


parent.$.scrollTo(parent.$("#FeedbackDiv"), 1000);

And now everything works perfectly :)

ASP.NET HTML encoding attributes in server controls

Did you know that asp.net will html encode your server control attributes as from .NET 4.0? This is done for security reasons and prevent cross site scripting attacks. Going forward, you will need to work around this if the need arises as a security enhancement is more likely not to be backwards compatible.

I came across this “problem” when I was trying to build a degradable web app. When javascript is turned off, everything would work as it’s supposed to (through postbacks) but is JS is enabled, then JQuery will kick in. For this to work, I needed to provide an OnClientClick function to the asp.net buttons. It works fine until you try to insert parameters (arguments) in the javascript function. Since javascript parameters are enclosed with apostrophes, you will find the rendered html quite different from what you entered in the code behind. Take for example the following:


btnSubmit.OnClientClick = String.Format("PostContent('{0}', '{1}', '{2}');", "6", _question.Id, "0");

This will be rendered as:

<input type="submit" name="btnSubmit" value="Submit" onclick="PostContent(&#39;6&#39;, &#39;10112&#39;, &#39;0&#39;);" id="btnSubmit" />

The attribute value has been html encoded. If you view the html source code, you will see that the apostrophe has been converted to ‘&#39;’ Now that’s not really a problem as your javascript functions will still work as normal. However it just looks a bit weird! If you want to change that behaviour of asp.net to html encode server control properties, then you will need to create a class as follows:

public class HtmlAttributeNoEncoding : System.Web.Util.HttpEncoder
 {
 protected override void HtmlAttributeEncode(string value, System.IO.TextWriter output)
 {
 output.Write(value);
 }
 }

And in your web.config file:

<httpRuntime encoderType="HtmlAttributeNoEncoding"/>

It’s probably worth doing that if you need backwards compatibility with the previous .net frameworks (prior to .net 4.0).

HttpHandlers and Sessions in ASP.NET

After struggling for a little while with sessions in an http handler, I found that you need to implement either the IRequiresSessionState (if you need read/write access to sessions) or IReadOnlySessionState (for read only access) to be able to work with session in the asp.net httphandler. You will also need to add the System.Web.SessionState namespace to get it working.

I was trying to read a cookie and restore a particular session from an httphandler but without implementing the IRequiresSessionState, it did not work. So this is a nice little reminder for me!

Connecting to MySQL database using ASP.NET

I decided to use MySQL instead of SQL Server because of the costs involved in getting a licence for Microsoft SQL Server. So although the programming language I’ve decided to stick to remains C# (asp.net), I’m moving away from SQL Server. I’m also using nHibernate which will make it easier if I need to switch to another data store.

Ways to connect to MySQL in .net

There are 3 ways to connect to MySQL database from a .net web application and they are Odbc, MySQL native .net providers or OLEDB.net. The first two are most common and there are advantages and disadvantages to both.

Using ODBC.NET

You need to download the MySQL ODBC driver – MyODBC 3.51 from here. Once this is installed, you can set up a DSN (Data Source Name) if you want.

You can now start using the odbc driver by editing your web.config file and adding a connection string for your MySQL database as follows:


string connectiongString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=mydatabase;UID=myusername;PASSWORD=mypassword;OPTION=3";
 OdbcConnection con = new OdbcConnection(connectiongString);

 con.Open();

 OdbcCommand cmd = new OdbcCommand("SELECT * FROM tblCustomers", con);
 OdbcDataReader dr = cmd.ExecuteReader();

 while (dr.Read())
 {
 // read the data here
 }

 cmd.Dispose();
 con.Close();
 con.Dispose();

You may need to add a reference to the System.Data.dll and import the System.Data namespace to get this to work.

The advantage of using ODBC is that it is the standard way of connecting to databases, it is not database specific, which means that if you change from MySQL to Oracle or PosgreSQL, your code will still work. The main drawback of this approach is that it is rather slow because of the many layers involved and performance suffers.

Using MySQL native .Net Providers

You will need to download the MySQL .NET provider from here. Once this is installed, you can write sample code as follows:


string connectionString = "Database=mydatabase;Data Source=localhost;User Id=myusername;Password=mypassword;"
string commandString = "DELETE FROM Session WHERE Expires < ?Expires";
MySqlConnection conn = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand(commandString, conn);
cmd.Parameters.Add("?Expires", MySqlDbType.DateTime).Value = DateTime.Now;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

You will need to import the MySql.Data.MySqlClient namespace to get the code to compile. The MySQL .net connector is the same as the ByteFX connector for MySQL. MySQL bought ByteFX and renamed it to MySQL .NET connector. You can also use CoreLab.MySql available from CoreLabs but that connector is not free.

There are many benefits to using a native .net connector instead of ODBC and the main reason is performance gain. There’s the speed, garbage collection and security as well as database specific things that you can use. The only drawback is that if you later decide to change from MySQL to something else, you might need to rework your code.

Using OLEDB.NET

You can use OLEDB in the same way as ODBC. You need to download MyOLEDB provider (MyOleDB 3.0) and install it on your machine first. Then you should edit your connection string as follows:

Provider=MySQLProv;location=localhost;Data Source=mydatabase;User ID=myusername;Password=mypassword;

I prefer to use a native .net connector and I’ve installed MySQL Server 5.1 on my Windows 7 machine to get it working.

MySql Error Code 1050 – Table ‘tablename’ already exists

I was playing around with some table creation and deletion and I noticed that after issuing a DROP command for a particular table, I was not able to recreate the table. I’ve installed MySQL on my Windows 7 laptop and by default tables are created with the InnoDB engine. This is how the problem occurs – you create a table (testtable) and immediately after that you issue the following command:

DROP TABLE testtable;

You will get a command executed successfully but when you try to create a table with the same name, you will get the following error:

MySql Error Code 1050 – Table ‘testtable’ already exists

Now if you look at your database schema, you will not find the table name and the same applies if you issue a “SHOW TABLES” command. If you look at the directory where MySQL data are stored, you will not find the .frm file for that specific table. The location is C:\ProgramData\MySQL\MySQL Server 5.1\data (not that it’s not Program Files or Program Files (x86) but ProgramData on Windows 7.

To solve the problem, you will need to restart the MySQL service. Once MySQL is restarted, you can create a table with the same name as the table you dropped earlier. If you drop another table and want to recreate a table with the same name, you will have to stop/start MySQL service again. I think MySQL stores a dictionary of the tables created and only checks whether they are still valid at a regular interval or when the service is restarted.

Writing my first Python program on Windows 7

Let me tell you first how I got into writing my 1st python program. I wanted to have a look at the database schema of OSQA (Open Source Question Answer) and there was no database model available online. So my only other choice was to download the source code and setup the application. Now OSQA runs on Django, python and MySql. I already have a running instance on MySql on Windows 7. So I had to download python and installed it on Windows. I got an MSI installer from the official python website and it installed with no problems. OSQA requires you to run manage.py syncdb -all to create the database it requires. I tried that command in IDLE but it complained about Django. So I went to download Django framework and got it installed as well. Since OSQA source files was in a different directory from where python was installed, I was having a hard time trying to get the command to work. Therefore I tested a line command from the IDLE command prompt (print hello world). This worked which means the python interpreter is OK. I then saved this code in a file called hello.py and saved that file in the OSQA folder. Surely this must work now but it didn’t. You have to add python to the Environment variables on Windows to ensure it works correctly. I also added the Scripts folder to environment variables (C:\Python27\Scripts). Once that is done, hello.py worked fine. So this was my first Hello World python program.

Now my problem was still not solved. The manage.py program would still not run and when I checked the errors, I saw that it was looking for MySql library. MySql for Python was required and a few other libraries as well to make OSQA work. I was only going to add the libraries which are required to get the database schema, so I downloaded MySql for Python and tried to install it on Windows 7. Big problem! The list of problems are as follows:

  1. If you don’t have development bits for MySql, it won’t work. So modify your current instance of MySql and check that development bits are selected. After successful install, you will have an include folder in your current MySql installation path with .h files.
  2. Second problem is that on Windows 7, the registry for MySql is located at SOFTWARE\Wow6432Node\MySQL AB\MySQL Server 5.1 instead of SOFTWARE\MySQL AB\MySQL Server 5.1 because Windows 7 is 64 bits rather than 32. So make sure the site.cfg file of MySQL-python-1.2.3 has the above registry and change MySQL Server 5.0 to 5.1 if you’re running MySQL 5.1
  3. Third problem is that you need to modify your C:/Python27/Lib/distutils/msvc9compiler.py and add ld_args.append(‘/MANIFEST’) after ld_args.append(‘/MANIFESTFILE:’ + temp_manifest). Make sure you edit this file in IDLE (the python scripter) otherwise you’ll get errors because of white space. It will complain about indentation. I tried editing this in Notepad++ and although it looked fine there, when I open the file in IDLE, there was spacing problems.
  4. Fourth problem is that MySQL-python-1.2.3 requires Visual Studio 2008 and since I’m running Visual Studio 2010, it’s not getting the right libraries to install MySQL for Python.

I’ve left it as that for the time being because it’s just too much work for something that’s not really necessary!

UPDATE : I managed to install MySQL-Python-1.2.3 through an installer instead (http://www.codegood.com/archives/129)

UPDATE 2 : On top on MySql for Python, I had to install Markdown and Html5lib as well to get the OSQA database created.

The breakpoint will not currently be hit – Visual Studio Debugging

Debugging your application is a great way to find the cause of a problem. However you may sometime encounter the dreaded “The breakpoint will not currently be hit. No symbols have been loaded for this document.” error. You can find fixes to this problem by following this link.

If like me, none of the above methods work for you, then there could be something else that’s preventing the debugger from working. So I tried creating a new application (web) using Visual Studio 2010 and set a breakpoint to see if it works. As soon as I start debugging from VS2010, the ASP.NET Web Development server kicks in and tries to open the webpage and then switches back to visual studio on my breakpoint. This means that debugging should work fine. I tried it on Firefox and Internet Explorer but it doesn’t work though. By that time, I’ve come to the conclusion that the debugger is not attaching itself correctly to the web server which is hosting my application. Note that the default web server that comes with visual studio opens the webpage either in Firefox or Internet Explorer but uses something like localhost:45609 to accomplish that.

My web app was hosted on IIS7.5 under Windows 7 and I had to attach the debugger to w3wp.exe worker process responsible for asp.net requests. Once this is done, the breakpoints work like a charm.

Should we ditch .net for open source?

I’m a .net guy and love programming in c#. It’s rare for me to look at other programming languages because I tend to stick to aso.net because I can easily get a project off the grounds as I know pretty much the inside outs of the syntax etc. I can code in php as well but I never really liked it as I get more control in .net. From a developer’s point of view, coding in the latest technology is the best way to stay motivated and learn the new stuff. That’s what we want cutting edge technology! However from a business perspective, it’s a different story. For the business, profit is what dictates its strategy. If there are no real benefit to using a certain technology, then most likely it’s not going to be adopted.

For a small company, microsoft products can be quite expensive. For easy programming in .net, you really need to have Visual Studio. Of course you need IIS as your web server and many .net programmers will want Microsoft SQL Server as well. This is all good but the costs of these things can amount to a lot. So you small business owners ditch .net for open source technologies? Well that really depends on the situation. Apache is a good webserver and is free as well as MySql. The standard approach is to use PHP to develop the website and host it on a linux box which is considerably cheaper than having to pay the licence for Windows Server (2008) and getting Microsoft SQL Server 2005/2008. With the Windows Server, you get IIS as your web server to run your website but you still need a database. MS SQL Server is very expensive, it costs pretty much the same as a dedicated server.

When I was looking at a dedicated Windows Server, you have to pay £99 per month for the server, £25 a month for Windows 2008 Standard Edition (the licence fees) and £99 for Microsoft SQL Server 2005/2008 Standard Edition (the licence fees). Now these are significant costs as you end up paying £223 per month for a .net site compared to £99 a month for a site done in PHP running on Apache with a MySql database.

When I thought about the costs involved, I wantet to switch to open source technologies so that I don’t end up paying so much money for my websites. I was looking into Django as it claims to make website designing quicker and better but after my extensive research, I came to the conclusion that it was not really worth it. As I explained in the post on DJango, there are a lot of things to consider before making such a decision. For example, putting aside the differences in programming syntax for Django (python) vs asp.net, I conclude that it’s going to take me more time to deploy a website using Django because of various configuration needed at the server level. I think that an IDE is really important when programming. Take Visual Studio for instance, you have intellisense which makes it quicker for you to code. You can go to a method’s definition to see what’s happening there and have debugging facility as well. You can write unit tests to see whether things are working as expected. You get to compile your website and see errors beforehand. I’m probably biased but I think I’ve got more control with .net than the open source languages.

There are a lot of advantages when programming in asp.net and it is a true object oriented programming language (C#). However I still find that Microsoft SQL Server is very expensive. I wouldn’t change C# as my main coding language but I’ll definitely look into an open source database like MySql or postgresql. The reasoning behind this is very simple; I do not need the special capabilities of MS SQL Server at the moment, I simply need it as a storage engine. I use nHibernate for the database abstraction and that makes it even easier to switch to another data store. At the end of the day, you have to pay to get a good product with all the facilities that you want. Just like you pay for MySql if you want their extended support, you need to pay Microsoft for using their technology!