尝试创建新对象时,格式字符串的格式不正确
本文关键字:格式 字符串 不正确 对象 创建 新对象 | 更新日期: 2023-09-27 18:17:43
我有一个可以创建产品的方法。但是当我将不同的输入转换为正确的格式时,我得到一个错误。
错误如下
[FormatException: string is not in a correct format.]System.Number。stringtonnumber(字符串str, NumberStyles选项,NumberBuffer&number, NumberFormatInfo info, Boolean parseDecimal) +12099621System.Number。ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +120System.Convert。ToInt32(字符串值)+44EPWebApp.NewProduct。btnSaveNewItem_Click(对象发送者,EventArgs e) in d:'Europoultry'Program'EPWCF'EPSystem'EPWebApp'NewProduct.aspx.cs:450System.Web.UI.WebControls.Button。OnClick(EventArgs e) +9671538System.Web.UI.WebControls.Button。RaisePostBackEvent(String eventArgument) +108System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler。RaisePostBackEvent(String eventArgument) +12System.Web.UI.Page。RaisePostBackEvent(ipostbackeventandler sourceControl, String eventArgument) +15System.Web.UI.Page。RaisePostBackEvent(NameValueCollection postData) +35System.Web.UI.Page。ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
这里是我在服务器端调用方法的地方。
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();
}
这里是被调用的方法
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());
}
我检查了方法得到的所有参数,它们都应该是正确的格式
答案保留在异常中。
调用Converter类的方法,在该方法中尝试解析一个无效的字符串,因为其中有一些非数字(特殊字符,字母)字符。
可以尝试转换try-catch块中的值。
try
{
//your method call
}
catch(FormatException ex)
{
//some handling
}