使用Windows窗体条目

时间:2015-11-14 00:00:18

标签: c# sql-server

我希望能够使用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();

            }
        }

    }

1 个答案:

答案 0 :(得分:0)

只是一个猜测,但您从文本字段和电子邮件字段分配电话:

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

然后你也有了这个:

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

至少我会尝试删除前两个。

如果这没有帮助,请使用分析器查看您实际发送到数据库的值。