2015-11-14 72 views
0

我希望能夠使用Windows窗體中的條目更新我的SQL數據表,但是我嘗試了一切,並且它不工作。 我一直有錯誤,尤其是「未能將字符串轉換爲十進制」。 我有兩個表,一個是總體成員表,第二個是用日期付款的經常記錄表。用Windows窗體條目更新SQL表格

存款部分應該爲每個會員添加並鏈接到會員表,這是我無法實現的。

//NC-4 Create account. 
    private void btnCreateAccount_Click(object sender, EventArgs e) 
    { 

     //NC-5 Set up and run stored procedure only if Customer Name is present. 
     if (isCustomerName()) 
     { 
      //NC-6 Create the connection. 
      SqlConnection conn = new SqlConnection(connstr); 


      //NC-7 Create a SqlCommand, and identify it as a stored procedure. 
      SqlCommand cmdNewCustomer = new SqlCommand("dbo.epaNewCustomer", conn); 
      cmdNewCustomer.CommandType = CommandType.StoredProcedure; 



      //NC-8 Add input parameter from the stored procedure and specify what to use as its value. 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@titheID", SqlDbType.NChar, 10, "titheID")); 
      cmdNewCustomer.Parameters["@titheID"].Value = txtTitheID.Text; 
      this.txtMemberID.Text = Convert.ToString(parsedMembersID); 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@surName", SqlDbType.NVarChar, 50, "surName")); 
      cmdNewCustomer.Parameters["@surName"].Value = txtSurname.Text; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50, "Name")); 
      cmdNewCustomer.Parameters["@Name"].Value = txtName.Text; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@otherName", SqlDbType.NVarChar, 50, "otherName")); 
      cmdNewCustomer.Parameters["@otherName"].Value = txtOthernames.Text; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@Gender", SqlDbType.NChar, 6, "Gender")); 
      cmdNewCustomer.Parameters["@Gender"].Value = cmbGender.Text; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@dob", SqlDbType.DateTime, 8, "dob")); 
      cmdNewCustomer.Parameters["@dob"].Value = dptDob.Value; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int, 8, "Age")); 
      cmdNewCustomer.Parameters["@Age"].Value = txtAge.Value; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@phone", SqlDbType.Decimal, 25, "phone")); 
      cmdNewCustomer.Parameters["@phone"].Value = txtPhone.Text; 


      cmdNewCustomer.Parameters.Add(new SqlParameter("@email", SqlDbType.NVarChar, 50, "email")); 
      cmdNewCustomer.Parameters["@phone"].Value = txtEmail.Text; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@addressHouse", SqlDbType.NVarChar, 1000, "addressHouse")); 
      cmdNewCustomer.Parameters["@addressHouse"].Value = txtAddress.Text; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@stateOrigin", SqlDbType.NVarChar, 50, "stateOrigin")); 
      cmdNewCustomer.Parameters["@stateOrigin"].Value = txtStateOrigin.Text; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@country", SqlDbType.NVarChar, 50, "country")); 
      cmdNewCustomer.Parameters["@country"].Value = txtCountry.Text; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@profession", SqlDbType.NVarChar, 50, "profession")); 
      cmdNewCustomer.Parameters["@profession"].Value = txtProfession.Text; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@department", SqlDbType.NVarChar, 15, "department")); 
      cmdNewCustomer.Parameters["@department"].Value = cmbDepartment.Text; 

      cmdNewCustomer.Parameters.Add(new SqlParameter("@Amount", SqlDbType.Money, 8, "Amount")); 
      amount = txtDeposit.Value; 
      string Amount = String.Format("Amount: {0:C}", amount); 
      cmdNewCustomer.Parameters["@Amount"].Value = Amount; 


      //NC-9 Add output parameter. 


      cmdNewCustomer.Parameters.Add(new SqlParameter("@titheID", SqlDbType.Int)); 
      cmdNewCustomer.Parameters.Add(new SqlParameter("@titheID", SqlDbType.NChar, 10, "titheID")); 
      cmdNewCustomer.Parameters["@titheID"].Value = this.parsedMembersID; 
      cmdNewCustomer.Parameters["@titheID"].Direction = ParameterDirection.Output; 
      bool isDecimal = decimal.TryParse(txtPhone.Text.Trim(new[] { '\"' }), out phone); 
      if (isDecimal) 
       cmdNewCustomer.Parameters["@phone"].Value = phone; 



      //NC-10 try-catch-finally 
      try 
      { 
       //NC-11 Open the connection. 
       conn.Open(); 

       //NC-12 Run the stored procedure. 
       cmdNewCustomer.ExecuteNonQuery(); 


       MessageBox.Show(" Congratulations!. Account" + " for " + txtName.Text + " with TitheID " + txtTitheID.Text + " has been created successfully."); 
      } 
      catch (SqlException se) 
      { 
       //NC-14 A simple catch. 

       MessageBox.Show("Sorry, Error in Transaction. Account" + " for " + txtName.Text + " with TitheID " + txtTitheID.Text + " could not be created."); 

       MessageBox.Show(se.Message); 
      } 
      finally 
      { 


       //NC-15 Close the connection. 
       conn.Close(); 

      } 
     } 

    } 
+0

PhoneNumber是小數點嗎? – Steve

+0

是Steve,電話號碼在SQL表中是十進制的 –

+1

你的問題是什麼? ho轉換爲十進制? – CPMunich

回答

0

只是一個猜測,但你從文本字段從電子郵件字段指定電話,也:

cmdNewCustomer.Parameters["@phone"].Value = txtPhone.Text; 
cmdNewCustomer.Parameters["@phone"].Value = txtEmail.Text; 

然後你也有這樣的:

cmdNewCustomer.Parameters["@phone"].Value = phone; 

至少我會嘗試刪除前兩個。

如果這沒有幫助,請使用分析器查看您實際發送的值到數據庫。

+0

我糾正了這一點;即使在我發佈了這個之後,就在今天上午再次通過代碼。 –

+0

我修復了它們。 –