看看这是为啥报错

卡哇伊 2012-03-09 11:01:07

string Pn = Request.QueryString["pn"];
string[] arr_pn = Pn.Split(',');
for (int i = 0; i < arr_pn.Length; i++)
{
int int_pn = Convert.ToInt32(arr_pn[i]);

}

报错的地方是int 转换上int int_pn = Convert.ToInt32(arr_pn[i]);
...全文
135 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 xgl503305 的回复:]

引用 14 楼 chinajiyong 的回复:

引用 10 楼 xgl503305 的回复:

引用 7 楼 maomao525800 的回复:

你是不是没排除空串啊,你先看看arr_pn里存的是什么,字符串就没问题
引用 4 楼 xgl503305 的回复:

引用 1 楼 maomao525800 的回复:

int int_pn = Convert.ToInt……
[/Quote]
算了,之前问你是不是全是数字,你的回答是什么?自己的问题
hykhym 2012-03-09
  • 打赏
  • 举报
回复
什么啊,结贴了,晕,回复之前还没的呢,悲催
hykhym 2012-03-09
  • 打赏
  • 举报
回复
改成这样:
string[] arr_pn = Pn.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < arr_pn.Length-1; i++)
{
if(!string.IsNullOrEmpty(arr_pn[i]))
int int_pn = Convert.ToInt32(arr_pn[i]);
}

如果你字符串的最后一个字符是‘,’,最后一个转换的字符就是空的了
卡哇伊 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 chinajiyong 的回复:]

引用 10 楼 xgl503305 的回复:

引用 7 楼 maomao525800 的回复:

你是不是没排除空串啊,你先看看arr_pn里存的是什么,字符串就没问题
引用 4 楼 xgl503305 的回复:

引用 1 楼 maomao525800 的回复:

int int_pn = Convert.ToInt32(arr_pn[i]);
arr_pn 是stri……
[/Quote]
你也是事后诸葛亮
EnForGrass 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xgl503305 的回复:]

引用 7 楼 maomao525800 的回复:

你是不是没排除空串啊,你先看看arr_pn里存的是什么,字符串就没问题
引用 4 楼 xgl503305 的回复:

引用 1 楼 maomao525800 的回复:

int int_pn = Convert.ToInt32(arr_pn[i]);
arr_pn 是string类型的,应该改为: int int_pn = I……
[/Quote]
我就说嘛,split就出去空格啊
卡哇伊 2012-03-09
  • 打赏
  • 举报
回复
这个是协议类型,376.1协议,PN的值肯定是数字,要不然我也不会这么定义,但是源代码我不能贴出来
songxh20122 2012-03-09
  • 打赏
  • 举报
回复
你里面有一个元素是空字符串
那里最好try一下 或者你遍历数组的时候 少遍历一个元素
ziyouli 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 xgl503305 的回复:]
C# code


string Pn = Request.QueryString["pn"];
string[] arr_pn = Pn.Split(',');
for (int i = 0; i < arr_pn.Length; i++)
{
int int_pn……
[/Quote]
改为:
string[] arr_pn = Pn.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < arr_pn.Length; i++)
{
if(!string.IsNullOrEmpty(arr_pn[i]))
int int_pn = Convert.ToInt32(arr_pn[i]);
}

这样试试。
卡哇伊 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 maomao525800 的回复:]

你是不是没排除空串啊,你先看看arr_pn里存的是什么,字符串就没问题
引用 4 楼 xgl503305 的回复:

引用 1 楼 maomao525800 的回复:

int int_pn = Convert.ToInt32(arr_pn[i]);
arr_pn 是string类型的,应该改为: int int_pn = Int.Parse(arr_pn[i]);

输入字符……
[/Quote]还真是空的啊
Castiel丶Luo 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xgl503305 的回复:]
引用 2 楼 chinajiyong 的回复:

arr_pn[i]里面全是数字吗?还有你这样写
for (int i = 0; i < arr_pn.Length; i++)
{
int int_pn = Convert.ToInt32(arr_pn[i]);

……

肯定是数字了,我传的值我不知道
[/Quote]

把arr_pn的值贴出来看看
EnForGrass 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xgl503305 的回复:]

引用 2 楼 chinajiyong 的回复:

arr_pn[i]里面全是数字吗?还有你这样写
for (int i = 0; i < arr_pn.Length; i++)
{
int int_pn = Convert.ToInt32(arr_pn[i]);
……
[/Quote]
报错的地方是int 转换上int int_pn = Convert.ToInt32(arr_pn[i]);
总有个具体错误吧
黯然若水 2012-03-09
  • 打赏
  • 举报
回复
你是不是没排除空串啊,你先看看arr_pn里存的是什么,字符串就没问题
[Quote=引用 4 楼 xgl503305 的回复:]

引用 1 楼 maomao525800 的回复:

int int_pn = Convert.ToInt32(arr_pn[i]);
arr_pn 是string类型的,应该改为: int int_pn = Int.Parse(arr_pn[i]);

输入字符串的格式不正确。
[/Quote]
卡哇伊 2012-03-09
  • 打赏
  • 举报
回复
输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.FormatException: 输入字符串的格式不正确。

源错误:


行 324: for (int i = 0; i < arr_pn.Length; i++)
行 325: {
行 326: int int_pn =Int32.Parse(arr_pn[i]);
行 327: CommonVariable.terminalParam.dt0410.list0410.Add(dtList[int_pn - 1]);
行 328: }

卡哇伊 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chinajiyong 的回复:]

arr_pn[i]里面全是数字吗?还有你这样写
for (int i = 0; i < arr_pn.Length; i++)
{
int int_pn = Convert.ToInt32(arr_pn[i]);

……
[/Quote]
肯定是数字了,我传的值我不知道
卡哇伊 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 maomao525800 的回复:]

int int_pn = Convert.ToInt32(arr_pn[i]);
arr_pn 是string类型的,应该改为: int int_pn = Int.Parse(arr_pn[i]);
[/Quote]
输入字符串的格式不正确。
wangchangming 2012-03-09
  • 打赏
  • 举报
回复
arr_pn中包含有不能转换为Int32的元素
EnForGrass 2012-03-09
  • 打赏
  • 举报
回复
arr_pn[i]里面全是数字吗?还有你这样写
for (int i = 0; i < arr_pn.Length; i++)
{
int int_pn = Convert.ToInt32(arr_pn[i]);

}
只能得到最后的数字串
黯然若水 2012-03-09
  • 打赏
  • 举报
回复
int int_pn = Convert.ToInt32(arr_pn[i]);
arr_pn 是string类型的,应该改为: int int_pn = Int.Parse(arr_pn[i]);

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧