如何消除错误:索引超出范围。必须是非负数且小于集合的大小。参数名称:index [英] How do I Errase Error:Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

查看:134
本文介绍了如何消除错误:索引超出范围。必须是非负数且小于集合的大小。参数名称:index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  DataList1_UpdateCommand( object  source,DataListCommandEventArgs e)
{
int Id;
string name = string .Empty;
string details = string .Empty;
string website = string .Empty;
string address = string .Empty;
string TextImg = string .Empty;
// FileUpload imagepath = FileUpload.ReferenceEquals;
SqlCommand cmd = new SqlCommand();


尝试
{
name =((TextBox)(e.Item.FindControl(< span class =code-string> txtEditName)))。文本;
details =((TextBox)(e.Item.FindControl( txtEditDetails)) )。文本;
address =((TextBox)(e.Item.FindControl( txtEditaddress)) )。文本;
website =((TextBox)(e.Item.FindControl( txtEditWebsite)) )。文本;
TextImg =((TextBox)(e.Item.FindControl( txtEditImgpath)) )。文本;

// areaid =((TextBox)(e.Item.FindControl(txtEditareaid) )))。文本;
FileUpload imagepath =((FileUpload)(e.Item.FindControl( FileUploadImg)));


Id = Convert.ToInt32(DataList1.DataKeys [e.Item.ItemIndex] .ToString());
con.Open();
cmd = new SqlCommand( Team_SocialActivities_Edit ,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ Id,Id);
cmd.Parameters.AddWithValue( @ Name,name);
cmd.Parameters.AddWithValue( @ Details,详情);
cmd.Parameters.AddWithValue( @ Address,address);
cmd.Parameters.AddWithValue( @ Website,网站);

// cmd.Parameters.AddWithValue(@ Area_ID,areaid);
Guid FileName = Guid.NewGuid();
if (imagepath.FileName!=
{
imagepath.SaveAs(Server.MapPath( 〜/ Images / + FileName + 。png));
cmd.Parameters.AddWithValue( @ ImagePath 〜/ Images / + FileName + PNG);
}
else
{
cmd.Parameters.AddWithValue( @ ImagePath,TextImg);
}
cmd.ExecuteNonQuery();
DataList1.EditItemIndex = -1;
BindData();
ScriptManager.RegisterStartupScript( this this .GetType(), 消息 alert('书籍记录已成功更新'); true );
}
catch (例外情况)
{
ScriptManager.RegisterStartupScript( this this .GetType(), 消息 alert('哎呀!!错误发生: + ex .Message.ToString()+ '););
}

解决方案

很可能,你还没有声明 DataKey in DataList 标记。



这就是为什么它无法在该索引中找到Key。

protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
    int Id;
    string name = string.Empty;
    string details = string.Empty;
    string website = string.Empty;            
    string address = string.Empty;
    string TextImg = string.Empty;
    //FileUpload imagepath=FileUpload.ReferenceEquals;
    SqlCommand cmd = new SqlCommand();


    try
    {
        name = ((TextBox)(e.Item.FindControl("txtEditName"))).Text;
        details = ((TextBox)(e.Item.FindControl("txtEditDetails"))).Text;
        address = ((TextBox)(e.Item.FindControl("txtEditaddress"))).Text;
        website = ((TextBox)(e.Item.FindControl("txtEditWebsite"))).Text;
        TextImg = ((TextBox)(e.Item.FindControl("txtEditImgpath"))).Text;
        
        //areaid = ((TextBox)(e.Item.FindControl("txtEditareaid"))).Text;
        FileUpload imagepath = ((FileUpload)(e.Item.FindControl("FileUploadImg")));

        
        Id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
        con.Open();
        cmd = new SqlCommand("Team_SocialActivities_Edit", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Id", Id);
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Parameters.AddWithValue("@Details", details);
        cmd.Parameters.AddWithValue("@Address", address);
        cmd.Parameters.AddWithValue("@Website", website);
        
        //cmd.Parameters.AddWithValue("@Area_ID", areaid);
        Guid FileName = Guid.NewGuid();
        if (imagepath.FileName != "")
        {
            imagepath.SaveAs(Server.MapPath("~/Images/" + FileName + ".png"));
            cmd.Parameters.AddWithValue("@ImagePath", "~/Images/" + FileName + ".png");
        }
        else
        {
            cmd.Parameters.AddWithValue("@ImagePath", TextImg);
        }
        cmd.ExecuteNonQuery();
        DataList1.EditItemIndex = -1;
        BindData();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Book record has been updated successfully');", true);
    }
    catch (Exception ex)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Oops!! Error occured : " + ex.Message.ToString() + "');", true);
    }

解决方案

Most likely, you have not declared the DataKey in DataList Markup.

That's why it could not able to find the Key in that index.


这篇关于如何消除错误:索引超出范围。必须是非负数且小于集合的大小。参数名称:index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆