4 Replies - 1068 Views - Last Post: 23 February 2011 - 12:41 PM Rate Topic: -----

#1 arkatec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-February 11

Execute NonQuery not adding row to database

Posted 19 February 2011 - 07:02 AM

I have a form that I'm trying to add to a database. It runs ok, but it's not inserting a row into my SQL server database. Where am I going wrong?

private void btnMemberAdd_Click(object sender, EventArgs e)
        {
            using (SqlConnection cs = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\CapitalCity107.mdf;Integrated Security=True;User Instance=True"))
            {
                using (SqlCommand insert = cs.CreateCommand())
                {

                    insert.CommandText = "INSERT INTO members (Salutation, FirstName, MInitial, LastName, Address1, Address2, City, State, Zip, HomePhone, MobilePhone, Email, DOB, DateRaised) VALUES (@Salutation, @FirstName, @MInitial, @LastName, @Address1, @Address2, @City, @State, @Zip, @HomePhone, @MobilePhone, @Email, @DOB, @DateRaised)";

                    insert.Parameters.AddWithValue("@Salutation",salutationBox.SelectedItem);
                    insert.Parameters.AddWithValue("@FirstName", txtBoxFName.Text);
                    insert.Parameters.AddWithValue("@MInitial", txtBoxInitial.Text);
                    insert.Parameters.AddWithValue("@LastName", txtBoxLName.Text);
                    insert.Parameters.AddWithValue("@Address1", txtBoxAddress1.Text);
                    insert.Parameters.AddWithValue("@Address2", txtBoxAddress2.Text);
                    insert.Parameters.AddWithValue("@City", txtBoxCity.Text);
                    insert.Parameters.AddWithValue("@State", stateBox.SelectedItem);
                    insert.Parameters.AddWithValue("@Zip", txtBoxZip.Text);
                    insert.Parameters.AddWithValue("@HomePhone", txtBoxHomePhone.Text);
                    insert.Parameters.AddWithValue("@MobilePhone", txtBoxMobilePhone.Text);
                    insert.Parameters.AddWithValue("@Email", txtBoxEmail.Text);
                    insert.Parameters.AddWithValue("@DOB", Convert.ToDateTime(txtBoxBirth.Text));
                    insert.Parameters.AddWithValue("@DateRaised",Convert.ToDateTime(txtBoxRaised.Text));

                    cs.Open();
                    insert.ExecuteNonQuery();
                    cs.Close();

                    MessageBox.Show("Entry Added");

             }
            }
        }


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 19 February 2011 - 09:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Execute NonQuery not adding row to database

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Execute NonQuery not adding row to database

Posted 19 February 2011 - 08:04 AM

Hi,

Where is your .mdf file located? If isn't in the bin/debug folder of your project, try moving it to that folder and run the code again. I had a bizarre problem with an update statement ages ago...

The problem in that instance was the 'Copy to Output Directory' of the database... If moving to the debug folder makes a difference, it is likely that it is the value of that property that is causing the problem, and you need to change it to something other than 'Copy Always' :)

This post has been edited by CodingSup3rnatur@l-360: 19 February 2011 - 08:32 AM

Was This Post Helpful? 2
  • +
  • -

#3 arkatec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-February 11

Re: Execute NonQuery not adding row to database

Posted 19 February 2011 - 08:50 AM

It worked!!! Thanks alot. This issue had been driving me crazy.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Execute NonQuery not adding row to database

Posted 19 February 2011 - 09:03 AM

If you post code again, please obey the rules and USE CODE TAGS!!!

:code:

If that's too complicated, see the first link in my signature.
Was This Post Helpful? 0
  • +
  • -

#5 arkatec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-February 11

Re: Execute NonQuery not adding row to database

Posted 23 February 2011 - 12:41 PM

It has happened again. No matter what I set the Copy to Output Directory property to it, or if I move the .mdf file to the bin directory. It still won't show that I've inserted a row into the database.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1