Solved

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

Posted on 2004-08-05
4
500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

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.
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
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…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

695 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