We help IT Professionals succeed at work.

issue with DB inserts in .NET

dtii asked
Medium Priority
Last Modified: 2010-08-05
Hello Experts..

I am fairly new to the .Net/C# sharp world and am having some issues getting Database interactivity to work properly.  

Basically the issue is my insert statements do not seem to be "committing" a record into the database.
my Database connection works.. I am able to pull records back its just that i cant insert rows..
Additionally i can run the same exact code from the command line and succesfully insert a row...
so im thinking it has something to do with the Visual Studio Envinroment and data connectivity..
But i dont know why that would be the case or if it even is..

A peculiar issue is when i run the app in Visual Studio from the same "debugging session" It seems as if it does insert the record because i have a listobx that displays the results and the new row is there..
But this is only evident from the existing "debugging session"  What i mean by this is..  If i view the Access database i do not see the new row but in the "debug session" i do.  When i close the "debugging session" and start debugging againthe newly inserted row DOES NOT display within the resultset.  It looks like a transaction commit issue.. But this is an Access database and the same code works from the command line??.. So im not sure what to think about that.

Here is the statement that will insert a row from the command line but not from visual studio

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myCampground.mdb");
String insertSQL = "insert into site_types (site_type, site_type_descr,base_rate, default_guests) values('Modern Thing','test',12,2)";
OleDbCommand cmd = new OleDbCommand(insertSQL, conn);
try {
  Console.WriteLine("it should have worked");
catch (Exception ex){
  //MessageBox.Show("we have a DB error during the insert of the site type" + ex.Message);

I hope this is enough information.. This seems very strange to me and im just hoping its some kind of
.net/visual studio intricacy im not aware of.

Watch Question

Most Valuable Expert 2012
Top Expert 2008

There is nothing that explains why this is happening to you.  You have not implemented any kind of transaction, and everything looks good.

What is the environment?  Operating system?  .NET version?  Winforms or ASP.NET?



That sucks .. i was hoping i was missing something simple..

OS: Windows XP
Im using Visual Studio 2005 so i think the .net framework version is 2.0
This is a desktop app im utilizing WinForms via C#

What would be the recommended method for database work utilizing a desktop app and and access database?..

Here is one more example of doing it. U are doing it the right way. U could check on two things

1. Have u referred Microsoft.Jet.OLEDB.4.0.dll in your project
2. Have u given a correct path for your data source
3. Try executing this code directly from your C# code Windows or Web
4. After running the insert statement..check by using

select * from <tablename> with (nolock) : To avoid locks if any

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Samples\\Employee.mdb";

OleDbConnection myConnection = new OleDbConnection( connectionString );

string query = "insert into EMPLOYEE_TABLE (EmployeeID, Name, Address) VALUES (101, 'John', '3960 CliffValley Way')";

OleDbCommand myCommand = new OleDbCommand();
myCommand.CommandText = query;
myCommand.Connection = myConnection;



OK.. i found the issue but i still dont know exactly why its happening

If you notice in my example.. in the connection string i am using the following as my data source
"Data Source=myCampground.mdb"

For some crazy reason when i "hardcode" the path.. meaning c:/whatever/myCampground.mdb it works fine and inserts the row as expected.
Whats strange if i insert a row manually into the "relative" pathed database and then run a Visual Studio debug session  .. The row shows up fine but yet when i attempt to insert a new one while within the debug session it acts as if it doesnt "commit" the record.. The Debug session indicates an insert and i can query up the new row from within the "Debug Session" but once im out of the "Debug Session" ... its like the row never existed..

Im thinking Visual Studio does some strange kind of temp database thing and copies the relatively pathed database into memory and then when the debug session is done for whatever reason it doesnt copy the database back down to the original.

Now i wanted the path datasource path to be relative because eventually the app will have to go to a dynamic location.  So what exactly is the best way to implement a datasource in a .net desktop app?...

Any tips and/or adivce on implementing an Access DB into a standalone desktop app .. would be helpful and worth 125 points..

Most Valuable Expert 2012
Top Expert 2008
You can do it full path, checking if the DEBUG compiler directive is present, otherwise use the startup location to build the path:

#if (DEBUG)
   string source = @"C:\whatever\myCampground.mdb";
   string source = Application.StartupPath + "myCampground.mdb";


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Cool thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.