我有一个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(); }
}
}
我得到以下内容“索引超出范围。必须是非负数且小于集合的大小。参数名称:index”网格中有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)
有什么建议吗?在此先感谢男孩/女孩
我刚刚尝试使用你建议的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;
但是我仍然得到相同的错误,我试图“锁定”网格中有145行的50条记录并循环23次然后在135行(b = 135)中抛出错误“相同错误”索引是超出范围。必须是非负数且小于集合的大小。参数名称:index“
答案 0 :(得分:1)
您要指定从位置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();