2013-11-24 50 views
-1

我下面這個教程我的表單不發送數據到數據庫

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

而現在,我完成了它,當我按下發送數據不會進入數據庫,但是代碼中並運行在Web服務頁面中,不是表單。

我的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'.

+0

你遇到的任何錯誤是什麼溫度的運行之後的結果? 「InsertPerson」方法? – User2012384

+0

不,沒有錯誤或警告 – user2469932

+0

運行cmd.ExecuteNonQuery();後,「roweffected」的結果是什麼? – User2012384

回答

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(); 
+0

試過這個,表單仍然沒有發送數據。 – user2469932

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> 

訪問connectionctringweb.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; 
     } 
+0

這在Visual Studio上本地運行,Conn字符串是從VS字符串中直接獲得的,它可以工作。 – user2469932

+0

@ user2469932:在連接字符串中刪除'Integrated Security = True',然後嘗試 –

+0

已編輯,請參閱上述 – user2469932

0

我不得不改變按鈕的名稱相匹配,也不得不刪除using block

+0

使用塊比您當前的代碼更推薦,我沒有看到任何con.close()代碼畢竟 – user2705620