M.Karavural
M.Karavural

Reputation: 23

ExecuteNonQuery is working but , changes are not saving

here is the code im working on ;

 public partial class Form2 : Form
{
    SqlConnection sc = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True");
    SqlDataAdapter sda;
    SqlCommand command;
    SqlCommand commands;
    public Form2()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
        Form1 f1 = new Form1();
        f1.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        command = new SqlCommand(@"SELECT * FROM [Table] WHERE email='" + textBox4.Text + "'", sc);
        sda = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        int i = ds.Tables[0].Rows.Count;
        if (i == 1)

            MessageBox.Show("Email Already Taken");
        else
        {
            sc.Open();
            command = new SqlCommand("INSERT INTO [Table](name,surname,yearofbirth,adress_home_city,adress_home_block,adress_home_street,adress_work_city,adress_work_block,adress_work_street,email,password) VALUES('"+textBox1.Text+"','"+textBox2.Text+ "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox7.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox6.Text + "') ", sc);

            command.ExecuteNonQuery();
            sc.Close();
            MessageBox.Show("Success");

        }

its working when im debugging it , its a register form and i can login with the information i give in this form.

but when closed , this insert command i do doesnt get saved in my database.

// additional info ;

when in register form , i get the success message and if i try again with same information , i get the email is already taken message

Upvotes: 2

Views: 1591

Answers (1)

Abhimanyu Ghei
Abhimanyu Ghei

Reputation: 176

The issue is because your mdf file is getting replaced by a blank one on rebuild i.e. your mdf file is being copied to your debug folder everytime you rebuild your application and so removing all the data that was inserted previously.

Solution

  1. Do not include the mdf file in solution.
  2. Provide full path (like AttachDbFilename=C:\Database1.mdf;) or relative path of mdf file in your connection string

SqlConnection sc = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Database1.mdf;Integrated Security=True");

  1. If you are including the mdf file to solution then change to property "Copy to Output Directory" as "Do not copy"
  2. Use database from your local MSSQL server instance instead of pointing to a mdf file if possible (MS SQLExpress is free).

This issue will only happen if you launch your application from visual studio, if you are launching the application multiple times from debug/release folder directly the application will work fine as it is not being rebuild and so mdf file is not replaced.

Upvotes: 2

Related Questions