输入字符串格式不正确,在c#中将字符串转换为长格式
本文关键字:格式 字符串 转换 不正确 输入 | 更新日期: 2023-09-27 17:49:39
嗨,我正在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 '我得到一个错误'输入字符串格式不正确'提前感谢
添加了一个调试消息框,以便您可以找到错误。
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;
}
}
应该使用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