Link to home
Start Free TrialLog in
Avatar of PelicanDev
PelicanDev

asked on

How to connect asp.net to mysql

Hi,

I tried to connect to a MySql data base with asp.net. It work fine on my dev machine but when i publish my web site on the server i got this message... I installed the odbc 3.51 driver on the server.

ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

[OdbcException (0x80131937): ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified]
   System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode) +1156770
   System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr, OdbcEnvironmentHandle environmentHandle) +95
   System.Data.Odbc.OdbcConnectionOpen..ctor(OdbcConnection outerConnection, OdbcConnectionString connectionOptions) +53
   System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) +55
   System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) +29
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +4863644
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +117
   System.Data.Odbc.OdbcConnection.Open() +40
   website_dealer.ViewSearchClient_Activate(Object sender, EventArgs e) +100
   System.Web.UI.WebControls.View.OnActivate(EventArgs e) +111
   System.Web.UI.WebControls.MultiView.set_ActiveViewIndex(Int32 value) +398
   System.Web.UI.WebControls.MultiView.OnInit(EventArgs e) +49
   System.Web.UI.Control.InitRecursive(Control namingContainer) +333
   System.Web.UI.Control.InitRecursive(Control namingContainer) +210
   System.Web.UI.Control.InitRecursive(Control namingContainer) +210
   System.Web.UI.Control.InitRecursive(Control namingContainer) +210
   System.Web.UI.Control.InitRecursive(Control namingContainer) +210
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +378

string ConnStr = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=XXX;DATABASE=XXX;UID=XXX;PASSWORD=XXX;PORT=3306;OPTION=3;";
        using (OdbcConnection con = new OdbcConnection(ConnStr))
        {
            using (OdbcCommand cmd = new OdbcCommand("DELETE FROM succursales WHERE ID=?", con))
            {
                cmd.Parameters.AddWithValue("@customerId", customerId);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

Open in new window

Avatar of psimation
psimation
Flag of South Africa image

Are you sure you configured the OCBC driver correctly on your server?

It's a two stage process:
install the driver, then add a user or system DSN "linked" to that driver. The User or system DSN will then have a "name" that you allocate to it, and it is this name that you can use in your code. Also make sure you configure the driver/dsn with your specific connection settings.
Avatar of PelicanDev
PelicanDev

ASKER

Yes, i have create a DSN Entry for  MySql connector and when i try the test connection button it connect succefully.

I added the DSN=MyConnector name in the connection string but no success...
string ConnStr = "DRIVER={MySQL ODBC 3.51 Driver};DSN=XXX;SERVER=XXX;DATABASE=XXX;UID=XXX;PASSWORD=XXX;PORT=3306;OPTION=3;";

Open in new window

Avatar of BALMUKUND KESHAV
Try to use w/o DSN

string ConnStr = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=XXX;DATABASE=XXX;UID=XXX;PASSWORD=XXX;PORT=3306;OPTION=3;";

Bm Keshav
ASKER CERTIFIED SOLUTION
Avatar of PelicanDev
PelicanDev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
which version of MySql are you using ? I hope you are using 5.1 thats why mysql odbc 3.51 driver is not working, its work upto 5.0 ver.

Bm Keshav