gpt4 book ai didi

c# - 将价格传递给 Paypal 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:41 25 4
gpt4 key购买 nike

每当我尝试将我的价格传递给 paypal 时,我总是收到以下错误。我应该怎么做才能解决这个问题?

An Exception of type System.FormatException occured in mscorlib.dll but was not handled in user code

Additional information: Input string was not in a correct format.

protected void MakePPLPayment(string itemInfo, string qty)
{
redirecturl += "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" + ConfigurationManager.AppSettings["paypalemail"].ToString();
redirecturl += "&first_name=" + Session["name"];
redirecturl += "&city=" + Session["city"];
redirecturl += "&state=" + Session["state"];
var itemInfos = itemInfo.Split('|');
var qtys = qty.Split('|');
var amounts = amount.Split('|');
for (int i = 0; i < itemInfos.Count(); i++)
{
if (itemInfos[i] != "" && qtys[i] != "" && qtys[i] != "0")
{
redirecturl += "&item_name_" + (i + 1) + "=" + itemInfos[i];
redirecturl += "&quantity_" + (i + 1) + "=" + qtys[i];
redirecturl += "&amount_" + (i + 1) + "=" + Math.Round(CalculateThis(amounts[i]), 2);
redirecturl += "&tax_" + (i + 1) + "=" + Math.Round(CalculateThis(amounts[i]) * Convert.ToDecimal(0.07), 2);
}
}
redirecturl += "&night_phone_a=" + Session["phone"];
redirecturl += "&address1=" + Session["email"];
redirecturl += "&business=er321224@hotmail.com";
redirecturl += "&shipping=0";
redirecturl += "&handling=0";
redirecturl += "&tax=" + tax;
redirecturl += "&currency=" + Session["currency"];;
redirecturl += "&return=" +
ConfigurationManager.AppSettings["SuccessURL"].ToString();
redirecturl += "&cancel_return=" +
ConfigurationManager.AppSettings["FailedURL"].ToString();
Response.Redirect(redirecturl);
}
private decimal CalculateThis(object p)
{
if (p == null)
return 0;
else if (p == "")
return 0;
return Convert.ToInt16(p);
}

错误是在return Convert.ToInt16(p);这行抛出的,当错误是p的值为9.7000抛出。

编辑

这是显示如何实现字符串值的语法,也许这有助于解决我的问题:

protected void PPLSelected_Click(object sender, EventArgs e)
{
var qtys = CalculateThis(dropdowncount.SelectedItem.Text) + "|" + CalculateThis(dropdowncount1.SelectedItem.Text) + "|" + CalculateThis(dropdowncount2.SelectedItem.Text) + "|" + CalculateThis(dropdowncount3.SelectedItem.Text) + "|" + CalculateThis(dropdowncount4.SelectedItem.Text) + "|" + CalculateThis(dropdowncount5.SelectedItem.Text) + "|" + CalculateThis(dropdowncount6.SelectedItem.Text);
var items = dropdownitem.SelectedItem.Text + "|" + dropdownitem1.SelectedItem.Text + "|" + dropdownitem2.SelectedItem.Text + "|" + dropdownitem3.SelectedItem.Text + "|" + dropdownitem4.SelectedItem.Text + "|" + dropdownitem5.SelectedItem.Text + "|" + dropdownitem6.SelectedItem.Text;
var amounts = dropdownamt.SelectedItem.CalculateThisue + "|" + dropdownamt1.SelectedItem.CalculateThisue + "|" + dropdownamt2.SelectedItem.CalculateThisue + "|" + dropdownamt3.SelectedItem.CalculateThisue + "|" + dropdownamt4.SelectedItem.CalculateThisue + "|" + dropdownamt5.SelectedItem.CalculateThisue + "|" + dropdownamt6.SelectedItem.CalculateThisue;
MakePPLPayment(items, qtys);
}

最佳答案

Convert.ToInt16(object) 方法使用 CurrentCulture 默认

我认为您的 CurrentCultureNumberDecimalSeparator不是 . 而是 NumberGroupSeparator.,这就是为什么此方法认为这是 97000,而不是 9.7

您可以提供具有 . 的区域性作为 NumberDecimalSeparator,例如 InvariantCulture .

return Convert.ToInt16(p, CultureInfo.InvariantCulture);

完整代码;

public static void Main(string[] args)
{
object o = 9.7000;
CalculateThis(o);
}

public static decimal CalculateThis(object p)
{
if (p == null)
return 0;
else if (p == "")
return 0;
return Convert.ToInt16(p, CultureInfo.InvariantCulture);
}

这里是一个 demonstration .

关于c# - 将价格传递给 Paypal 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33545809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com