My form does not send data to DB

advertisements

I was following this tutorial

http://www.c-sharpcorner.com/UploadFile/1d42da/a-xml-web-service-that-update-data-into-a-default-table-of-t/

And now that I finished it, when I press send the data does not go into the DB, however the code does run in the web service page, just not the forms.

My web service code:

public class SampleService : System.Web.Services.WebService
    {
        SqlConnection con;
        SqlCommand cmd;

        [WebMethod]
        public int insertPerson(string firstName, string lastName, string DOB, int phoneNumber, string address, int postCode)
        {
            con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=collegedatabase;Integrated Security=True;Pooling=False");
            con.Open();
            cmd = new SqlCommand("INSERT INTO person (firstName, lastName, DOB, phoneNumber, address, postCode) VALUES (@firstName, @lastName, @DOB, @phoneNumber, @address, @postCode)", con);
            cmd.Parameters.AddWithValue("@firstName", firstName);
            cmd.Parameters.AddWithValue("@lastName", lastName);
            cmd.Parameters.AddWithValue("@DOB", DOB);
            cmd.Parameters.AddWithValue("@phoneNumber", phoneNumber);
            cmd.Parameters.AddWithValue("@address", address);
            cmd.Parameters.AddWithValue("@postCode", postCode);
            int roweffected = cmd.ExecuteNonQuery();

            return roweffected;
        }

and my .aspx.cs code:

 protected void Button1_Click(object sender, EventArgs e)
        {
            string firstName = TextBox34.Text;
            string lastName = TextBox35.Text;
            string  DOB = TextBox36.Text;
            int phoneNumber = Convert.ToInt32(TextBox38.Text);
            string address = TextBox37.Text;
            int postCode = Convert.ToInt32(TextBox39.Text);
            SampleService myservice = new SampleService();
            int temp = myservice.insertPerson(firstName, lastName, DOB, phoneNumber, address, postCode);
            if (temp == 1)
            {
                messageLabel.Text = "record is update";
            }
            else
            {
                messageLabel.Text = "record is not update";
            }
        }

EDIT:

So at some point I changed the button name and that's why it wasn't running, however upon changing the button name and clicking the button, I get a crash and the program points to the conn string in the web service, and says that Keyword not supported: 'initialcatalog'.


Place your connection in a using block so that it closes and commits the transaction.

public int insertPerson(string firstName, string lastName, string DOB, int phoneNumber, string address, int postCode)
    {
       using(SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;InitialCatalog=collegedatabase;Integrated Security=True;Pooling=False"))
       {
        con.Open();
        cmd = new SqlCommand("INSERT INTO person (firstName, lastName, DOB,     phoneNumber, address, postCode) VALUES (@firstName, @lastName, @DOB, @phoneNumber, @address, @postCode)", con);
        cmd.Parameters.AddWithValue("@firstName", firstName);
        cmd.Parameters.AddWithValue("@lastName", lastName);
        cmd.Parameters.AddWithValue("@DOB", DOB);
        cmd.Parameters.AddWithValue("@phoneNumber", phoneNumber);
        cmd.Parameters.AddWithValue("@address", address);
        cmd.Parameters.AddWithValue("@postCode", postCode);
        int roweffected = cmd.ExecuteNonQuery();

        return roweffected;
        }
    }

OR

call

con.Close();