Please I have been trying this code below to insert and delete record from my db but it compiles and when I check my db I dont see the update to show the query worked. But surprisingly if I use a select statement It gives me a sign that the query executed.

I would paste the logic that I am using to delete below (class code)

class Logic
 {
        SqlCeConnection con = null;

        //code the constructor of the class
        public Logic() { 
            //now we are going to collect the database configuration string
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectedCrud.Properties.Settings.DatabaseConnectionString"].ConnectionString;

            con = new SqlCeConnection(connectionString);

            Console.WriteLine(con.Database);
        }

      //This method is going to delete from the database
        public void deleteValue(int r){
            int k = 0;
            string sql = string.Format("delete from Details where id = {0}",r);
            using (SqlCeCommand command = new SqlCeCommand(sql, this.con)){
                try
                {
                    //open the connection 
                    this.con.Open();
                    k = command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally{
                    this.con.Close();
                }
            }

            if (k > 0)
            {
                Console.WriteLine("The rows as been deleted");
            }
            else
                Console.WriteLine("The row was not deleted");

        }

}

I have looked and looked and I am tried of looking. Is there something that I am suppose to do to my db to make my codes work. I have tried codes from two different textbooks on connected layer. But they give me the same confusing result.

Recommended Answers

All 4 Replies

Wich is the confusing result?

When I run the code and call the delete method. It does not affect the database content. I notice that the content of the database that I am trying to delete is till there. But if I run a select from the command prompt the returned value show the rows was deleted but it is still in the database when I check.

Did you use "Add DataSource" and click yes to copy local?

This copies the DB to your project folder. The default is to then copy from your project folder to the output (Debug) folder on the building the project. Your program is working on that copy in the output folder, but you are viewing the original in you project folder.

If you wish to stop this behavior, go to the properties on the DB and select "Copy to Output Directory - Do not copy".

Thanks I have solved the problem. The datasource was pointing to the db in the debug folder. so I went into the app config file and pointed it to the db copy in the project directory. Data Source = |Directory|/Database.sdf. I edited that line in apconfig. But for production sake. I would change it back as soon as am true developing so that it would point back to the db file in debug folder.

Thanks alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.