Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

issue with DB inserts in .NET

Posted on 2006-03-20
6
Medium Priority
?
221 Views
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 {
  conn.Open();
  cmd.ExecuteNonQuery();
  conn.Close();
  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);
  Console.WriteLine(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.

Thanks,
Dave
0
Comment
Question by:dtii
  • 3
  • 2
6 Comments
 
LVL 96

Expert Comment

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

Bob
0
 

Author Comment

by:dtii
ID: 16243661
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?..



0
 
LVL 10

Expert Comment

by:123654789987
ID: 16244159
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 );
myConnection.Open();

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;
myCommand.ExecuteNonQuery();

myConnection.Close();
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:dtii
ID: 16264924
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..

thanks,
Dave
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 16270165
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";
#else
   string source = Application.StartupPath + "myCampground.mdb";
#endif

Bob
0
 

Author Comment

by:dtii
ID: 16270188
Cool thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 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