Abid Ali
Abid Ali

Reputation: 1747

Error Converting DataType nvarchar to decimal

I am struggling with an error. I`ve checked my stored procedure and code, but i don't know what and where the problem is actually existing. 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
        {
        }
    }

My DB_Columns: DatabaseTable Columns

Upvotes: 0

Views: 10053

Answers (1)

MrFox
MrFox

Reputation: 5126

Maybe someone entered a text into your price field (which should be a decimal) or maybe someone used , in stead of .

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

with (and do something similar for all numerical values)

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

If you want to use the format "1.0", use other culture information if you want a different format. Also you should catch NumberFormatException and tell the user to use a specific format. In case the user uses the wrong format or says something like "my year salary!" :P

Upvotes: 1

Related Questions