在C#中保存货币文本框值

时间:2018-04-17 08:55:14

标签: c#

我下载了这个用于货币文本框的课程。

当我在货币文本框中输入值时,代码工作正常。但是当我尝试从存储过程执行插入查询时,会出现此错误:

  

将数据类型nvarchar转换为十进制时出错。

我不知道错误在哪里。

这是类货币文本框的代码;

using System;
using System.Linq;
using System.Windows.Forms;
using System.Globalization;

namespace SSFASys
{
    public partial class currencytextbox:TextBox
    {
        readonly CultureInfo _ci = CultureInfo.InstalledUICulture;
        private readonly string _allowedCharacterSet;
        private readonly char _decimalSeparator;

        public currencytextbox()
        {
            var nf = new CultureInfo(_ci.Name, false).NumberFormat;
            _decimalSeparator = nf.CurrencyDecimalSeparator.ToCharArray()[0];
            _allowedCharacterSet = string.Format("0123456789{0}", _decimalSeparator);

            InitializeComponent();
        }
        /// Return Currency Text with No Formatting
        public string TextNoFormatting
        {
            get { return TypedText(); }
        }

        protected override void OnLeave(EventArgs e)
        {
            decimal amount;
            Text = decimal.TryParse(Text, NumberStyles.Currency, null, 
            out amount) ? amount.ToString("N") : 0.ToString("N");
            base.OnLeave(e);
        }

        private string TypedText()
        {
            var sonuc = string.Empty;
            return Text.Trim().Where(ch => _allowedCharacterSet.Contains(ch)).Aggregate
            (sonuc, (current, ch) => current + ch);
        }

        protected override void OnEnter(EventArgs e)
        {     
            Text = TypedText();
            base.OnEnter(e);
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && 
            e.KeyChar != _decimalSeparator && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
            base.OnKeyPress(e);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // currencytextbox
            // 
            this.Font = new System.Drawing.Font("Consolas", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ResumeLayout(false);

        }
    }
}

这是我用于插入货币文本框值的存储过程;

private void scholarshipinputs()
        {
            if (cmbclass.Text == string.Empty)
            {
                MessageBox.Show("Please fill-in required CLASSIFICATION OF SCHOLARSHIP to proceed.");

            else if (currencytextboxamount.Text == string.Empty)
            {
                MessageBox.Show("Please indicate a specific AMOUNT GRANTED for this SCHOLARSHIP PROGRAM to proceed.");
            }
            else if (cmbclass.Text == string.Empty && cmbtype.Text == string.Empty && cmbcode.Text == string.Empty && cmbdesc.Text == string.Empty && cmbterm.Text == string.Empty && currencytextboxamount.Text == string.Empty)
            {
                MessageBox.Show("Please fill-in all required fields to proceed.");
            }
            else
            {
                sqlcon.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "insertssfapsdetails";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = sqlcon;
                cmd.Parameters.Add(new SqlParameter("@ssfaptype", ssfaptype));
                cmd.Parameters.Add(new SqlParameter("@ssfapsclass", ssfapclass));
                cmd.Parameters.Add(new SqlParameter("@amount", currencytextboxamount.Text));
                cmd.Parameters.Add(new SqlParameter("@ssfapscode", cmbcode.Text));
                cmd.Parameters.Add(new SqlParameter("@ssfapsdesc", cmbdesc.Text));
                cmd.Parameters.Add(new SqlParameter("@term", cmbterm.Text));
                int rowsaffected = cmd.ExecuteNonQuery();
                if (rowsaffected > 0)
                {
                    MessageBox.Show("Scholarship program successfully included for the term.");
                    cmbclass.Refresh();
                    cmbtype.Refresh();
                    cmbcode.Text = "";
                    cmbdesc.Text = "";
                    cmbterm.Text = "";
                    currencytextboxamount.Text = "";
                }
                else 
                {
                    MessageBox.Show("No scholarship program included for the term. Please contact your system administrator to fix the problem.");
                }
                sqlcon.Close();
            }
        }

1 个答案:

答案 0 :(得分:1)

你的错误应该在这里:

 cmd.Parameters.Add(new SqlParameter("@ssfapsclass", ssfapclass));
            cmd.Parameters.Add(new SqlParameter("@amount", currencytextboxamount.Text));
            cmd.Parameters.Add(new SqlParameter("@ssfapscode", cmbcode.Text));
            cmd.Parameters.Add(new SqlParameter("@ssfapsdesc", cmbdesc.Text));
            cmd.Parameters.Add(new SqlParameter("@term", cmbterm.Text));

您正在尝试将文本写入数据库中的十进制字段,不确定它是哪个参数(可能是金额),但您需要先将其转换为十进制

与问题没有直接关系: 你应该使用

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();

}

即使程序抛出了您当前未处理的异常

,您的连接也将被关闭