Solved

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

Posted on 2004-08-05
4
476 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
Comment Utility
stupid question: the variable objConn is of type OleDbConnection, right?
0
 
LVL 4

Expert Comment

by:nmwis70
Comment Utility
I bet that is it.
0
 
LVL 1

Author Comment

by:ravl
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now