Solved

OleDb.ExecuteNonQuery not using the correct path

Posted on 2011-02-28
4
569 Views
Last Modified: 2012-05-11

I create an OleDbCommand, giving it a connection that I know ought to work.  Just before I execute this command:

int retval = cmd.ExecuteNonQuery();

...I look at the values in the OleDbCommand object cmd.

The Connection’s connection string is: "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\francine\\Winform.Finance\\Finance.mdb;"

…and the connection’s DataSource is: "C:\\francine\\Winform.Finance\\Finance.mdb"

…but when I execute it, an OleBdException is thrown:  {"Could not find file 'C:\\francine\\Winform.Finance\\bin\\Debug\\dbo.mdb'."}

Does anybody know why it might be losing the name of the database?
0
Comment
Question by:FrancineTaylor
[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
  • 3
4 Comments
 
LVL 13

Expert Comment

by:agarwalrahul
ID: 35004202
Try This:

Put the database in App_Data folder and

then change your Datasource path to

Source=|DataDirectory|\Finance.mdb
 
0
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 35014269
I changed the pathname to access the App_Data folder.  The code now reads:

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\francine\\Crystal Applications\\Crystal.Winform.Finance\\App_Data\\Finance.mdb;");
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("INSERT INTO dbo.trans(description) VALUES('Description')", conn);
            int retval = cmd.ExecuteNonQuery();

When I hit the ExecuteNonQuery line, it throws the same error:

{"Could not find file 'C:\\francine\\Crystal Applications\\Crystal.Winform.Finance\\Crystal.Winform.Finance\\bin\\Debug\\dbo.mdb'."}

The path works fine for other commands on that database.  Tables were created, records inserted into those tables, etc.  The tran table was just created previous to this command ("CREATE TABLE [trans](ID int IDENTITY PRIMARY KEY, Description VARCHAR(50)")

It isn't until I try to insert a record into the trans table that the error is thrown.
0
 
LVL 1

Accepted Solution

by:
FrancineTaylor earned 0 total points
ID: 35048228
Okay, never mind.  I found the error.  It was throwing an error on the INSERT statement that I was trying to execute because one of the string values that I was inserting just happened to have a single quote in the data, and that threw off the whole statement.

Not sure why it was getting a .NET error instead of an SQL error, but there it is.
0
 
LVL 1

Author Closing Comment

by:FrancineTaylor
ID: 35106999
Figured it out myself
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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