-2

When i try to insert the value from the textbox into the database, the values are not being updated in the database. But the newly inserted rows are temporarily available, after inserting when i use select query to get the rows, the new rows are available. When i close the solution and open it again, the newly insert rows are gone. The table in the database explorer is always not updated.

Here is my code.

            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
            string na = textBox1.Text;
            int ag = int.Parse(textBox2.Text);
            string ci = textBox3.Text;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {

                using (SqlCommand insertCommand = connection.CreateCommand())
                {
                    insertCommand.CommandText = "INSERT INTO address(name,age,city) VALUES (@na,@ag,@ci)";
                    insertCommand.Parameters.AddWithValue("@na", na);
                    insertCommand.Parameters.AddWithValue("@ag", ag);
                    insertCommand.Parameters.AddWithValue("@ci", ci);


                    insertCommand.Connection.Open();
                    insertCommand.ExecuteNonQuery();
                    insertCommand.Connection.Close();
                    MessageBox.Show("finish");

                }

                //connection.Close();
            }

The table name is "address" and has three fields name(varchar(50)),age(int),city(nchar(10)) Please help.

4
  • didnt u just ask this question? stackoverflow.com/questions/11213091/… Commented Jun 26, 2012 at 18:56
  • 1
    possible duplicate of Inserting into database from textbox in C# using parameters Commented Jun 26, 2012 at 18:57
  • that question is different. I was not able to insert into the database, now the data is temporarily available and not persisted. Please understand. Commented Jun 26, 2012 at 19:01
  • @dnivra - this is the same question as the link Jane Doe posted. If it is a separate issue, then please accept an answer to the previous step. I understand that you moved on to the next step, but since your last question had activity less than 10 minutes ago, it is inappropriate to repost without closing the last question. Commented Jun 26, 2012 at 19:01

2 Answers 2

2

Try with Right click on your mdf file, select properties, and select not copy in order to not copy in your bin.

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

I agree that this appears to be the issue.
An attempt to attach an auto-named database for file C:\Users\Thangamani\documents\visual studio 2010\Projects\AddressBook\AddressBook\bin\Debug\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
what's the detail of exception ?
I get this exception when i select Do not copy.
it's possible you have two databases with the same name, open your explorator server and delete one
|
0

You said "C:\Users\Thangamani\documents\visual studio 2010\Projects\AddressBook\AddressBook\bin\Debug\" in the error. You are attaching to the local copy during debug.

Now that you are not copying the database over, that mdf file does not exist, hence the error.

Change your connection string to access the original database, not the local copy (which you are not making anymore).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.