How can I update a cell value in a dB table, using SQL Server CE and C# (Visual Studio 2010)
You have to tell us exactly what connection string you are using, saying things like "I updated the connection to point to the database in the bin folder" is not helpful. Are you using a connection string like c:\Users\Me\Visual Studio 2010\Projects\MyProject\bin\debug\HKRMoviesDB.sdf
? That is not right, that is hard-coded to your VS2010 debug test folder on your machine, and will not work for anyone else, will only work when you run from the debugger, and will not work when you deploy your app.
ADO.NET looks for vertical bars in connection strings and expands the enclosed string using the AppDomain property. For example, if you use Data Source=|DataDirectory|\HKRMoviesDB.sdf
from the VS debugger, DataDirectory
expands to your project's bin folder, where VS copies the debug DLLs. If you deploy your app using an MSI installer, DataDirectory
expands to your app's install folder chosen by the user. Further explanation is at this answer.
Be aware that DataDirectory
may not be a directory writable by users; it is a good directory for read-only data, but if you want users to write to the file you should copy it to Environment.SpecialFolder.ApplicationData
or somewhere in your app, as explained in this answer.
There is one more thing to keep in mind. When you are debugging you typically want to copy your original source data to the debug folder, test it, then discard your test data because you typically do not want your test data included in the final deployment. This is what the "Copy to Output Directory" property in your project does. If it is set to "copy if newer", which it probably should be, then any changes you make to your test data is temporary; the next time you update your original source data in your project, your test data will be discarded. Remember, your application's deployment directory is typically not writable by users so you should not be writing user data in DataDirectory
anyway.
SQL CE Not entering information to DB
Try this:
if (checkPasswordsMatch() == "B")
{
SqlCeConnection myConnection = new SqlCeConnection("Data Source = pwdb.sdf");
try
{
myConnection.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
SqlCeCommand myCommand = myConnection.CreateCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = "INSERT INTO PW Values ('Master', '" + saltedcryps + "', '" + hashedResult + "');"
myCommand.ExecuteNonQuery();
myConnection.Close();
this.Hide();
}
If this doesnt works, try to put the absolut path on the connection string.
How am i using the SclCeCommand and ExecuteNonQuery method wrong?
This is a possible scenario.
Your connection string use |DataDirectory|
. This means that at RUNTIME, inside VS debug session, the substitution string points to the folder BIN\DEBUG
where is located the file sdf copied by VS when it starts the debug session (Check the property Copy to Output directory
for your sdf file).
The insert works flawlessy. (you can check that looking at the return value of ExecuteNonQuery()).
You stop the debug and check the content of your table using Server Explorer but you don't find anything.
The only problem is the connection used by your Server Explorer window that point to a different database. The one in the project folder.
I suggest to create two connections on Server Explorer. Leave the current one as is (so this db is your main empty copy ready to be deployed with your application) and add another connection that points to the db in the PROJECTFOLDER\BIN\DEBUG where is located the db used during the debug sessions (Right click 'Add Connections', Select Sql Server Compact Edition and browse to the BIN\DEBUG folder)
Local SDF No Data After insert
It is a Classic - you are probably using |DataDirectory| in your connection string, and has the database file as project content, so you should look in bin/debug for a copy of the database file with data in it
Related Topics
Why Does System.Threading.Timer Stop on Its Own
What Are the Differences Between the Xmlserializer and Binaryformatter
Collection Was Modified; Enumeration May Not Execute Error When Removing a Listitem from a Listbox
How Are Nullable Types Implemented Under the Hood in .Net
Entity Framework Queryable Async
Is There a Faster Way Than This to Find All the Files in a Directory and All Sub Directories
Referenced Project Gets "Lost" at Compile Time
Why Would One Ever Use the "In" Parameter Modifier in C#
JSON.Net Adding Backslash While Returning JSON Serialized String
Why Can't I Use Interface with Explicit Operator
Allow Access Permission to Write in Program Files of Windows 7
ASP.NET Dynamically Button with Event Handler
System.Badimageformatexception: Reference Assemblies Should Not Be Loaded for Execution
Unique Ways to Use the Null Coalescing Operator
How to Compile and Run C# Program Without Using Visual Studio
How to Create an Audit Trail with Entity Framework 5 and MVC 4