![](/img/trans.png)
[英]c#: Input string not in correct format error when converting list of type string to int
[英]input string was not in correct format converting string to long in c#?
大家好,我正在mvc4上工作,這里有些問題,一旦我將值存儲在像'String []'這樣的數組中,就從表單集合中獲取值,我將這些值移動到我的數據庫表中,但是我總是只有一個問題“輸入字符串的格式不正確”,任何機構都可以幫助我
這里我的代碼如下:
string[] BundleItemID = form.GetValues("txtbundleid");
for (int i = 0; i < skuid.Length; i++)
{
ProductBundleItem productbundleitem = new ProductBundleItem();
if (!string.IsNullOrEmpty(BundleItemID[i]))
{
productbundleitem.BundleItemId = Convert.ToInt64(BundleItemID[i]);
}
}
當我嘗試將值從“ Convert.ToInt64(BundleItemID [i])”移動到“ BundleItemId”時,我得到一個錯誤提示“輸入字符串格式不正確”
您應該使用long.TryParse來檢查是否可以進行轉換,如下所示:
long val;
if (long.TryParse(BundleItemID[i], out val)
productbundleitem.BundleItemId = val;
else
// handle situation when BundleItemID[i] is not a number somehow
添加了調試MessageBox,以便您可以找到錯誤。
string[] BundleItemID = form.GetValues("txtbundleid");
for (int i = 0; i < skuid.Length; i++)
{
ProductBundleItem productbundleitem = new ProductBundleItem();
if (!string.IsNullOrEmpty(BundleItemID[i]))
{
long val = 0;
if (!long.TryParse(BundleItemID[i], out val))
{
MessageBox.Show(string.Format("{0} is not a valid Int64 value", BundleItemID[i]));
break;
}
productbundleitem.BundleItemId = val;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.