# trouble connecting to database



## gib88 (Aug 18, 2005)

I'm having trouble connecting to my database. Here's my specs:

Computer: AMD Athlon XP 3200+ with 2.20 GHz, 448 MB of RAM
OS: Windows Server 2003 Web Edition (SP1)
Database: MySQL 5.0.19
Database name: gibsdb

I created a new user named 'gib' with password 'gibpw' and host set to 'localhost'. Now I'm trying to connect to it from a website on the same machine using this user account. The website is programmed in ASP.NET and VB.NET. Here's the VB function that tries to connect to the database:

Function Connect() As String

Dim ConStr As String = "Server=localhost; Database=gibsdb;

uid=gib; pwd=gibpw; Provider=SQLOLEDB;"
Dim Connection As New OleDb.OleDbConnection(ConStr)
Connection.Open

End Function

This is called from the Page_Load() subroutine so it should connect (or fail to connect) upon loading the page. When I load the page, it gives me this error message:

Server Error in '/' Application.
--------------------------------------------------------------------------------

[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.

Source Error:

Line 149:	Dim ConStr As String = "Server=localhost; Database=gibsdb; uid=gib; pwd=gibpw; Provider=SQLOLEDB;"
Line 150:	Dim Connection As New OleDb.OleDbConnection(ConStr)
Line 151:	Connection.Open
Line 152:
Line 153:End Function

Source File: D:\Inetpub\public_websites\sandstoneestates\wwwroot\admin.aspx Line: 151

Stack Trace:

[OleDbException (0x80004005): [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.]
System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr) +20
System.Data.OleDb.OleDbConnection.InitializeProvider() +57
System.Data.OleDb.OleDbConnection.Open() +203
ASP.admin_aspx.Connect() in D:\Inetpub\public_websites\sandstoneestates\wwwroot\admin.aspx:151
ASP.admin_aspx.Page_Load() in D:\Inetpub\public_websites\sandstoneestates\wwwroot\admin.aspx:58
System.Web.Util.ArglessEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +10
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2300; ASP.NET Version:1.1.4322.2300

I'm not sure how to interpret this. Is it a problem of accessing the database or of finding the database? Can anyone see what the problem might be?


----------



## treydx (Jan 4, 2006)

Isn't SQLOLEDB for SQL Server, not MySQL?? Hence your error: "SQL Server does not exist"

Try this website:
http://www.connectionstrings.com


----------



## gib88 (Aug 18, 2005)

Thanks,

That solved the current problem, but now I've got a new problem. The error message I'm getting now is:

No error information available: REGDB_E_CLASSNOTREG(0x80040154). 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: No error information available: REGDB_E_CLASSNOTREG(0x80040154).

Source Error:

Line 149:	Dim ConStr As String = "Server=localhost; Database=gibsdb; uid=gib; pwd=gibpw; Provider=MySQLProv;"
Line 150:	Dim Connection As New OleDb.OleDbConnection(ConStr)
Line 151:	Connection.Open
Line 152:
Line 153:End Function

Source File: D:\Inetpub\public_websites\sandstoneestates\wwwroot\admin.aspx Line: 151

Stack Trace:

[OleDbException (0x80040154): No error information available: REGDB_E_CLASSNOTREG(0x80040154).]

[InvalidOperationException: The 'MySQLProv' provider is not registered on the local machine.]
System.Data.OleDb.OleDbConnection.CreateProviderError(Int32 hr) +83
System.Data.OleDb.OleDbConnection.CreateProvider(OleDbConnectionString constr) +107
System.Data.OleDb.OleDbConnection.Open() +203
ASP.admin_aspx.Connect() in D:\Inetpub\public_websites\sandstoneestates\wwwroot\admin.aspx:151
ASP.admin_aspx.Page_Load() in D:\Inetpub\public_websites\sandstoneestates\wwwroot\admin.aspx:58
System.Web.Util.ArglessEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +10
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750

I looked the problem up and one site said that I need to download the MySQL connector. So I did that, installed it, but it still gives me the same message.

Any insights?


----------



## treydx (Jan 4, 2006)

I think you might still be mixing up your connection strings. If you want to use the OLE DB one, use this connection string:
Provider=MySQLProv;Data Source=gibsdb;User Id=gib;Password=gibpw;


------


But if you want to use the "Connector" one that you downloaded (?), try building your connection string with the appropriate class (i.e. not oledb).

connection string:
Server=localhost;Database=gibsdb;Uid=gib;Pwd=gibpw;" 

open it:
Imports MySql.Data.MySqlClient
Dim oMySqlConn As MySqlConnection = New MySqlConnection()
oMySqlConn.ConnectionString = "Server=localhost;Database=gibsdb;Uid=gib;Pwd=gibpw;"
oMySqlConn.Open()


----------



## gib88 (Aug 18, 2005)

OK, further problems.

I'm not sure where I need to place the line

Imports MySql.Data.MySqlClient

or even how the syntax should look for my particular application.

If I put it in the Connect() function, like so:

Function Connect() As String

Imports MySql.Data.Client

Dim ConStr As String = "Server=localhost; Database=gibsdb;

uid=gib; pwd=gibpw; Provider=MySQLProv;"
Dim Connection As MySqlConnection = New MySqlConnection()
Connection.ConnectionString = ConStr
Connection.Open

End Function

it gives me the error message: BC30035: Syntax error.

If I put it at the top of the VB.NET script, like so:

<script runat="server">

Imports MySql.Data.MySqlClient
...

it gives me the error message: BC30465: 'Imports' statements must precede any declarations.

Even though would seem it IS preceding any declarations (there are some ASP.NET controls in HTML tags below the VB.NET script. These might be getting declared before anything else).

If I place it in the Namespace tag (as it probably should be) like so:

<%@ Import namespace="MySql.Data.MySqlClient" %>

It gives me the error message: BC30002: Type 'MySqlConnection' is not defined.

Obviously, I have to retype the syntax in this case, but it seems like it's not really getting imported.

I tried searching the web for examples, but since my application is so particular (connecting to a MySQL database in VB.NET in an ASP.NET file), I couldn't find anything. I'm almost there though - I just need a little bit more help... please.


----------



## treydx (Jan 4, 2006)

Unfortunately, I know very little about ASP.NET. I do a lot with ASP, though, and have realized that it can be very picky about it's connection strings. If you use "Database" instead of "Data Source" or "User Id" instead of "Uid" it will blow up on you sometimes.

Figuring out which connection string you want to use is entirely up to how you and your host has set up your database, but the ones on connectionstrings.com are correct and you should try to use the one that best fits your needs from there.

Try the MySQL site for help, they certainly will tell you how to connect to their database.

MySQL AB :: Exploring MySQL in the Microsoft .NET Environment

Here's a quote from the MySQL page:


> You also must include a reference to the .DLL when you compile your code. For example, if you are compiling a C# program, your command line should include:
> 
> csc /r:System.Data.dll


So try adding the MySql.Data.MySqlClient reference to your application when you compile it.


----------



## gib88 (Aug 18, 2005)

Actually, the problem was with where I placed the MySQL.Data.dll. I had it in the wwwroot directory rather than the wwwroot/bin directory. Once placed there, everything seemed to work fine.


----------

