问题描述
我有一个GridView:
I have a 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绑定后面的代码:
That I bind from the code behindwith ADO:
SELECT a.[recordid], [lname] + ', ' + [fname] as [name], Convert(char, [datebirth], 101) as 'DOB', phone1, phone2, phone3, ext FROM [dbo].[Respondent] a
然后我有一个过程可以从网格中选择随机数的记录,然后将它们插入表中:
I then have a procedure to select a random number of records from the grid and insertthem into a table:
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.这在允许的行范围内.我不明白这是如何超出索引范围的.
I'm getting the following "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" There are 145 rows in the grid and the value of b (row number) is 120 for instance for this particular test. This is in the range of allowable rows. I'm don't understand how this is outside the index range.
堆栈跟踪
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)
有什么建议吗?预先感谢男孩/女孩
Any Suggestions ? Thanks in advance Guys / Girls
我只是按照您的建议尝试使用datakeynames,并将插入块替换为
I just tried using the datakeynames as you suggested and replaced the insert block with
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)Same Error索引为超出范围.必须为非负数并且小于集合的大小.参数名称:index"
However I still get the same error, I tried to "lock" 50 records with 145 rows in the grid and it looped through 23 times and then threw an error n the 135 row (b=135) Same Error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
推荐答案
您要指定从位置 1
而不是 0
开始的列索引.您的选择中有7列,因此列索引的范围应为 0-6
.
You're specifying the column indexes starting at position 1
instead of 0
. You have 7 columns in your select, so your column indexes should range 0-6
.
我相信这是违规行:
_gv_AllResp.Rows[b].Cells[7].Text // = bang!
编辑
要完全消除该问题,请使用数据键访问所需的列:
To eliminate the problem altogether, use data keys to access the columns you need:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Name, DOB, phone1, phone2, ..." >
在后面的代码中:
string name = GridView1.DataKeys[0]["Name"].ToString();
这篇关于网格视图超出范围异常,值在范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!