2012-10-02 48 views
0

我正在努力解決一個錯誤。我檢查過我的存儲過程和代碼,但我不知道問題實際存在的地方。 The Error.錯誤將DataType nvarchar轉換爲十進制

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

+5

空捕獲不會幫助... –

+1

你有十進制參數,並傳遞字符串... –

+0

我也嘗試解析字符串,:/但沒有幫助 –

回答

1

也許有人進入了一個文本到你的價格字段(這應該是一個十進制)或也許有人用,而不是。

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

與(和做所有數值類似的東西)

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

如果你想使用的格式「1.0」,如果你想有一個不同的格式,使用其他的文化信息。你也應該捕獲NumberFormatException並告訴用戶使用特定的格式。如果用戶使用了錯誤的格式或者說「我的年薪」! :P