下面是ASPX文件代码
<form id="Form1" method="post" runat="server">
<asp:datagrid id="dgly" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 88px" runat="server"
AutoGenerateColumns="False" Width="241px" PageSize="3" AllowPaging="True">
<Columns>
<asp:BoundColumn DataField="enName" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="chName"></asp:BoundColumn>
<asp:BoundColumn DataField="oicq"></asp:BoundColumn>
<asp:BoundColumn DataField="phone"></asp:BoundColumn>
<asp:BoundColumn DataField="ly"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Save" CancelText="Cannel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:datagrid></form>

解决方案 »

  1.   

    下面是CS文件
    public class WebForm2 : System.Web.UI.Page
    {
    public System.Web.UI.WebControls.DataGrid dgly;
    public String strConnection = "server=(local);user id=sa;pwd=sa;database=lybbook;";
    public String strSQL = "select * from liuyan";
    public String lyTableName = "lyTable";
    public SqlConnection objConnection;

    public void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!IsPostBack)
    LoadGrid();
    } public void LoadGrid()
    {
    Connect();
    SqlDataAdapter adapter = new SqlDataAdapter(strSQL,objConnection);
    DataSet ds = new DataSet();
    adapter.Fill(ds,lyTableName);
    Disconnect(); dgly.DataSource = ds.Tables[lyTableName];
    dgly.DataBind();
    } public void Connect()
    {
    if(objConnection == null)
    objConnection = new SqlConnection(strConnection); if(objConnection.State == ConnectionState.Closed)
    objConnection.Open();
    } public void Disconnect()
    {
    objConnection.Close();
    } public void EditRecord(object sender, DataGridCommandEventArgs e)
    {
    dgly.EditItemIndex = e.Item.ItemIndex;
    LoadGrid();
    } public void CannelEdit(object sender, DataGridCommandEventArgs e)
    {
    dgly.EditItemIndex=-1;
    LoadGrid();
    } public void UpdateRecord(object sender, DataGridCommandEventArgs e)
    {
    String strConnection = "server=(local);user id=sa;pwd=sa;database=lybbook;";
    SqlConnection objConnection = new SqlConnection(strConnection);
    String strSQL1 = "UPDATE liuyan SET chName=@chName, oicq=@oicq, phone=@phone, ly=@ly";
    SqlDataAdapter adapter1 = new SqlDataAdapter(strSQL1, objConnection);
    adapter1.UpdateCommand.Parameters["chName"].Value = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
    adapter1.UpdateCommand.Parameters["oicq"].Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
    adapter1.UpdateCommand.Parameters["phonee"].Value = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
    adapter1.UpdateCommand.Parameters["ly"].Value = ((TextBox)e.Item.Cells[4].Controls[0]).Text; adapter1.UpdateCommand.Connection.Open();
    adapter1.UpdateCommand.ExecuteNonQuery();
    adapter1.UpdateCommand.Connection.Close() ;
    dgly.EditItemIndex = -1; 
    LoadGrid();
    }
      

  2.   

    当想修改完行中的一个  项(文本框内容) 的时候,再点  SAVE(保存)的时候,就出错了未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 79:  String strSQL1 = "UPDATE liuyan SET chName=@chName, oicq=@oicq, phone=@phone, ly=@ly";
    行 80:  SqlDataAdapter adapter1 = new SqlDataAdapter(strSQL1, objConnection);
    行 81:  adapter1.UpdateCommand.Parameters["chName"].Value = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
    行 82:  adapter1.UpdateCommand.Parameters["oicq"].Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
    行 83:  adapter1.UpdateCommand.Parameters["phonee"].Value = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
     
      

  3.   

    创建UpdateCommand了吗?adapter1.UpdateCommand = new SqlCommand(strSQL1,objConnection);
      

  4.   

    未将对象引用设置到对象的实例。 使用 adapter使用有四大对象:SELECT UPDATE DELETE INSERT,你用update的时候必须提供其他三个对象,如果你不想执行更改的话,只提供SELECTCOMMAND对象就可以了。如果你使用的是控件adapter,那不会出现你的问题,但是估计搂主是自己敲的代码,你必须手动加上其他三个command对象。SqlCommandBuilder自动生成其他的三个对象,查阅SqlCommandBuilder看一下,希望楼主的问题已经解决了。
      

  5.   

    当想修改完行中的一个  项(文本框内容) 的时候,再点  SAVE(保存)的时候,就出错了当然是这样了,你这个时候要执行update 操作,要数据服务器连接进行更新操作的,重新请求与新页。
      

  6.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; 
    namespace student
    {
    /// <summary>
    /// WebForm28 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Data.SqlClient.SqlConnection sqlConnection1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
     
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!IsPostBack)
    {
    SetBind();
    SetBind2();
    }
    } protected void SetBind()
    { SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
    SqlDataAdapter da=new SqlDataAdapter("select * from stu,dep where stu.studepid=dep.depid",conn);
    DataSet ds=new DataSet();
    da.Fill(ds,"table1");
    this.DataGrid1.DataSource=ds.Tables["table1"];
    this.DataGrid1.DataBind();
       
    } protected void SetBind2()
    { SqlConnection conn2=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
    SqlDataAdapter da2=new SqlDataAdapter("select * from dep",conn2);
    DataSet ds2=new DataSet();
    da2.Fill(ds2,"table1");
    this.DropDownList1.DataSource=ds2.Tables["table1"];
    this.DropDownList1.DataTextField="depname";
    this.DropDownList1.DataValueField="depid";
    this.DropDownList1.DataBind();
    this.DropDownList1.Items.Insert(0,new ListItem("请选择",""));
       
    } protected void SetBind3()
    {
    string s=this.DropDownList1.SelectedValue;
    SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
    SqlCommand comm=new SqlCommand();
    comm.Connection=conn;
    if(s!="")
    {
    comm.CommandText="select * from stu,dep where stu.studepid=dep.depid and depid=@depid";
    SqlParameter parm1=new SqlParameter("@depid",SqlDbType.Int);
    parm1.Value=s;
    comm.Parameters.Add(parm1);
    }
    else
    comm.CommandText="select * from stu,dep where stu.studepid=dep.depid";
    SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand=comm;
    DataSet ds=new DataSet();
    da.Fill(ds,"table1");
    this.DataGrid1.DataSource=ds.Tables["table1"];
    this.DataGrid1.DataBind();
       
    }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }
      
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
    this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
    // 
    // sqlConnection1
    // 
    this.sqlConnection1.ConnectionString = "workstation id=JINBING;packet size=4096;user id=sa;data source=JINBING;persist se" +
    "curity info=False;initial catalog=isswhu";
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
    SqlDataAdapter da=new SqlDataAdapter("select * from dep",conn);
    DataSet ds=new DataSet();
    da.Fill(ds,"table1");
    if(e.Item.ItemType==ListItemType.EditItem)
    {
    DropDownList ddl=(DropDownList)e.Item.FindControl("dep");
    ddl.DataSource=ds.Tables["table1"];
    ddl.DataTextField="depname";
    ddl.DataValueField="depid";
    ddl.DataBind();
    ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"depid"))).Selected=true;//选择数据库内的作为默认
    }
    } protected void edit(object sender,DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
    if(this.DropDownList1.SelectedValue=="")
    SetBind();
    else
    SetBind3();
    } protected void cancel(object sender,DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=-1;
    if(this.DropDownList1.SelectedValue=="")
    SetBind();
    else
    SetBind3();
    } protected void update(object sender,DataGridCommandEventArgs e)
    {
    if(e.Item.ItemType==ListItemType.EditItem)//只有在编辑按下以后才能提交
    {
    SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
    SqlCommand comm=new SqlCommand("update stu set stuname=@name,studepid=@depid where stuid=@id",conn);
    SqlParameter parm1=new SqlParameter("@name",SqlDbType.NVarChar,50);
    parm1.Value=((TextBox)e.Item.FindControl("name")).Text;
    SqlParameter parm2=new SqlParameter("@depid",SqlDbType.Int);
    parm2.Value=((DropDownList)e.Item.FindControl("dep")).SelectedValue;
    SqlParameter parm3=new SqlParameter("@id",SqlDbType.Int);
    parm3.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
    comm.Parameters.Add(parm1);
    comm.Parameters.Add(parm2);
    comm.Parameters.Add(parm3);
    conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
    this.DataGrid1.EditItemIndex=-1;
    if(this.DropDownList1.SelectedValue=="")
    SetBind();
    else
    SetBind3();//如果选择过滤则使用SetBind3()
    }
    } private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    SetBind3();
    }
    }
    }
      

  7.   

    adapter1.UpdateCommand.Parameters["chName"]......
    应该加上@,即
     adapter1.UpdateCommand.Parameters["@chName"]......
      

  8.   

    jinbingg(bing)   我按你提供的方法写的,可是提示还是一样的错误,好郁闷啊!!!
      

  9.   

    new SqlDataAdapter(strSQL1, objConnection);
    构造参数应该是select command,最好不要通过这种方法来更新数据
      

  10.   

    fancyf(Fancyray)   能不能说详细些