Tuesday, June 24, 2008

Punycode in VB.NET for IDN domains

On one of my web sites I noticed that IDN (Internationalized Domain Names) don't work when I do Response.Redirect in VB.NET.

IDNs are used to allow accented and other special international characters (non-Ascii) to be used in domain names. Since Domain Name Servers (DNS) only support Ascii, any IDN is encoded in Punycode.


To encode a string as Punycode in VB.NET simply do this:


Dim strUrl As String = "http://idn-example-åäö.com"
Dim idn As System.Globalization.IdnMapping = New System.Globalization.IdnMapping
strUrl = idn.GetAscii(strUrl)

Labels: , ,

Monday, April 16, 2007

Unable to convert MySQL date/time value to System.DateTime

After switching from MySQL ODBC driver to the .NET connector I have been getting this error: Unable to convert MySQL date/time value to System.DateTime.

This is when a DATETIME or DATE field contains an invalid value. It turns out that 0000-00-00 which I have used as default value in a DATE column is considered an invalid date. Instead NULL should be used when date/time is not set.

The one solution I found is to add Allow Zero Datetime=True to the connection string, this allows for 0000-00-00 00:00:00 in a DATETIME column. Just add it at the end of your connection string so it looks something like this:
<add key="connectionString" value="Database=your_db;Data Source=localhost;User Id=root;Password=password;Allow Zero Datetime=True;"/>


This however did not seem to work 100% for my code.
In VB.NET I was using this piece of code to check if the valid_until field (of type DATE) is set or not:
If ("" & myDataReader("valid_until")) = "" Then ...

I was concatenating to an empty string istead of using .ToString() because this (if I remember it correctly) would catch both NULL and 0000-00-00 values (both would evaluate to empty string).
Now instead I got this error message: Conversion from type 'MySqlDateTime' to type 'String' is not valid.

With ODBC, 0000-00-00 used to evaluate to empty string, but using the .NET connector with the connection string work around it returns the actual value, so I have to check for the zeroes instead. So, the solution to my problem is of course to use the proper .ToString function and check for both "0000-00-00" and empty string returned for null value (or even better - use the proper IsDBNull function).

Another way besides using the connection string workaround is of course to fix the data.
  • First check the data definitions in your databases to make sure it does not default to 0000-00-00 (for data type DATE) or 0000-00-00 00:00:00 (for data type DATETIME). I'm still running MySQL server version 4 but I think that on version 5, using 0000-00-00 as default value is no longer valid.

  • Then update the data. An SQL statement to update a column of type DATE would typically look like this for a column named valid_until:
    UPDATE your_table SET valid_until=null WHERE valid_until LIKE '0000-00-00';

Labels: ,

Thursday, April 05, 2007

.NET and MySQL in Five Easy Steps

I've been using the MySQL ODBC driver for a long time now in my ASP.NET projects (as posted about here). But today when I was about to start a new project on my new Vista machine I thought I'd check if anything new had emerged the last years when it comes to MySQL drivers for .NET - and not too surprisingly there is now (probably been there for a loooong time) an ADO.NET driver called ADO.NET Driver for MySQL Connector/NET.

So this post is on how to get it going, it's very straight forward.
This is tried on Vista with Visual Studio 2005 SP1 and the Visual Studio 2005 Service Pack 1 Update for Windows Vista using the ADO.NET Driver for MySQL (Connector/NET) version 5.0.6.

1) Download and install (using the included installer) the ADO.NET Driver for MySQL Connector/NET.

2) Create or open a project in Visual Studio 2005. I'm using a VB.NET console project in my example.

3) Go to Project + Add Reference...
On the .NET tab find and hilite MySQL.Data and then click Ok.
(It seems this step can be skipped sometimes...)
[Update: In an ASP.NET project go to Website + Add Reference]

4) Go to Project + YourProject Properties... + References Tab
Under Imported Namespaces check MySql.Data.MySqlClient and MySql.Data.Types and save.
[Update: In an ASP.NET project open web.config and add as namespace under configuration - system.web - pages - namespaces]

5) Write your code and run it. Here is an example to get you started:

Dim myConnectionString As String = "Database=TestDB;Data Source=localhost;User Id=TestID;Password=TestPwd"
Dim myConnection As New MySqlConnection(myConnectionString)
Dim myCommand As New MySqlCommand("SELECT * FROM testtable", myConnection)
Dim myDataReader As MySqlDataReader
myConnection.Open()
myDataReader = myCommand.ExecuteReader
While myDataReader.Read
Console.WriteLine(myDataReader("testcolumn"))
End While
myDataReader.Close()
myConnection.Close()

More examples can be found in the manual from MySQL.

Note: If your accessing your MySQL server on another machine, i.e. not as localhost, you need to make sure the firewall on the server allows access on TCP port 3306. If the server is using Windows firewall just simply go into Control Panel - Windows Firewall - Exceptions Tab click Add Port. (Just make sure you remember to disable the port again after you are done testing!)

Labels: ,

Sunday, March 04, 2007

BC30456: InitializeCulture is not a member of xxx

Lately I've been getting this error on one of my ASP.NET 2.0 web sites: BC30456: 'InitializeCulture' is not a member of xxx

I've been getting it intermittently, ie. sometimes the page works fine and sometimes all I get is this error. I've been troubleshooting it for awhile but it finally looks like I have gotten a grip on it, much thanks to this MSDN thread.

This is what worked for me:
  • Deleted a "Backup" folder that I kept in Visual Studio (but never copied to my production Web Server). In the Backup folder I kept copies if various apsx pages. Apparently you should not have multiple aspx pages referencing the same code file.
  • Deleted the Bin folder on the Web Server before copying a new precompiled version there. Turned out I had lots of outdated bin files on the Web Server.

I have not yet installed Visual Studio 2005 SP1 (Service Pack) maybe this is something that is also solved there.

Labels:

Friday, October 27, 2006

How to Create a Favicon in Visual Studio 2005 ASP.NET Web Project

In Internet Explorer 7 the new tabs feature shows the sites icon, aka favicon.ico. (I know tabs has been around in Firefox and other browsers forever but I've been stuck with IE)

I've been lazy and haven't created favicons for my sites, but after using IE7 for a few days I realized that they're pretty great when you have many tabs open.
So I Googled a little and found out that the favicon.ico file cannot be just any image file, it has to be a 16x16 pixel .ico file (at least if you want it to work in IE, some other browsers support all kinds of images incl animated gifs) and that not that many imaging programs can create .ico files out of the box, but Visual Studio can.

The only problem is that in Visual Studio 2005 for a web project (ASP.NET) you cannot add an item that is of type icon :(

So after some testing and searching I discovered these two workarounds to create a favicon.ico for a web project:

1) Easiest: Use an existing .ico file and just add it to your project. Double-click it and the icon editor fires up.


2) Easy but not as easy: Create a Windows Application project. From here you can create an item of type icon.

If you got a better solution please let me know.

Just make sure your 16x16 pixel favicon.ico is in the root of your web app and it should work. You should also add a LINK tag in the HEAD section of your html: (but this doesn't seem to be necessary)

<link rel="SHORTCUT ICON" href="favicon.ico">

There is plenty of info available on Google on how to implement favicons, this is one good link.

I had to clear my IE7 history to get my favicon to show up and I did read about other people who also had to clear their caches etc.

Labels:

Tuesday, May 30, 2006

How to access MySQL from .NET (ASP.NET using VB.NET)

[Update: Go to this new post instead on how to use MySQL ADO.NET drivers]

This is a three step description of what you need to do to access a MySQL server from VB.NET.

I used Microsoft Windows Server 2003 with Microsoft Visual Studio 2003 Professional and MySQL Server 4.1.7.

It is possible to use ODBC, OLEDB and native .NET providers to access MySQL from .NET. However the ODBC driver (MyODBC) is the only official driver (ie from MySQL) provided for free. The drawback is that ODBC gives the worst performance of the three. More info on this can be found at dev.mysql.com/tech-resources/articles/dotnet.

Ok, so this is how I did it:

1) Download and install the MyODBC driver. Latest version can be found at dev.mysql.com/downloads/connector/odbc. Current version, used in this example, is 3.51.

2) Start a new or use an existing ASP.NET (using VB.NET) project in Visual Studio. You need to import the System.Data.Odbc namespace to the project. From the "Project" menu select "Properties". Under "Common Properties" and "Imports" type "System.Data.Odbc" in the "Namespace" box and click "Add Import".

3) Below is some example code to get started. More examples can be found at dev.mysql.com/doc/mysql/en/odbc-net-op-vb-cp.html.

Dim MyConString As String = _
"DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=localhost;" & _
"DATABASE=testdb;" & _
"UID=root;" & _
"PASSWORD=rootpassword;" & _
"OPTION=3"

Dim MyConnection As New OdbcConnection(MyConString)
MyConnection.Open()

Dim MyCommand As New OdbcCommand
MyCommand.Connection = MyConnection


Happy programming!

(This article is previously published and has been moved to this blog)

Labels: ,

Saturday, May 13, 2006

Failed to update database "XXX.MDF" because the database is read-only

After a few hours of Googling and testing things I finally solved this problem so I thought I'd write the solution down both for my own and other's benefit.

What I did to get the error:
I had just done some changes to one of my web sites using Visual Studio 2005 and ASP.NET 2.0 with VB.NET. I have previously used MySQL as a database for this site but thought I would test using SQL Server 2005 Express Edition for a new feature I just added.
As usual I did Build - Publish Web Site and then copied and pasted the published directory to my web server. When browsing any of the web pages that use the SQL Server 2005 database I get this error message: Failed to update database "XXX.MDF" because the database is read-only.

Solution:
From what I found on Google this seems to be a confirmed bug in the beta releases of Visual Studio but should be fixed in the released version, but obviously it is not. There seems to be many ways and suggestions on how to fix this, some very complicated.

This worked for me:
1) Make sure App_Data directory or any contained files does not have file system attribute Read-only set. (I had some files marked as Read-only)
2) Give user ASPNET and NETWORK SERVICE Modify control over the App_Data directory. (I had to add both these)
3) Run IISRESET to restart IIS to refresh its permissions.


Main part of this solution was found in this MSDN forum thread, specifically posts by Thongtap and Justin. Thanks guys!

Labels: ,