Question:
Why am I losing my data in SQL?
SoniX
2008-06-15 09:55:03 UTC
Hi. I am a SQL newbie. I am using C# and Visual Studio 2005.
I have created a table and then I try to add a value to the table. It works, my coding works, becuase in the program I call the table again and it shows the new value, BUT, the moment when I close my program I lose those changes.

Why is this?

Here is some of my code.

SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = Properties.Settings.Default.Setting;

myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = ("UPDATE Stock SET Stock_Quan = " + newFigure +
" WHERE StockName = '" + stockBox.Text + "';");

myCommand.ExecuteNonQuery();

myConnection.Close();

ANY help will be so greatly appreciated. THANK YOU.
Four answers:
chuvek
2008-06-15 10:04:19 UTC
Try to save your changes.
Daniel B
2008-06-15 19:31:13 UTC
I've done a lot of SQL programming using .NET and that code looks fine to me, don't see any reason why it wouldn't work. You are not doing a transaction so as soon as ExecuteNonQuery is run the data will be committed to the database.



I would suggest opening the table with another tool, for example SQL Query Analyzer, or even connect to the table with MS Access. Find the record you are updating, run the code above and be sure the record changes, then exit the program and see if the change disappears. Maybe another part of your code is modifying the records after this code does.
MattsComputersAndService
2008-06-15 17:00:30 UTC
I use SQL using PHP, so correct me if I'm wrong. But are you even connecting to a database and table somewhere on a server? Otherwise I think it's just being saved temporarily in your application.
Patel Ankur
2008-06-15 17:01:46 UTC
may be ur connecttion string will be wrong







ANKUR PATEL

admin of

www.mobiledon.com

www.cellnxt.com


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...