+ Reply to Thread
Results 1 to 4 of 4

Thread: asp.net and mysql

  1. #1
    spangeman is offline x10Hosting Member spangeman is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    6

    asp.net and mysql

    Hi All

    Could you tell me if it is possible to get aspx pages talking to mysql on x10 hosting?

    I have looked through the forums and someone seems to be suggesting adding MySql.Data.dll to a directory called bin and adding the reference to the web.config file. I have tried this and I get the following error.
    System.Exception: Exception of type 'System.Exception' was thrown.
    at MySql.Data.MySqlClient.NativeDriver.Open () [0x00000]



    Here is my aspx code




    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="MySql.Data.MySqlClient" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>CD cat</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <script runat="server">
    private void Page_Load(Object sender, EventArgs e)
    {
    string connectionString = "Server=localhost;Database=my_database;User ID=mysuerid;Password=mypassword;Pooling=false;";
    MySqlConnection dbcon = new MySqlConnection(connectionString);
    dbcon.Open();

    MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM artists", dbcon);
    DataSet ds = new DataSet();
    adapter.Fill(ds, "result");

    dbcon.Close();
    dbcon = null;

    ArtistsControl.DataSource = ds.Tables["result"];
    ArtistsControl.DataBind();
    }
    </script>

    </head>

    <body>
    <h1>Artists</h1>
    <asp:DataGrid runat="server" id="ArtistsControl" />
    </body>

    </html>



    Thanks
    SPangeman

  2. #2
    thegriff is offline x10Hosting Member thegriff is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    14

    Re: asp.net and mysql

    Yes it is - but I had loads of problems myself!
    The solution:

    Copy both mysql.Data.dll and mysql.web.dll to the bin directory, i didn't have to add any references, it just worked.

    Code:
    using MySql.Data.MySqlClient;
    ...
    strConnect = ConfigurationManager.ConnectionStrings["LoginDatabase"].ConnectionString;
    sqlCmd.Connection = newMySqlConnection(strConnect);
    sqlCmd.Connection.Open();
    reader = sqlCmd.ExecuteReader();
    reader.Read();
    ...use reader["fieldname"]
    sqlCmd.Connection.Close();
    


    In your ONLINE web.config:
    Code:
    <?xml version="1.0" ?>
    <configuration>
      <appSettings />
      <connectionStrings>
        <add name="LoginDatabase"
          connectionString="server=mysql-chopin.x10hosting.com;database=XXX_YYY;user id=ZZZ;password=???" />
        <remove name="LocalMySqlServer"/>
        <add name="LocalMySqlServer" 
      </connectionStrings>
     
    ...anything else you have
    where XXX is your login ID, YYY is the name of your database: so if you run phpMyAdmin it will show a database as XXX_YYY
    ZZZ is the userid as set via phpMYAdmin,
    ??? is the user password fro ZZZ

    Doing it this way makes it easier to debug locally as your online web.config can contain different values from your local system - my local system for example uses "localhost".
    The really important bit is "mysql-chopin.x10hosting.com" rather than "localhost".

    Haven't tried the dataadapter binding, but if the above works it should all be obvious.
    No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.


    FOR SALE: Bargain! Large supply of slightly used '0's and '1's. Can be delivered electronically. Special rates for bulk purchases!

  3. #3
    spangeman is offline x10Hosting Member spangeman is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    6

    Re: asp.net and mysql

    Thanks for this post, it helped a lot.

    Written a tutorial for anyone else that gets stuck with setting up MySql with asp.net

    I coded mine slightly differently so I have posted both examples in the tutorial.

    How to get ASP.Net to work with MySql at x10Hosting

    Can we get it made sticky?

    Thanks again.
    Spangeman
    Last edited by spangeman; 10-28-2009 at 05:23 AM.

  4. #4
    pioneer_fawad's Avatar
    pioneer_fawad is offline x10Hosting Member pioneer_fawad is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    6

    Wink Re: asp.net and mysql

    Thanx

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. MySql + ASP.net Connections problem..
    By monsterhit in forum Programming Help
    Replies: 3
    Last Post: 12-10-2009, 01:28 AM
  3. mySQL and ASP.NET
    By fred070 in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 08-30-2009, 07:32 AM
  4. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  5. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers