0

I am making simple application. It has only one button "Add" to add new data in database. First it didn't insert data, i checked it when i press right button on table and then select "Show Table Data" it showed nothing. And when i stated program again, datagridview also was empty. After that i changed my database option "Copy to Output" in Solution Explorer to "Do not Copy" it appears that error. When I press that button, it occurs with that exception

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: An attempt to attach an auto-named database for file c:\users\sanan\documents\visual studio 2013\Projects\Test\Test\bin\Debug\Base.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Also I have database called "Base.mdf" with table "Stu" in project folder (not in debug)

This is code to add

    private void Form1_Load(object sender, EventArgs e)
    {
        //this.stuTableAdapter.Fill(this.baseDataSet.Stu);
    }

    private void button1_Click(object sender, EventArgs e)
    {

        SqlConnection connection = new SqlConnection
            (@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Base.mdf;Integrated Security=True;");
        connection.Open();
        SqlCommand command = new SqlCommand("insert into Stu values(@id,@Name,@SurName)",connection);
        command.Parameters.AddWithValue("@id", int.Parse(textBox1.Text));
        command.Parameters.AddWithValue("@Name", textBox2.Text);
        command.Parameters.AddWithValue("@SurName", textBox3.Text);

        command.ExecuteNonQuery();

        this.stuTableAdapter.Fill(this.baseDataSet.Stu);
    }
}

}

panda007
  • 17
  • 1
  • 8
  • 1
    Did you read the error message? Did you verify that `Base.mdf` gets copied to the `|DataDirectory|` (likely the `bin\{BuildConfiguration}\` directory)? Or is there already a database with the same name attached to the SQL Server instance? Or is your project directory on a network share/drive? – stakx - no longer contributing Jun 22 '15 at 10:40
  • 1
    error message seems to fairly explicitly state what is going wrong. – user1666620 Jun 22 '15 at 10:40
  • @stakx i dont have database in bin folder, this error appered when I made my database option "Copy to Output Directory" to "Do not copy". I made it because that database is local, was created in Visual Studio, add code above didn't insert values. – panda007 Jun 22 '15 at 11:17
  • @panda007: You just stated what caused the error initially. The error message you get explicitly states this as one possible cause. So where do you need any further help? – stakx - no longer contributing Jun 22 '15 at 11:37
  • @stakx I want to insert value in my local database. First it didn't insert and then when i made "do not copy" began to appear that error – panda007 Jun 22 '15 at 11:48
  • @panda007: How did you verify that "it didn't insert"? Please add all these relevant details to your question (by editing it). – stakx - no longer contributing Jun 22 '15 at 11:50
  • @stakx I edited question, please help – panda007 Jun 22 '15 at 11:58

1 Answers1

0

though I do not know the final (working) solution to this problem, the cause most likely has to do with your database.mdf residing in your project directory. The settings of the .mdf and .ldf in the properties window might show always copy if newer.
The Database or Server Explorer looks in your project directory and changes are done in the subfolder /bin/debug. Somehow VS gets confused. Have a look here:

Why saving changes to a database fails

Regards Andi

Community
  • 1
  • 1
Andi
  • 57
  • 4