asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

A Step-by-Step Guide To Using MySQL with ASP.NET - Part 3
By Ziran Sun
Rating: 3.5 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    The first part of this article illustrated how to install and configure the MySQL Database Server, install and use the MySQL Administrator, create a new database, and create and populate a new sample table with some sample data.

    The second part covered creating a MySQL user account, granting that user access to the database, and examined the basic steps required to connect to the database from an ASP.NET web page.

    When it was written, part two was designed to be the end of the article. However, since then I've received a number of email messages requesting additional information. Therefore this part will attempt to address the questions raised in those messages and share what I've found while researching them.

    Please note that the code listings below assume that your tables and users are set up as outlined in the earlier parts of the article. The information in this part may be useful even if you haven't read parts one and two, but if that's the case, it is highly unlikely that the code will actually run without some minor tweaks. For example, I assume most people who haven't followed along with the earlier parts won't have a database named "mydatabase" and most certainly wouldn't have a user named "15secs".   ;)

    Namespace Not Found Errors

    As I mentioned in part two, I ran into a problem where I was unable to get my ASP.NET pages to access the MySQL Connector/Net namespace without placing a copy of the MySql.Data.dll file into my application's /bin directory. Well, a reader was kind enough to send me the following email explaining the situation and so I thought I should pass it along.

    Hello Ziran,

    I was reading [A Step-by-Step Guide To Using MySQL with ASP.NET], and in the second half of the tutorial you are having a problem that Mysql.Data could not be found. The solution you give of putting the .dll in the bin directory is just fine but there is a more 'official' manner for this.

    You need to change the machine.config file of the server and add a reference to the assembly in the GAC or, if you are using the latest .NET Framework, you can make this reference in the web.config file. You can find it here:
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG

    You need some information about the MySql.Data assembly in the GAC, which you can obtain by typing the following at the SDK Command Prompt:

    gacutil -l Mysql.Data

    With this information you can put sometime like this in the web.config file:

    <add assembly="MySql.Data, Version=1.0.7.30072, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />

    And then is it works like magic.  :)

    Please tell me if I can do something for this great community. I like to be involved. I am from The Netherlands, Europe.

    Regards,
    Rene Veenstra

    I hope this helps explain things a bit and in response to Rene, you already have done something for the community... keep up the good work!

    A C# Version of the Sample ASP.NET 1.x Page to Query a MySQL Database

    I didn't realize how many people were actually using C# instead of VB as their language of choice for .NET development. One of the most popular requests I received was for a C# version of the MySQL.aspx page that I published in part two. While C# is not my development language of choice, I was able to get a C# version of the page working and am including the listing for it here for your reference.

    MySqlC.aspx

    <%@ Page Language="C#" Debug="true" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="MySql.Data.MySqlClient" %>
    <script runat="server">

    protected void Page_Load(Object sender, EventArgs e)
    {
        MySqlConnection myConnection = new MySqlConnection(
            "server=localhost; user id=15secs; password=password; database=mydatabase; pooling=false;");

        String strSQL = "SELECT * FROM mytable;";

        MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(strSQL, myConnection);

        DataSet myDataSet = new DataSet();
        myDataAdapter.Fill(myDataSet, "mytable");

        MySQLDataGrid.DataSource = myDataSet;
        MySQLDataGrid.DataBind();
    }

    </script>
    <html>
    <head>
        <title>Simple MySQL Database Query</title>
    </head>
    <body>
        <form runat="server">
            <asp:DataGrid id="MySQLDataGrid" runat="server"></asp:DataGrid>
        </form>
    </body>
    </html>

    You'll have to pardon me if the style of the code isn't perfect, but like I said C# isn't really my thing and getting it running was about the best I could do. (The case sensitivity threw me off for a bit and those C# error messages aren't really all that helpful.) I know it's not much, but hopefully those of you who were looking for a C# version at least now have something to start with.

    Connecting to MySQL from ASP.NET 2.0

    While you can still connect to MySQL from ASP.NET 2.0 using the same old ASP.NET 1.x style of code (using the MySql.Data.MySqlClient) that sort of defeats the point of all the cool .NET 2.0 stuff. Hopefully the official MySQL Connector/Net will support the new fully declarative data binding model soon, but in the mean time you'll need to turn to a third party. While there may well be others, one third-party connector that does support the new provider model is CoreLab's MySQLDirect .NET Data Provider. They provide a fully-functional 30-day trial version so you can determine whether their product is right for you.

    Please note that there are a few different versions available for download. Make sure that you get the one for .NET Framework 2.0 if you're going to be trying the code that follows. The versions for .NET Framework 1.x are not the same thing. You don't need to use the same version I used, but if for some reason you want to then look for "MySQLDirect .NET version 3.50 beta for .NET Framework 2.0".

    I didn't play with the Visual studio plug-ins or any of the bells and whistles, but just did my best to get it working on it's own. The first step is obviously to download and install the connector, which is extremely straight-forward. Once that's done, the next step is to get the connector registered as a data provider. To do this you need to add an entry to either your web server's machine.config or your application's web.config. Here's a copy of the web.config file I used to get my sample page running:

    web.config

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.data>
            <DbProviderFactories>
                <add
                    name="MySQLDirect .NET Data Provider"
                    invariant="CoreLab.MySql"
                    description="CoreLab MySQLDirect .NET Data Provider"
                    type="CoreLab.MySql.MySqlDirectFactory, CoreLab.MySql, Version=3.50.10.0, Culture=neutral, PublicKeyToken=09af7300eec23701"
                />
            </DbProviderFactories>
        </system.data>
    </configuration>

    I don't know why, but I couldn't find this information anywhere in the documentation. I ended up scouring through CoreLab's Forums for a while until I found a few posts that explained the entry. (http://www.crlab.com/forums/viewtopic.php?t=2555 and http://www.crlab.com/forums/viewtopic.php?t=3285)

    The connector also has a licensing requirement. How to generate a license is covered in the documentation, but it is a little bit of an exercise so I'm going to outline the steps I used here.

    1. Create a text file named licenses.licx.
    2. Place the text CoreLab.MySql.MySqlConnection, CoreLab.MySql in the file.
    3. Save the file and add it to a Visual Studio 2005 project.
    4. Right click on licenses.licx in the VS's Solution Explorer and select "Build Runtime Licenses" from the context menu.
    5. This will create a file named App_Licenses.dll in the solution's /bin folder.
    6. Copy this file to the /bin folder of your application.

    I wasn't actually using Visual Studio to write the code. The licensing step may be simpler if you do, but I was trying to keep things as simple as possible for the sake of illustration.

    Once the provider is registered and the licensing is taken care of, all that's left to do is write the code to connect to our data. I'm going to use some very simple code from my earlier article Introducing the ASP.NET 2.0 GridView Control and simply modify it to connect to MySQL instead of Microsoft SQL Server.

    MySqlGridView.aspx

    <%@ Page Language="VB" %>
    <html>
    <head>
      <title>MySQL ASP.NET 2.0 CoreLab GridView</title>
    </head>
    <body>

    <form runat="server">

      <asp:SqlDataSource id="myMySqlDataSrc" runat="server"
        ProviderName="CoreLab.MySql"
        ConnectionString="server=localhost; Database=mydatabase; User Id=15secs; password=password;"
        SelectCommand="SELECT * FROM mytable;"
      />

      <asp:GridView id="myGridView" runat="server"
        DataSourceID="myMySqlDataSrc"
      />

    </form>

    </body>
    </html>

    The line in red is the one that tells the script to use CoreLab's provider. Without that the script would attempt to connect via the default SQL Server provider which obviously wouldn't work very well.

    And here's a quick screen shot, just to show you that this actually does work:

    Now I realize that the output isn't much to look at, but it does run and you automatically get all the goodies that go along with the new ASP.NET 2.0 data-bound controls.

    Conclusion

    I hope this part of the article has helped address some of the questions and issues that readers have been running into when trying to use ASP.NET with MySQL. Whether you were introduced to the combination by the earlier parts or arrived here simply trying to find a solution to a problem you ran into on your own, please let me know if you found the information useful and/or how it could be made more so. You can reach me by using the site's feedback form and mentioning either my name, Ziran Sun, or the title of the article.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 21, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 1
    While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
    [Read This Article]  [Top]
    Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
    With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
    [Read This Article]  [Top]
    Mar 10, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2, Cont'd
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 9, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 3, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1, Cont'd
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Mar 2, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Feb 16, 2005 - Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
    In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
    [Read This Article]  [Top]
    Dec 29, 2004 - ClickOnce Deployment in .NET Framework 2.0
    In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications.
    [Read This Article]  [Top]
    Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
    With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
    [Read This Article]  [Top]
    Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
    Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers