We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,913 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

InvalidOperationException was unhandled

Hi everyone,
I'm creating a booking application for a hotel and have encountered an error on the following code;

Public Class Seaside_Hotel_DB
    Dim dbconn As OleDb.OleDbConnection
    Dim dbcmd As New OleDb.OleDbCommand
    Dim dbread As OleDb.OleDbDataReader
    Dim spath As String
    Public Sub New()
        spath = Application.StartupPath
        spath = spath.Replace("\bin\Debug", "")
        spath = spath + "\Seaside Hotel.mdb"
        dbconn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & spath & ";Persist Security Info=True")
    End Sub
    Public Function NewClient(ByVal cname As String, ByVal adr As String, ByVal city As String, ByVal pcode As Int32, ByVal phone As String) As Boolean
        Dim query As String
        Try
            dbconn.Open()
            query = "insert into Customer (CustName,Address,City,PostCode,Phone) values ('" & cname & "','" & adr & "','" & city & "'," & pcode & ",'" & phone & "')"
            dbcmd = New OleDb.OleDbCommand(query, dbconn)
            dbcmd.ExecuteNonQuery()
            dbconn.Close()
            Return True
        Catch ex As OleDb.OleDbException
            Return False
        End Try
    End Function

The error is the dbconn.open() in the public function New Client.

Your assistance will be appreciated.

Many thanks

7
Contributors
6
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
YungSoprano
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi Yung,

What is the error message?

catherine sea
Junior Poster
126 posts since Jan 2008
Reputation Points: 25
Solved Threads: 22
Skill Endorsements: 0

This error may relate to which excel edition you use or you can choose another provider instead "Microsoft.Jet.OLEDB.4.0"

ntzfx
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

Your database path setup looks a bit odd.
do a

MsgBox(dbconn.DataSource)

just before the dbconn.open() command and see if the database is stored at the path that is shown in the messagebox.

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 169
Skill Endorsements: 1

Try to put your database in the same location as your application and run it again.

Al-zene
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & spath & ";Persist Security Info=True")

Your connection string might be incorrect, it looks like it is missing some things.

Here is a good reference for connection strings:

http://www.connectionstrings.com/excel

Begginnerdev
Practically a Posting Shark
861 posts since Apr 2010
Reputation Points: 184
Solved Threads: 142
Skill Endorsements: 8

Thanks everyone, the app is good to go.

YungSoprano
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by catherine sea, GeekByChoiCe, Begginnerdev and 2 others

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0804 seconds using 2.75MB