asp.net--未将对象引用设置到对象实例。我给200分,说到做到
哪位能够帮我解决的话,,我给200分
<script runat="server" language="c#">
OleDbDataAdapter thisAdapter;
DataSet thisDS;
public void Page_Load(Object Sender,System.EventArgs e)
{
OleDbConnection thisConnection=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath("../sjku/mes.mdb")+"");
thisConnection.Open();
OleDbDataAdapter thisAdapter=new OleDbDataAdapter("SELECT id,nicheng,lytitle,content,time FROM liuyan",thisConnection);
DataSet thisDS=new DataSet();
thisAdapter.Fill(thisDS,"liuyan");
dg.DataSource=thisDS;
dg.DataBind();
thisConnection.Close();
}
public void dg_Delete(Object Sender,DataGridCommandEventArgs e)
{
DataRow dr;
dr=thisDS.Tables["liuyan"].Rows[(int)e.Item.ItemIndex];//编译时这一行红色显示。
dr.Delete();
thisAdapter.Update(thisDS,"liuyan");
dg.DataSource=thisDS;
dg.DataBind();
this.dg.EditItemIndex=-1;
}
</script>
参考答案:指定你DataGrid的DataKeyField
使用int id=Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex].ToString())作为Rows的index
其实删除行数据不应该这样写。通过SQL更新库才是正道。id作为参数传进。
另注意分页
if(e.Item.ItemIndex==0)
{
if(dg.CurrentPageIndex!=0)
{
dg.CurrentPageIndex--;
}
}