issue with DB inserts in .NET

Posted on 2006-03-20
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.

Question by:dtii
    LVL 96

    Expert Comment

    by:Bob Learned
    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?


    Author Comment

    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?..

    LVL 10

    Expert Comment

    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;


    Author Comment

    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..

    LVL 96

    Accepted Solution

    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";


    Author Comment

    Cool thanks

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now