2014年7月13日 星期日

[RESOLVED] GridView error: index out of range on updating


Hi,


i have total 7 fields in my grid:


Edit,Delete,Id,Name,SurName,PhoneNo and EmailId


for updating i have written code:


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

con.Open();

TextBox name, phone, srname, email;

name = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].FindControl("TextBox1");

phone = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0];

srname = (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];

email = (TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0];


int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);


SqlCommand cmd = new SqlCommand("update Register set Name='"+name+"',PhoneNo='"+phone+"',SurName='"+srname+"',EmailId='"+email+"' where Id='"+ID+"'",con);

cmd.ExecuteNonQuery();

con.Close();

loaduser();

}


Inline code:


I have bind tabe data directly to gridview so only delete and edit field.


















Problem is that it use to show me error :


Specified argument was out of the range of valid values.

Parameter name: index



Your indexes are off by one, I think. Cells is zero-based; so control number seven will be Cells[6].


沒有留言:

張貼留言