Search code examples
c#winformssql-server-ce

Can't commit changes to SQL Server Compact Edition database


I have a problem,

private void button_Submit_Click(object sender, EventArgs e)
{
   try
   {
      string connectionString = @"Data Source=Database_TouchPOS.sdf;Persist Security Info=False;";
      using (SqlCeConnection connection = new SqlCeConnection(connectionString))
      {
         using (SqlCeCommand command = connection.CreateCommand())
         {
            connection.Open();
            command.CommandText = "INSERT INTO Product (Title,Price,Category_Id) VALUES (@title, @price,@category_Id)";
            command.Parameters.AddWithValue("@title", textBox_Title.Text);
            command.Parameters.AddWithValue("@price", textBox_Price.Text);
            command.Parameters.AddWithValue("@category_Id", comboBox_Category.SelectedIndex);

            command.ExecuteNonQuery();

            MessageBox.Show("Product Added Successfully...");
         }
         connection.Close();
      }                
   }
   catch (SqlException ex)
   {
      MessageBox.Show(ex.Message);
   }
}

Everything seem to be fine, but still can't add data into database.

  1. I did try with complete database path, example c:\project\database.sdf
  2. I did a lot of search before asking. I saw similar problems but not even a single one works for me.
  3. Data are added when compiling but not committed to database. I can see the data after second attempt of debugging.
  4. Please kindly try to explain in detail.

Thanks


Solution

  • You are probably facing this, http://erikej.blogspot.com/2010/05/faq-why-does-my-changes-not-get-saved.html suggest you use a full path to your database file in your connection string.