0
votes

i have a method where it is possible to create a product. But when i convert the different inputs to the correct format i get an error.
The error is this

[FormatException: string was not in a correct format.] System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +12099621 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +120 System.Convert.ToInt32(String value) +44 EPWebApp.NewProduct.btnSaveNewItem_Click(Object sender, EventArgs e) in d:\Europoultry\Program\EPWCF\EPSystem\EPWebApp\NewProduct.aspx.cs:450 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9671538 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

Here is where i call the method on the server side.

protected void btnSaveNewItem_Click(object sender, EventArgs e)
    {


        svc.CreateNewProduct(
            txtNewGtin.Text,
            ddNewTargetMarket.DataValueField,
            Convert.ToString(datepicker1.Value),
            Convert.ToString(datepicker2.Value),
            Convert.ToString(txtNewLastChanged.Text),
            txtNewGLNNr.Text,
            txtNewGLNName.Text,
            hiddenRFIDValue.Value.ToString(),
            hiddenIsTradeItemACombinationItem.Value.ToString(),
            Convert.ToBoolean(hiddenHasBatchNumber.Value.ToLower()),
            ddNewTradeChannel.DataValueField,
            Convert.ToDecimal(txtNewNetContent.Text),
            ddnewNetContent.DataValueField,
            Convert.ToDecimal(txtNewGrossWeight.Text),
            ddNewGrossWeight.DataValueField,
            Convert.ToDecimal(txtNewNetWeight.Text),
            ddNewNetWeight.DataValueField,
            Convert.ToDecimal(txtNewHeight.Text),
            ddNewHeight.DataValueField,
            Convert.ToDecimal(txtNewWidth.Text),
            ddNewWidth.DataValueField,
            Convert.ToDecimal(txtNewDepth.Text),
            ddNewDepth.DataValueField,
            Convert.ToBoolean(hiddenIsBasePriceDeclarationRelevant.Value.ToLower()),
            Convert.ToBoolean(hiddenIsBarCodeOnPackageVariableMeasureBarCode.Value.ToLower()),
            Convert.ToBoolean(hiddenIsPackagingMarkedReturnable.Value.ToLower()),
            ddNewPackaginTypeCode.DataValueField,
            ddNewPACKMATERIALCODE.DataValueField,
            Convert.ToDecimal(txtNewPCKMATERIALCOMPOSITIONQTY.Text),
            ddPCKMATERIALCOMPOSITIONQTY.DataValueField,
            Convert.ToBoolean(hiddenIsTradeItemAConsumerUnit.Value.ToLower()),
            Convert.ToBoolean(hiddenIsTradeItemADespatchUnit.Value.ToLower()),
            Convert.ToBoolean(hiddenIsTradeItemAnOrdableUnit.Value.ToLower()),
            Convert.ToBoolean(hiddenIsTradeItemAnInvoiceUnit.Value.ToLower()),
            Convert.ToString(datepicker3.Value),
            txtNewCountryOfOrigin.Text,
            hiddenOrganicInformation.Value.ToString(),
            ddDescriptionOfSizeAndContent.DataValueField,
            txtNewDescriptionOfSizeAndContent.Text,
            hiddenRawMaterialIrradiatedCode.Value.ToString(),
            hiddenTradeIrradiatedCode.Value.ToString(),
            hiddenIngredientIrradiatedCode.Value.ToString(),
            ddNewPreservationTechniqueCodes.DataValueField,
            ddNewTradeItemUnitDesc.DataValueField,
            txtNewBrandName.Text,
            Convert.ToBoolean(hiddenIsTradeItemBaseUnit.Value.ToLower()),
            ddNewDescriptionLanguage.DataValueField,
            txtNewFunctionalName.Text,
            txtNewShortDescription.Text,
            Convert.ToInt32(txtNewMinimumStorageTemperature.Text),
            ddNewMinimumStorageTemperature.DataValueField,
            Convert.ToInt32(txtNewMaximumStorageTemperature.Text),
            ddNewMaximumStorageTemperature.DataValueField,
            txtNewGPCNr.Text,
            txtNewArticleNr.Text,
            ddImportClassificationType.DataValueField,
            Convert.ToInt32(txtNewImportClassificationType.Text),
            Convert.ToBoolean(hiddenIsPackMarkedWithIngredients.Value.ToLower()),
            Convert.ToBoolean(hiddenIsPriceOnPack.Value.ToLower()),
            DropDownList1.DataValueField,
            ddNewPackageMarksHigienic.DataValueField,
            ddNewExpirationDateTypeCode.DataValueField,
            ddPackageMarksDietOrAllergenCode.DataValueField,
            Convert.ToInt32(ddNewEUSubsidyCategoryAgencyName.DataValueField),
            ddNewTaxTypeCode.DataValueField,
            ddNewTaxAgencyCode.DataValueField,
            ddNewTaxApplicabilityCode.DataValueField,
            Convert.ToInt32(txtNewTaxRate.Text),
            ddNewMinimumTradeItemLifespanFromTimeOfProduction.DataValueField,
            Convert.ToInt32(txtNewMinimumTradeItemLifespanFromTimeOfProduction.Text),
            ddNewMinimumTradeItemLifespanFromTimeOfArrival.DataValueField,
            Convert.ToInt32(txtNewMinimumTradeItemLifespanFromTimeOfArrival.Text),
            hiddenIsDangerousSubstance.Value.ToString(),
            Convert.ToInt32(txtNewPreparedKCalQuantityContained.Text),
            Convert.ToInt32(txtNewPreparedKJoQuantityContained.Text),
            ddNewPreparedMeasurementPrecision.DataValueField,
            Convert.ToInt32(txtNewPreparedServingSize.Text),
            ddNewPreparedServingSize.DataValueField,
            Convert.ToInt32(txtNewUnPreparedKCalQuantityContained.Text),
            Convert.ToInt32(NewUnPreparedKJoQuantityContained.Text),
            ddNewUnPreparedMeasurementPrecision.DataValueField,
            Convert.ToInt32(txtNewUnPreparedServingSize.Text),
            ddNewUnPreparedServingSize.DataValueField);

        AddAllAllergenToItem();
        AddAllAdditiveAndEnumbers();
        AddAllNutrientValues();
        AddAllPreparationInformations();

    }

And here is the method that is called.

public void CreateNewProduct(string gtin, string targetMarket, string effectiveDate, string publicationDate, string lastChanged, string glnNr, string glnName, string rfid, string isTradeItemAnCombination, bool hasBatchNr, string tradeItemChannel, decimal netContentValue, string netContentMeasure, decimal grossWeightValue, string grossWeightMeasure, decimal netWeightValue, string netWeightMeasure, decimal heightValue, string heightMeasure, decimal widthValue, string widthMeasure, decimal depthValue, string depthMeasure, bool isBasePriceDeclarationRelevant, bool isTradeItemAVariableUnit, bool isPackagingMarkedReturnable, string packagingType, string packagingMaterial, decimal materialCompositionQtyValue, string materialCompositionMeasure, bool isTradeItemAConsumerUnit, bool isTradeItemADespatchUnit, bool isTradeItemAnOrderableUnit, bool isTradeItemAnInvoiceUnit, string startAvailabilityDateTime, string countryOfOrigin, string productOrigin, string descriptiveSizeLanguage, string descriptiveSizeLanguageValue, string rawMaterialIrradiateCode, string tradeItemIrradiatedCode, string ingredientIrradiatedCode, string preservationTechniqueCodes, string tradeItemUnitDescriptor, string brandName, bool isTradeItemAbaseUnit, string languageCode, string functionalName, string descriptionShort, int minStorageTemperatureValue, string minStorageTemperatureMeasure, int maxStorageTemperatureValue, string maxStorageTemperatureMeasure, string gPCBrickCode, string additionalTradeItemIdentificationValue, string classificationType, int classificationValue, bool isPackagingMarkedWithIngredients, bool isPriceOnPack, string barcodeType, string packageMarksHygienic, string expirationDateTypeCode, string packageMarksDietOrAllergenCode, int eUSubsidyAgencyCode, string applicabilityTaxTypeCode, string applicabilityTaxAgencyCode, string applicabilityCode, int taxRate, string minTradeItemLifeSpanFromTimeOfProduction, int minTradeItemLifeSpanFromTimeOfProductionvalue, string minTradeItemLifeSpanFromTimeOfArrival, int minTradeItemLifeSpanFromTimeOfArrivalvalue, string isDangerousSubstance, int preParedcalorificValueInKilocalories, int preParedcalorificValueInKilojoules, string preParedmeasurementPrecision, int preParednutritionalReferenceValue, string preParednutritionalReferenceMeasure, int unPreparedcalorificValueInKilocalories, int unPreparedcalorificValueInKilojoules, string unPreparedmeasurementPrecision, int unPreparednutritionalReferenceValue, string unPreparednutritionalReferenceMeasure)
    {
        CreateTradeItem(gtin, targetMarket, effectiveDate, publicationDate, lastChanged, glnNr, glnName, rfid, isTradeItemAnCombination, hasBatchNr, tradeItemChannel, 
            GetNewestDimensiomsId(), GetNewestPackageId(), GetNewestItemStatusId(), GetNewestOriginId(), GetNewestOrganicInformationId(), GetNewestSizeDescriptionId(), GetNewestItemProcessingTypeId(), GetNewestFoodDrinkAndTobacco(), GetNewestLifespanId(), GerNewestisDangerousSubstanceId(), GetNewestItemIdentitiesId(), GetNewestStorageAndHandelingId(), GetNewestProductClassificationId(), GetNewestMarkId(), GetNewestTaxAndSubsidiesId(), GetNewestPreparedNutrientInformationId(), GetNewestUnPreparedNutrientInformationId());

        CreateDimensions(netContentValue, netContentMeasure, grossWeightValue, grossWeightMeasure, netWeightValue, netWeightMeasure, heightValue, heightMeasure, widthValue, widthMeasure, depthValue, depthMeasure, isBasePriceDeclarationRelevant, isTradeItemAVariableUnit);

        CreatePackage(isPackagingMarkedReturnable, packagingType, packagingMaterial, materialCompositionQtyValue, materialCompositionMeasure);

        CreateTradeItemStatus(isTradeItemAConsumerUnit, isTradeItemADespatchUnit, isTradeItemAnOrderableUnit, isTradeItemAnInvoiceUnit, startAvailabilityDateTime);

        CreateOrigin(countryOfOrigin);

        CreateOrganicInformation(productOrigin);

        CreateSizeDescription(descriptiveSizeLanguage, descriptiveSizeLanguageValue);

        CreateItemProcessingType(rawMaterialIrradiateCode, tradeItemIrradiatedCode, ingredientIrradiatedCode);

        CreateFoodDrinkAndTobacco(preservationTechniqueCodes);

        CreateItemIdentities(tradeItemUnitDescriptor, brandName, isTradeItemAbaseUnit, languageCode, functionalName, descriptionShort);

        CreateStorageAndHandeling(minStorageTemperatureValue, minStorageTemperatureMeasure, maxStorageTemperatureValue, maxStorageTemperatureMeasure);

        CreateProductClassification(gPCBrickCode, additionalTradeItemIdentificationValue, classificationType, classificationValue);

        CreateMarks(isPackagingMarkedWithIngredients, isPriceOnPack, barcodeType, packageMarksHygienic, expirationDateTypeCode, packageMarksDietOrAllergenCode);

        CreateTaxAndSubsidies(eUSubsidyAgencyCode, applicabilityTaxTypeCode, applicabilityTaxAgencyCode, applicabilityCode, taxRate);

        CreateLifespan(minTradeItemLifeSpanFromTimeOfProduction, minTradeItemLifeSpanFromTimeOfProductionvalue, minTradeItemLifeSpanFromTimeOfArrival, minTradeItemLifeSpanFromTimeOfArrivalvalue);

        CreateDangerousSubstance(isDangerousSubstance);

        CreatePreparedNutrientInformation(preParedcalorificValueInKilocalories, preParedcalorificValueInKilojoules, preParedmeasurementPrecision, preParednutritionalReferenceValue, preParednutritionalReferenceMeasure);

        CreateUnPreparedNutrientInformation(unPreparedcalorificValueInKilocalories, unPreparedcalorificValueInKilojoules, unPreparedmeasurementPrecision, unPreparednutritionalReferenceValue, unPreparednutritionalReferenceMeasure);

        CreateItemsOverview(gtin, brandName, tradeItemChannel, functionalName, tradeItemUnitDescriptor, lastChanged, GetNewestTradeItemsId());
    }

I have check all of the parameters that the method gets, and all of them should be in the right format.

1
You should show only the relevant line that throws this exception. Show the string value that you try to parse and tell what culture you use.Soner Gönül
The problem is that it dosent give an relevant line where the problem is.. I just gives the line where the method is called.Code_Viking
Then you should set a break point and debug your method. Check your values and Text properties of your controls. ericlippert.com/2014/03/05/how-to-debug-small-programsSoner Gönül
That code looks horrific!404
Probably you are passing to one of the Convert.ToInt32 and empty string or a null value.... debug it...Sílvio N.

1 Answers

0
votes

The answer stays in the exception.

You call a Method of the Converter class, in which you try to parse a string which is not valid, because there are some non-numerical (special characters, alphabetical letters) characters in it.

You can try to convert the values in an try-catch block.

    try
    {
        //your method call
    }
    catch(FormatException ex)
    {
        //some handling
    }