我的表单不会将数据发送到DB

时间:2013-11-24 10:34:16

标签: c# asp.net visual-studio-2010

我正在学习本教程

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

现在我完成了它,当我按下发送时数据不会进入数据库,但是代码确实在Web服务页面中运行,而不是表单。

我的网络服务代码:

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;
        }

和我的.aspx.cs代码:

 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";
            }
        }

编辑:

所以在某些时候我更改了按钮名称,这就是它没有运行的原因,但是在更改按钮名称并单击按钮后,我遇到了崩溃,程序指向Web服务中的conn字符串,并且说Keyword not supported: 'initialcatalog'.

3 个答案:

答案 0 :(得分:1)

将您的连接放在using块中,以便关闭并提交事务。

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

呼叫

con.Close();

答案 1 :(得分:0)

您需要在提供ProviderName时将connection string指定为第二个参数。

我建议您在web.config文件中添加c onnectionstringprovidername,并从后面的代码中访问它们。

将以下语句添加到web.config文件

web.config

<connectionStrings>
<add name="ConnectionString1" providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=collegedatabase;Integrated Security=True;Pooling=False" />
</connectionStrings>

要从connectionctring文件访问web.config,请尝试以下操作:

代码背后:

String connectionstring=System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;

完整代码:

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)
        {
String connectionstring=System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;

            con = new SqlConnection(connectionstring);
            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;
        }

答案 2 :(得分:0)

我必须更改按钮名称才能匹配,还必须删除using block