Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
166 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...