问题描述
protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse){SqlConnectionconn=newSqlConnection(ConfigurationManager.ConnectionStrings["KYGLConnectionString"].ConnectionString);conn.Open();stringxybh=GridView1.DataKeys[e.RowIndex].Value.ToString();/*获取主键,需要设置DataKeyNames,这里设为xybh*/Stringyxmc=(GridView1.Rows[e.RowIndex].Cells[2].Controls[0]asTextBox).Text.ToString();/*获取要更新的数据*/Stringyz=(GridView1.Rows[e.RowIndex].Cells[3].Controls[0]asTextBox).Text.ToString();Stringbgdd=(GridView1.Rows[e.RowIndex].Cells[4].Controls[0]asTextBox).Text.ToString();Stringxyjj=(GridView1.Rows[e.RowIndex].Cells[5].Controls[0]asTextBox).Text.ToString();Stringxydh=(GridView1.Rows[e.RowIndex].Cells[6].Controls[0]asTextBox).Text.ToString();Stringsql="UPDATEYXGLsetxybh='"+xybh+"',yxmc='"+yxmc+"',yz='"+yz+"',bgdd='"+bgdd+"',xyjj='"+xyjj+"',xydh='"+xydh+"'wherexybh='"+xybh+"'";SqlCommandcmd=newSqlCommand(sql,conn);cmd.ExecuteNonQuery();conn.Close();GridView1.EditIndex=-1;BindData();}
解决方案
解决方案二:
GridView1.Rows[e.RowIndex].Cells[6].Controls[0]中的某个属性为null。