Solved

.NET - "Specified cast is not valid" error when instantiating oleDBCommand

Posted on 2004-08-05
4
492 Views
Last Modified: 2008-02-01
Specified cast is not valid.
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.InvalidCastException: Specified cast is not valid.

Source Error:


Line 12:         Public Sub AddCart(ByVal CustomerID As Integer, ByVal ProductID As Integer, ByVal Qty As Double)
Line 13:             'Add a single row item to the cart
Line 14:             Dim objCmd As New OleDbCommand("uspAddCart", objConn)
Line 15:             objCmd.CommandType = CommandType.StoredProcedure
Line 16:

The error refers to line 14. At one stage this was working. Now all of a sudden it's not! And all functions containing code like line 14 now fail the same way. Any ideas please?
 
0
Comment
Question by:ravl
  • 2
4 Comments
 
LVL 14

Expert Comment

by:dfu23
ID: 11732590
stupid question: the variable objConn is of type OleDbConnection, right?
0
 
LVL 4

Expert Comment

by:nmwis70
ID: 11738697
I bet that is it.
0
 
LVL 1

Author Comment

by:ravl
ID: 11850155
The problem is certainly with obConn. I am using:
        Private objConn = CType(configurationAppSettings.GetValue("DSN", GetType(System.String)), String)
        Dim strConnectionString = "Provider=Microsoft.Jet.OleDB.4.0;Data Source=C:\Files\vsDotNet\ProvidenceVineyard\database\ProvidenceData.mdb"

and DSN is defined in webconfig <appsettings> as:
            <add key="DSN" value="Provider=Microsoft.Jet.OleDB.4.0;Data Source=C:\Files\vsDotNet\Test\database\ProvidenceData.mdb" />
            
WHat am I missing?
0
 
LVL 14

Accepted Solution

by:
dfu23 earned 125 total points
ID: 11851476
objConn needs to be an OleDbConnection ... it appears that you are setting it as a string from the code snippet above ...

I think that it should be something along the lines of the following (I use C# more than VB.NET so this might be a little off):

Dim objConn As New OleDbConnection(CType(configurationAppSettings.GetValue("DSN", GetType(System.String)), String))

This will set the variable objConn to the OleDbConnection type and will pass it the connection string from your web.config file for the constructor. There should not be a need for the second line of code:

Dim strConnectionString = "Provider=Microsoft.Jet.OleDB.4.0;Data Source=C:\Files\vsDotNet\ProvidenceVineyard\database\ProvidenceData.mdb"

Which duplicates the value in the key that you have in your web.config and would defeat the purpose of having it in there.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question