|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Calling MS Access Connection String problem
I have the connection string located in a common.vb file in my asp.net solution. The connection string looks like this:
Public Function dbConn() Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Documents an" & _ "d Settings\EZ\Desktop\PurdueAlumni.mdb" Dim strConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString ) dbConn = strConnection End Function This is what I have in a form that needs to call the connection string: Dim dbConnection As String dbConnection = dbConn() When I run the debugger, I go to the default page. I type in my username and password (the main page is a login screen), and I recieve this error: Cast from type 'OleDbConnection' to type 'String' is not valid. Line 41: Line 42: Dim strSQL As String Line 43: Dim conn As New OleDb.OleDbConnection(dbConnection) Line 44: Dim cmd As New OleDb.OleDbCommand(strSQL, conn) Line 45: Dim reader As OleDb.OleDbDataReader This is what is in my code right now: Dim dbConnection As String dbConnection = dbConn() Dim strSQL As String Dim conn As New OleDb.OleDbConnection(dbConnection) Dim cmd As New OleDb.OleDbCommand(strSQL, conn) Dim reader As OleDb.OleDbDataReader Dim adapter As OleDb.OleDbDataAdapter Any ideas?? Thanks |
|
#2
|
|||
|
|||
|
Your function returns a value of datatype System.Data.IDbConnection since it returns strConnection, not connectionString. When you call your function, you set a string variable to it:
Dim dbConnection As String dbConnection = dbConn() .Net doesn't know how to cast the IDbConnection value to a string. I'm not sure I know what you're trying to do but I'm guessing you want to return a connection object to be used for database access. If so, you should call your function with something like the following: Dim oConn as System.Data.IDbConnection = dbConn() HTH. |
|
#3
|
|||
|
|||
|
Cast from type 'OleDbConnection' to type 'String' is not valid.
Line 41: Dim conn As New System.Data.odbc.OdbcConnection(dbConn) Line 42: Dim cmd As New System.Data.odbc.OdbcCommand Line 43: Dim reader As System.Data.odbc.OdbcDataReader Line 44: Dim adapter As System.Data.odbc.OdbcDataAdapter Line 43 is where the error occurs. Here is my dbConn function that I call. It may be something wrong with this. Public Function dbConn() Dim strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Documents an" & _ "d Settings\EZ\Desktop\Alumni.mdb" dbConn = strConnectionString End Function Thanks for the help!!! |
|
#4
|
|||
|
|||
|
An incorrect connection string shouldn't cause a cast error. I suspect dbConn may be defined in more than one place. Try replacing
Line 41: Dim conn As New System.Data.odbc.OdbcConnection(dbConn) with Line 41: Dim conn As New System.Data.odbc.OdbcConnection("Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Documents and Settings\EZ\Desktop\Alumni.mdb") and see if you get the same error. I'm not sure about the connection string itself (Ole DB Services=-4?). I use this site for reference: http://www.connectionstrings.com/. Click on Access and then the OLE DB connection. HTH |
![]() |
| Viewing: Dev Articles Community Forums > Programming > .NET Development > Calling MS Access Connection String problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|