如何解决索引超出范围的异常? [英] How to solve Index Out of range exception ?
问题描述
编译错误:
描述:编译为此请求提供服务所需的资源时发生错误。请查看以下具体错误详细信息并正确修改源代码。
Compilation Error:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0266: Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)
Source Error:
Line 270:
Line 271:
Line 272: n_sno = Convert.ToInt64(((TextBox)(row.Cells[1].Controls[0])).Text);
Line 273: n_date = ((TextBox)(row.Cells[2].Controls[1])).Text.Trim();
Line 274: n_c_ref = ((TextBox)(row.Cells[3].Controls[2])).Text.Trim();
Source File: c:\Users\bhushan.COMTEK\Desktop\Halo Convert in Publication\PublicationWeb\Indent.aspx.cs Line: 272
plz check这个代码.......
首先我取旧值然后新值然后新值插入gridvalue但是当我采取新的序列号时:显示异常 - 索引出范围。
错误行是这样的:n_sno = Convert.ToInt16(((TextBox)(row.Cells [1] .Controls [0]))。Text) ;
plz check this code.......
firstly i''m taking old values then new values then new value inserted in gridvalue but when i''m taking new serialnumber: show exception- Index out of range.
Error line is this : n_sno = Convert.ToInt16(((TextBox)(row.Cells[1].Controls[0])).Text);
protected void grid_indent_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
// retrieve the table from the session object...
// DataTable dt = (DataTable)Session["grid_value"];
// update the values of grid...
List<item_list> objList = (List<item_list>)Session["listdata"];
GridViewRow row = grid_indent.Rows[e.RowIndex];
o_sno = objList[row.DataItemIndex].serialnumber;
o_date = objList[row.DataItemIndex].date;
o_c_ref = objList[row.DataItemIndex].coderef;
o_i_name = objList[row.DataItemIndex].item_name;
o_inhand = objList[row.DataItemIndex].inhand;
o_request = objList[row.DataItemIndex].newrequest;
o_rate = objList[row.DataItemIndex].rate;
o_amt = objList[row.DataItemIndex].amount;
n_sno = Convert.ToInt16(((TextBox)(row.Cells[1].Controls[0])).Text);
n_date = ((TextBox)(row.Cells[2].Controls[1])).Text.Trim();
n_c_ref = ((TextBox)(row.Cells[3].Controls[2])).Text.Trim();
n_i_name = ((TextBox)(row.Cells[4].Controls[3])).Text.Trim();
n_inhand = Convert.ToInt32(((TextBox)(row.Cells[5].Controls[4])).Text.Trim());
n_request = Convert.ToInt32(((TextBox)(row.Cells[6].Controls[5])).Text.Trim());
n_rate = ((TextBox)(row.Cells[7].Controls[6])).Text.Trim();
n_amt = ((TextBox)(row.Cells[8].Controls[7])).Text.Trim();
objList[row.DataItemIndex].serialnumber =n_sno;
objList[row.DataItemIndex].date = n_date;
objList[row.DataItemIndex].coderef = n_c_ref;
objList[row.DataItemIndex].item_name = n_i_name;
objList[row.DataItemIndex].inhand = n_inhand;
objList[row.DataItemIndex].newrequest = n_request;
objList[row.DataItemIndex].rate = n_rate;
objList[row.DataItemIndex].amount = n_amt;
grid_indent.EditIndex = -1;
popUpData();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{ }
}
在此先感谢...
Bhushan
thanks in advance...
Bhushan
推荐答案
如果您想了解处理此类问题的方法,请查看本文问题
掌握视觉调试Studio 2010 - 初学者指南 [ ^ ]
实际上重读了你的问题 - 也许你需要的只是一双额外的眼睛: )
Have a look at this article if you want to know about ways to deal with this sort of problem
Mastering Debugging in Visual Studio 2010 - A Beginner''s Guide[^]
Having reread your question actually - maybe all you needed was a pair of extra eyes :)
看起来n_sno
被定义为int
,即32位整数变量。因此,你需要一个Convert.ToInt32()
。
就你的IndexOutOfRangeException而言,检查该行是否有一个索引为1的Cell ,并且该单元格具有索引为0的控件。
Looks liken_sno
was defined to be anint
, i.e. a 32 bit integer variable. Hence, you need aConvert.ToInt32()
.
As far as your IndexOutOfRangeException is concerned, check that the row has a Cell with index 1, and that that cell has a control with index 0.
这篇关于如何解决索引超出范围的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!