网格视图超出范围异常,值在范围内
本文关键字:范围内 异常 范围 视图 网格 | 更新日期: 2023-09-27 18:10:58
我有一个GridView:
<asp:GridView runat="server" ID="_gv_AllResp" Width="100%" style="color: White;" DataKeyNames="recordid"
Font-Size="10pt" OnDataBound="_gvDataBind_Eve" AutoGenerateSelectButton="true" OnSelectedIndexChanged="_gv_AllResp_SelectedIndexChanged">
<RowStyle HorizontalAlign="Center" />
<HeaderStyle BackColor="#57768f"/>
<RowStyle BackColor="#dae2e8" ForeColor="Black" />
<AlternatingRowStyle BackColor="#ffffff" ForeColor="Black" />
</asp:GridView>
我从ADO后面的代码绑定:
SELECT a.[recordid], [lname] + ', ' + [fname] as [name], Convert(char, [datebirth], 101) as 'DOB', phone1, phone2, phone3, ext FROM [dbo].[Respondent] a
然后,我有一个过程从网格中选择随机数量的记录,并将它们插入到表中:
var rnd = new Random();
var _rand = Enumerable.Range(0, _RowCount).Select(x => new { val = x, order = rnd.Next() }).OrderBy(i => i.order).Select(x => x.val).Take(_LockCount).ToArray();
string _jobnum = _dd_ActJobs.SelectedValue.ToString();
foreach (var a in _rand)
{
int b = Convert.ToInt32(a);
using (var con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["RM_V1.0CS"].ConnectionString;
try
{
con.Open();
var cmd = new SqlCommand("INS_DemoLockQuery", con) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.Add("@jobnum", SqlDbType.NVarChar, 100, "jobnum");
cmd.Parameters.Add("@respnum", SqlDbType.NVarChar, 100, "respnum");
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 100, "name");
cmd.Parameters.Add("@phone", SqlDbType.NVarChar, 100, "phone");
cmd.Parameters.Add("@wphone", SqlDbType.NVarChar, 100, "wphone");
cmd.Parameters.Add("@wphone_ext", SqlDbType.NVarChar, 100, "wphone_ext");
cmd.Parameters.Add("@cellphone", SqlDbType.NVarChar, 100, "cellphone");
cmd.Parameters.Add("@quota", SqlDbType.NVarChar, 100, "quota");
cmd.Parameters["@jobnum"].Value = _jobnum;
//GETTING ERROR HERE
cmd.Parameters["@respnum"].Value = _gv_AllResp.Rows[b].Cells[1].Text.ToString();
cmd.Parameters["@name"].Value = _gv_AllResp.Rows[b].Cells[2].Text.ToString();
cmd.Parameters["@phone"].Value = _gv_AllResp.Rows[b].Cells[4].Text.ToString().Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "").Trim();
cmd.Parameters["@wphone"].Value = _gv_AllResp.Rows[b].Cells[5].Text.ToString().Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "").Trim();
cmd.Parameters["@wphone_ext"].Value = _gv_AllResp.Rows[b].Cells[6].Text.ToString().Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "").Trim();
cmd.Parameters["@cellphone"].Value = _gv_AllResp.Rows[b].Cells[7].Text.ToString().Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "").Trim();
cmd.Parameters["@quota"].Value = _ddActQuota.SelectedValue.ToString();
cmd.ExecuteNonQuery();
ViewAvailable();
}
//catch (Exception ex) { ErrHandler.WriteError(ex.Message); }
finally { con.Close(); }
}
}
我得到下面的"索引超出范围。"必须非负且小于集合的大小。例如,对于这个特定的测试,网格中有145行,b的值(行号)是120。这在允许的行范围内。我不明白为什么这超出了索引范围。
堆栈跟踪
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Web.UI.WebControls.GridViewRowCollection.get_Item(Int32 index)
at Default3._b_selectRepond(Object sender, EventArgs e) in c:'Afocus'Jobs'Query.aspx.cs:line 668
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(Str ing eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
有什么建议吗?提前感谢男生/女生
EDIT @ James
我只是尝试使用您建议的datakeynames,并将插入块替换为
cmd.Parameters["@jobnum"].Value = _jobnum;
cmd.Parameters["@respnum"].Value = _gv_AllResp.DataKeys[b]["recordid"].ToString();
cmd.Parameters["@name"].Value = _gv_AllResp.DataKeys[b]["Name"].ToString().Replace(" ", "");
cmd.Parameters["@phone"].Value = _gv_AllResp.DataKeys[b]["Main"].ToString().Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "").Trim();
cmd.Parameters["@wphone"].Value = _gv_AllResp.DataKeys[b]["Work Phone"].ToString().Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "").Trim();
cmd.Parameters["@wphone_ext"].Value = _gv_AllResp.DataKeys[b]["ext"].ToString().Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "").Trim();
cmd.Parameters["@cellphone"].Value = _gv_AllResp.DataKeys[b]["Cell Phone"].ToString().Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "").Trim();
cmd.Parameters["@quota"].Value = _ddActQuota.SelectedValue;
然而,我仍然得到同样的错误,我试图"锁定"50条记录,在网格中有145行,它循环了23次,然后在135行(b=135)抛出了一个错误。必须非负且小于集合的大小。参数名称:index"
指定从位置1
而不是0
开始的列索引。你有7列在你的选择,所以你的列索引应该范围0-6
。
我相信这是错误的行:
_gv_AllResp.Rows[b].Cells[7].Text // = bang!
编辑
要完全消除这个问题,可以使用数据键访问所需的列:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Name, DOB, phone1, phone2, ..." >
在代码后面:
string name = GridView1.DataKeys[0]["Name"].ToString();