Click here to Skip to main content
16,014,892 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am trying to insert data into my table whose primary key is auto increment.

The problem I encounter is that anytime I perform the insert, it goes through and I can view it when my application is running but as soon as I stop debugging and I run again I dont see the data again.

I also notice that the insertion I did does not show up in the database.

Below is the code I am using:


String name, type;
 name = txtname.Text;
 type = txttype.Text;
 
 int qtyToInt = Int32.Parse(txtqty.Text);
 decimal priceToInt = decimal.Parse(txtprice.Text);

            
 shopDataSet.stockRow newStockRow =  shopDataSet1.stock.NewstockRow();
               
                      
 newStockRow.name = name;
 newStockRow.type = type;
 newStockRow.quantity = qtyToInt;
 newStockRow.price = priceToInt;

 this.shopDataSet1.stock.Rows.Add(newStockRow);
 this.stockTableAdapter1.Update(this.shopDataSet1.stock);
Posted
Updated 8-Oct-10 10:18am
v3
Comments
2gizzo 6-Oct-10 13:41pm    
What version of visual studio/sql are you using?
[no name] 6-Oct-10 14:06pm    
vs team system 2008
2gizzo 6-Oct-10 14:36pm    
Okay sometimes when you debug an application a copy of the database is sent to \<your_project_name>\bin\Debug it could be that that the one its saving to and when you run your app it reference the one in your project folder so delete the one in your project folder (but make a backup copy before you do) and leave the one in the debug folder then try run it again and see if that solves it!
[no name] 6-Oct-10 15:41pm    
it didnt work. I could not even run the application from vs. some error about not being able to copy contents of the database from the project folder to that in the debug folder poped up
Dalek Dave 8-Oct-10 16:18pm    
Edited for Grammar.

Okay then put it back as it was before. I had a similar problem when i upgraded to win 7 (was on vista before) but when i deployed the application it seemed to work fine. Before we try drastic measure try to to use a different method to save data may be using sql parameters! Try that and tel me if it behaves the same. Before you do that try it the other way around remove the debug one clean ur project from the build menu and debug it again (make sure you clean for both debug and release )
 
Share this answer
 
Comments
[no name] 7-Oct-10 6:25am    
I tried removing the debug database. That didnt work either so am i am trying to use sql command objects but i am having problems with the connection string. This is the connection string i am using:

String connection = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\shop.mdf;Integrated Security=True;User Instance=True";

I am using this one because it was generated by the tableadapter. Since vs 2008 installs with mssql server 2005, i didnt bother to install sql 20005 server separately. i dont know if thats what is causing the connection problem
Your connection string is fine but in C# '\' is a special character when used inside string literals so hav to explicitly tel C# that are using special characters literally. So ther two ways to solve your problem
1. Using the escape character- declare your string variable like this
string conString = @".\SQLEXPRESS;AttachDbFilename=|DataDirectory|\shop.mdf;Integrated Security=True;User Instance=True"; // the @ symbol tels csharp to escape all characters

2. Use the connectionstring stored in the app.config xml file
This is a much more portable way and less messy as you dont hav to deal with escape characters of any kind but make sure app.config was generated (it usually does when u add a database to the project) and then call it lyk this
string conStr = Properties.Settings.Default.ShopConnectionString;
// rest of code 

and thats it
 
Share this answer
 
Comments
[no name] 7-Oct-10 13:03pm    
it does insert when i use the command objects but the same problem of it not registering in the database occurs. When the application is running and you insert data, this data can be viewed. but immediately i stop debugging and i run again, the previously inserted data is gone.
Dalek Dave 8-Oct-10 16:18pm    
Good Call
Okay try deploying ur app as a setup file then install and run the installed app and check if it the information iz ther when u re-launch the application. Also try to restart the sql service frm sql configuration manager
 
Share this answer
 
Comments
[no name] 7-Oct-10 14:53pm    
i didnt deploy it but i run the exe file in the debug folder and the data seems to be there. when i add the data it registers in the database when i try to view it. it is still there even after i restarted my pc. i wonder why it behaves differently when i am debugging in the vs ide
Exe one is refering to the db in the bin and vs iz refering to the one in your project so here is what you do, on the solution explorer right click Shop.mdf make sure the build action is set to content and set Copy to Output Directory->Copy if newer. Debug the project again and let me knw what happens!
 
Share this answer
 
Comments
[no name] 8-Oct-10 13:07pm    
i think its working now. but it still causes the same problem when i am in the vs ide. but when i run it from the exe from the debug folder it works perfectly
Okay then but i tried the copy if newer tip and it worked 4me even from within Vs but atleast for you its much better than before
 
Share this answer
 
Comments
[no name] 8-Oct-10 16:43pm    
yeah it is....thanks alot u ve been extremely helpful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900