Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-05
4
Medium Priority
?
508 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 500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

916 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