vladponcea
vladponcea

Reputation: 35

I can't add records to the database

I am working on an app and I got stuck in this problem. I am trying to add a record to my SQL local database. It gives the successful message box but when I close the app and check the table the record is not there. I tried adding it in two different ways and none of them worked. I will post the code here

Here is the first method that I tried

using (SqlConnection _conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Restaurant.mdf;Integrated Security=True"))
                {
                    using (SqlCommand _cmd = new SqlCommand("INSERT INTO Chelneri(username, nume, prenume, email, parola) values ('" + textBoxUser.Text.ToString().Trim() +"', '"+ textBoxNume.Text.ToString().Trim() +"', '"+ textBoxPrenume.Text.ToString().Trim() +"', '"+ textBoxEmail.Text.ToString().Trim() +"', '"+ textBoxParola.Text.ToString().Trim() +"')", _conn))
                    {
                        try
                        {
                            _conn.Open();
                            _cmd.ExecuteNonQuery();
                            _conn.Close();
                            textBoxUser.Text = string.Empty;
                            textBoxNume.Text = string.Empty;
                            textBoxPrenume.Text = string.Empty;
                            textBoxParola.Text = string.Empty;
                            textBoxEmail.Text = string.Empty;
                            MessageBox.Show("Utilizatorul a fost creat!", "Restaurant Casa Verde", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "Restaurant Casa Verde", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            _conn.Close();
                        }
                    }
                }

And here is the second one.

using (SqlConnection _conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Restaurant.mdf;Integrated Security=True"))
                {
                    using (SqlCommand _cmd = new SqlCommand("INSERT INTO Chelneri(username, nume, prenume, email, parola) values (@user, @nume, @prenume, @email, @parola)", _conn))
                    {
                        try
                        {
                            _conn.Open();

                            _cmd.Parameters.Clear();
                            _cmd.Parameters.Add("@user", SqlDbType.NVarChar).Value = textBoxUser.Text.ToString().Trim();
                            _cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = textBoxEmail.Text.ToString().Trim();
                            _cmd.Parameters.Add("@nume", SqlDbType.NVarChar).Value = textBoxNume.Text.ToString().Trim();
                            _cmd.Parameters.Add("@prenume", SqlDbType.NVarChar).Value = textBoxPrenume.Text.ToString().Trim();
                            _cmd.Parameters.Add("@parola", SqlDbType.NVarChar).Value = textBoxParola.Text.ToString().Trim();

                            _cmd.ExecuteNonQuery();
                            _conn.Close();
                            textBoxUser.Text = string.Empty;
                            textBoxNume.Text = string.Empty;
                            textBoxPrenume.Text = string.Empty;
                            textBoxParola.Text = string.Empty;
                            textBoxEmail.Text = string.Empty;
                            MessageBox.Show("Utilizatorul a fost creat!", "Restaurant Casa Verde", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "Restaurant Casa Verde", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            _conn.Close();
                        }
                    }
                }

I don't think the code is the problem. I think that I am missing something and I don't know what. Here is a printscreen of the database.

Upvotes: 0

Views: 484

Answers (3)

NEOhitokiri
NEOhitokiri

Reputation: 539

You are using "using", you don't need _cmd.Close();

Upvotes: 0

Fatihi Youssef
Fatihi Youssef

Reputation: 411

I think your code works fine, but you are missing a simple touch check-in Restaurant.mdf is moving with your application, if not do this steps:

  1. Right-click on Restaurant.mdf and click properties.
  2. build options -> select if newer.

And also make sure that you are checking the correct database file.

If you run on debug mode, go to Bin\Debug folder and check the mdf file in there, if you run on release mode you need to check on Bin\Release folder.

Source : codeproject

Upvotes: 0

Akbar Asghari
Akbar Asghari

Reputation: 681

hi I think your connection string is wrong first read this page for get true connection string from your sql server second read this page for true code for insert in database in C#

and I suggest use try catch like this

try
  {

  }
catch
  {

  }
finally { cnn.close}

I hope it will help you

Upvotes: 1

Related Questions