我在更新GridView时发现错误 [英] I am finding en error while Updating a GridView

查看:83
本文介绍了我在更新GridView时发现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现此错误



指数超出范围。必须是非负数且小于集合的大小。

参数名称:index



我该怎么做才能消除此错误。

请给我解决方案



这就是我写的代码

I am finding this error

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

What should i do to remove this error.
Please give me solution

this is what i have write a code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Details : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        Bindgrid();
    }

    public void Bindgrid()
    {
        SqlConnection con = new SqlConnection("server=SACHIN-5DDBFA03\\SACHIN;uid=sa;password=india;database=master");
        con.Open();
        da = new SqlDataAdapter("select * from Details", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "Details");
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        


        string empid = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string empname = ((TextBox)GridView1.Rows[e.RowIndex].Controls[2].FindControl("txtempname")).Text;
        string deptno = ((TextBox)GridView1.Rows[e.RowIndex].Controls[3].FindControl("txtdeptno")).Text;

        SqlConnection con = new SqlConnection("server=SACHIN-5DDBFA03\\SACHIN;uid=sa;password=india;database=master");
        con.Open();
        cmd = new SqlCommand("update Details set empname=@empname,deptno=@deptno where empid=@empid", con);

        cmd.Parameters.Add("@empid", SqlDbType.Int);
        cmd.Parameters.Add("@empname", SqlDbType.VarChar);
        cmd.Parameters.Add("@deptno", SqlDbType.Int);


        cmd.Parameters[0].Value = int.Parse(empid);
        cmd.Parameters[1].Value = empname;
        cmd.Parameters[2].Value = int.Parse(deptno);


        cmd.ExecuteNonQuery();

        GridView1.EditIndex = -1;

        Bindgrid();
        con.Close();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bindgrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bindgrid();
    }
}

推荐答案

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx [ ^ ]


string empid = GridView1.DataKeys[e.RowIndex].Value.ToString();
string empname =((TextBox)GridView1.Rows[e.RowIndex].Controls[2].Controls[0]).Text;
string deptno = ((TextBox)GridView1.Rows[e.RowIndex].Controls[3].Controls[0]).Text;


<asp:gridview id="GridView1" runat="server" autogenerateeditbutton="True" xmlns:asp="#unknown">
            onrowcancelingedit="GridView1_RowCancelingEdit" DataKeyNames="Your_Primary_Key"
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
        </asp:gridview>


这篇关于我在更新GridView时发现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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