-1

When i run the program it doesn't seem to have any errors but it doesn't insert any data to the database. Is there some essential code missing?

Here's my code:

            Using string connection = @"Data Source=|DataDirectory|\InvoiceDatabase.sdf";
            SqlCeConnection cn = new SqlCeConnection(connection);

            try
            {
                cn.Open();
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.ExitThread();
            }

            SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Client(Name, Address, Postcode, Telephone_Number)VALUES(@name, @address, @postcode, @tel)", cn);
            cmd.Parameters.AddWithValue("@name", txt_ClientName.Text);
            cmd.Parameters.AddWithValue("@address", txt_ClientAddress.Text);
            cmd.Parameters.AddWithValue("@postcode", txt_postcode.Text);
            cmd.Parameters.AddWithValue("@tel", txt_TelNo.Text);

            try
            {
                int affectedRows = cmd.ExecuteNonQuery();

                if (affectedRows > 0)
                {
                    txt_ClientAddress.Text = "";
                    txt_ClientName.Text = "";
                    txt_postcode.Text = "";
                    txt_TelNo.Text = "";
                    MessageBox.Show("Client: " + txt_ClientName.Text + " added to database. WOoo", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Client: " + txt_ClientName.Text + " Failed to add to database. WOoo", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

Thanks in advance for any feedback.

leppie
  • 115,091
  • 17
  • 196
  • 297
James Henry
  • 11
  • 1
  • 2
  • 5
  • What's the value of `affectedRows`? – LarsTech Mar 27 '13 at 16:00
  • possible duplicate of [Insert to a SQL Server CE database](http://stackoverflow.com/questions/15660112/insert-to-a-sql-server-ce-database) – leppie Mar 27 '13 at 16:05
  • Value of affectedRows = 1. – James Henry Mar 27 '13 at 16:06
  • What was wrong the answer you got the same question you asked only 2 hours ago? – leppie Mar 27 '13 at 16:06
  • @leppie. I re-coded the button again in a slightly different way. This is as far as i got. It works in terms of compiling. But just doesn't insert values to the database for some reason. The same code format worked for a different part of my application. – James Henry Mar 27 '13 at 16:09
  • @user2093030: You probably overwriting the database when you build. – leppie Mar 27 '13 at 16:10
  • @leppie: All dummy data that i have in the database doesn't get affected. – James Henry Mar 27 '13 at 16:17
  • What is the value of `|DataDirectory|`? By default VS sets it to a temporary file, not the original source file. Read http://stackoverflow.com/questions/12093962 and http://stackoverflow.com/questions/11801352. – Dour High Arch Mar 27 '13 at 16:39

1 Answers1

1

Please make sure that you watch the database where you add the rows.

In most cases, the database .sdf file gets copied to the release folder and you work on that, while the server explorer has opened some other database file.

Try and open the .sdf file under Release, check if rows where added there.

Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • This is gonna make me sound so stupid but after reading your answer a looked at the connection string to the database folder and i was using some weird database connection string. Got it fixed not thanks. Would upvote but my reps too low because people are so ignorant and because they know it doesn't mean i do. Thanks again you are a Legend!!! – James Henry Mar 29 '13 at 15:07
  • Think everybody working with sql ce has stumbled on this a few times. No problem. And if it helped you, you can still accept it as answer ;) – Mare Infinitus Mar 29 '13 at 15:20