Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses

670 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