用c#.Am运行asp.net应用程序使用gridview来更新和删除columns.deleting工作正常。如果点击我得到的更新按钮
System.NullReferenceException: Object reference not set to an instance of an object.
我的代码是;
protected bool IsRowModified(GridViewRow row)
{
int currentID;
string currentName;
string currentLocation;
currentID = Convert.ToInt32(GridView1.DataKeys
[row.RowIndex].Value);
currentName = ((TextBox)row.FindControl
("txtName")).Text;
currentLocation = ((TextBox)row.FindControl
("txtLocation")).Text;
**System.Data.DataRow newRow = originalTable.Select
(String.Format("ID = {0}", currentID))[0];** //got error in this line
if (!currentName.Equals(newRow["Name"].ToString()))
{ return true; }
if (!currentLocation.Equals(newRow["Location"].ToString()))
{ return true; }
return false;
}
答案 0 :(得分:1)
originalTable为null,或者originalTable.Select(...)返回null。
您是否已从originalTable
删除基础数据而未更新用户界面?
另一种方法可能是使用GridViewRow参数的DataItem属性:
protected bool IsRowModified(GridViewRow row)
{
string currentName = ((TextBox)row.FindControl("txtName")).Text;
string currentLocation = ((TextBox)row.FindControl("txtLocation")).Text;
DataRow newRow = (DataRow)row.DataItem;
if (!string.Equals(currentName, newRow["Name"].ToString()))
{
return true;
}
if (!string.Equals(currentLocation, newRow["Location"].ToString()))
{
return true;
}
return false;
}
答案 1 :(得分:-2)
我的猜测是你的currentID变量为null或为空。在尝试使用它们之前,应检查null或empty的值。
if(!String.IsNullOrEmpty(currentID))
{
// Continue processing
}