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

时间:2012-10-02 07:53:25

标签: c# asp.net

我正在努力解决错误。我检查了我的存储过程和代码,但我不知道问题实际存在的内容和位置。 The Error.

MY SP:

    ALTER PROCEDURE [dbo].[sp_Tbl_Contract_Insert]
    -- Add the parameters for the stored procedure here
    @ItemID int,
    @BrandID int,
    @CountID int,
    @SellerID int,
    @BuyerID int,
    @CountryFromID int,
    @countryToID int,
    @UnitID int,
    @KeyWinCountNumber varchar(50),
    @ContractNumber varchar(50),
    @ContractDate varchar(50),
    @TotalQty varchar(50),
    @Vans varchar(50),
    @UnitPrice decimal(18,2),
    @TotalAmount decimal(18,2)


    AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.

    -- Insert statements for procedure here

    Insert into Tbl_Contract (ItemID,BrandID,CountID,SellerID,BuyerID,
    CountryFromID,CountryToID,UnitID,
    KeyWinCountNumber,ContractNumber,ContractDate,TQty,Vans,
    UnitPrice,TAmount) values ( @ItemID,@BrandID,@CountID,@SellerID,@BuyerID,@CountryFromID,
    @countryToID,@UnitID,@KeyWinCountNumber,@ContractNumber,@ContractDate,@TotalQty,
    @Vans,@UnitPrice,@TotalAmount)

END

CODE:

if (ViewState["action"].ToString() == "Insert")
    {
        try
        {
            obj = new DalLib();
            paramArray = new string[16, 2];
            paramArray[0, 0] = "@ItemID";
            paramArray[0, 1] = ddlItem.SelectedValue.ToString();
            paramArray[1, 0] = "@BrandID";
            paramArray[1, 1] = ddlBrand.SelectedValue.ToString();
            paramArray[2, 0] = "@CountID";
            paramArray[2, 1] = ddlCount.SelectedValue.ToString();
            paramArray[3, 0] = "@SellerID";
            paramArray[3, 1] = ddlSellerName.SelectedValue.ToString();
            paramArray[4, 0] = "@BuyerID";
            paramArray[4, 1] = ddlBuyerName.SelectedValue.ToString();
            paramArray[5, 0] = "@CountryFromID";
            paramArray[5, 1] = ddlCountryFrom.SelectedValue.ToString();
            paramArray[6, 0] = "@countryToID";
            paramArray[6, 1] = ddlCountryTo.SelectedValue.ToString();
            paramArray[7, 0] = "@UnitID";
            paramArray[7, 1] = ddlUnit.SelectedValue.ToString();
            paramArray[8, 0] = "@KeyWinCountNumber";
            paramArray[8, 1] = txtkeyWinCount.Text;
            paramArray[9, 0] = "@ContractNumber";
            paramArray[9, 1] = txtContractNum.Text;
            paramArray[10, 0] = "@ContractDate";
            paramArray[10, 1] = txtContractDate.Text;
            paramArray[11, 0] = "@TotalQty";
            paramArray[11, 1] = txttotalqty.Text;
            paramArray[12, 0] = "@Vans";
            paramArray[12, 1] = txtVans.Text;
            paramArray[13, 0] = "@UnitPrice";
            paramArray[13, 1] = txtUnitPrice.Text;
            paramArray[14, 0] = "@TotalAmount";
            paramArray[14, 1] = txtTotal.Text;
            paramArray[15, 0] = "`";
            result = obj.setData("sp_Tbl_Contract_Insert", paramArray);
            mtvResult.ActiveViewIndex = 0;
            LoadData();
            EmptyFields();
        }
        catch
        {
        }
    }

我的DB_Columns: DatabaseTable Columns

1 个答案:

答案 0 :(得分:1)

也许有人在您的价格字段中输入了一个文本(应该是小数)或者可能是某人使用的,而不是。

replace paramArray[13, 1] = txtUnitPrice.Text;

with(并对所有数值做类似的事情)

paramArray = Convert.ToDecimal(txtUnitPrice.Text, new System.Globalization.CultureInfo("en-US"));

如果要使用格式“1.0”,请使用其他文化信息(如果您需要其他格式)。您还应捕获NumberFormatException并告诉用户使用特定格式。如果用户使用错误的格式或说“我的年薪!” :P