private void showinfo_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string idstring=e.Item.Cells[1].Text;
string nastring=(((TextBox)e.Item.Cells[2].Controls[0]).Text).ToString();
string tystring=(((TextBox)e.Item.Cells[3].Controls[0]).Text).ToString();
string phstring=(((TextBox)e.Item.Cells[4].Controls[0]).Text).ToString();
string strup="Update setdeptinfo set deptname='"+nastring+"',depttype='"+tystring+"',deptphone='"+phstring+"' where deptid='"+idstring+"'";
            OleDbCommand mycmd=new OleDbCommand(strup,showconn);
mycmd.ExecuteNonQuery();
showinfo.EditItemIndex=-1;
BindGrid(); }

解决方案 »

  1.   

    string idstring=e.Item.Cells[1].Text;你的ID再第一列吗??默认第一列的索引是0哦,看看有没搞错
      

  2.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.OleDb;
    namespace WebApplication2
    {
    /// <summary>
    /// showdeptinfo 的摘要说明。
    /// </summary>
    public class showdeptinfo : System.Web.UI.Page
    {
    protected System.Data.DataSet ds;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.HtmlControls.HtmlGenericControl FONT1;
    protected System.Web.UI.WebControls.Button gosetdetp;
    protected System.Web.UI.WebControls.Button gomain;
    protected System.Web.UI.WebControls.DataGrid showinfo;
        OleDbConnection showconn;
    public void Page_Load(object sender, System.EventArgs e)
    {
    string strConnection="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
    strConnection+=Server.MapPath("cnetdata\\cnet.mdb"); 
    //OleDbConnection showconn=new OleDbConnection(strConnection);
    showconn=new OleDbConnection(strConnection);
    showconn.Open(); // 在此处放置用户代码以初始化页面
        //string showstring="select * from setdeptinfo";
    //ds=new DataSet();
    //OleDbDataAdapter myadapter=new OleDbDataAdapter(showstring,showconn);
    //myadapter.Fill(ds,"setdeptinfo");
    //showinfo.DataSource=ds;
    //showinfo.DataMember="setdeptinfo";
    //showinfo.DataBind();
    if (!Page.IsPostBack)
    BindGrid();
    }
    ICollection createtable()
    {
    string showstring="select * from setdeptinfo";
    //ds=new DataSet();
    OleDbDataAdapter myadapter=new OleDbDataAdapter(showstring,showconn);
    myadapter.Fill(ds,"setdeptinfo");
    //showinfo.DataSource=ds;
    //showinfo.DataMember="setdeptinfo";
    //showinfo.DataBind();
    return ds.Tables["setdeptinfo"].DefaultView;
    }
    public void BindGrid()
    {
                  
    showinfo.DataSource=createtable();
    showinfo.DataBind();
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.ds = new System.Data.DataSet();
    ((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
    // 
    // ds
    // 
    this.ds.DataSetName = "NewDataSet";
    this.ds.Locale = new System.Globalization.CultureInfo("zh-CN");
    this.gosetdetp.Click += new System.EventHandler(this.gosetdetp_Click);
    this.gomain.Click += new System.EventHandler(this.gomain_Click);
    this.showinfo.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.showinfo_PageIndexChanged);
    this.showinfo.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.showinfo_CancelCommand);
    this.showinfo.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.showinfo_EditCommand);
    this.showinfo.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.showinfo_UpdateCommand);
    this.showinfo.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.showinfo_DeleteCommand);
    this.showinfo.SelectedIndexChanged += new System.EventHandler(this.showinfo_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load);
    ((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit(); }
    #endregion private void showinfo_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    } private void gosetdetp_Click(object sender, System.EventArgs e)
    {
    Response.Redirect("setdeptinfo.aspx");
    } private void gomain_Click(object sender, System.EventArgs e)
    {
    Response.Redirect("century.aspx");
    } private void showinfo_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    showinfo.CurrentPageIndex=e.NewPageIndex;
    BindGrid(); } private void showinfo_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    showinfo.EditItemIndex=(int)e.Item .ItemIndex;
    BindGrid();
    } private void showinfo_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    showinfo.EditItemIndex=-1;
    BindGrid(); } private void showinfo_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string idstring=e.Item.Cells[1].Text;
    string nastring=(((TextBox)e.Item.Cells[2].Controls[0]).Text).ToString();
    string tystring=(((TextBox)e.Item.Cells[3].Controls[0]).Text).ToString();
    string phstring=(((TextBox)e.Item.Cells[4].Controls[0]).Text).ToString();
    string strup="Update setdeptinfo set deptname='"+nastring+"',depttype='"+tystring+"',deptphone='"+phstring+"' where deptid='"+idstring+"'";
                OleDbCommand mycmd=new OleDbCommand(strup,showconn);
    mycmd.ExecuteNonQuery();
    showinfo.EditItemIndex=-1;
    BindGrid(); } private void showinfo_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {

    }
    }
    }
    这是所有的代码了,就是要对一个部门的信息的数据库进行更新等操作,这个表名叫,setdeptinfo,有四个字段,分别为deptid,deptname,depttype,deptphone,现在要更新,后面的三个字段操作,我最不明白的是string idstring=e.Item.Cells[1].Text;
    string nastring=(((TextBox)e.Item.Cells[2].Controls[0]).Text).ToString();这两句是什么意思,是在书上看的,然后改成我的。所以不对了,
      

  3.   

    “/WebApplication2”应用程序中的服务器错误。
    --------------------------------------------------------------------------------指定的转换无效。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误: 
    行 137: string nastring=(((TextBox)e.Item.Cells[2].Controls[0]).Text).ToString();
    行 138: string tystring=(((TextBox)e.Item.Cells[3].Controls[0]).Text).ToString();
    行 139: string phstring=(((TextBox)e.Item.Cells[4].Controls[0]).Text).ToString();
    行 140: string strup="Update setdeptinfo set deptname='"+nastring+"',depttype='"+tystring+"',deptphone='"+phstring+"' where deptid='"+idstring+"'";
    行 141:            OleDbCommand mycmd=new OleDbCommand(strup,showconn);
     源文件: d:\inetpub\wwwroot\webapplication2\showdeptinfo.aspx.cs    行: 139 堆栈跟踪: 
    [InvalidCastException: 指定的转换无效。]
       WebApplication2.showdeptinfo.showinfo_UpdateCommand(Object source, DataGridCommandEventArgs e) in d:\inetpub\wwwroot\webapplication2\showdeptinfo.aspx.cs:139
       System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e)
       System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       System.Web.UI.Page.ProcessRequestMain() 
    --------------------------------------------------------------------------------
    版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573 
    这是出错的页面
      

  4.   

    string idstring=e.Item.Cells[1].Text;你的ID再第一列吗??默认第一列的索引是0哦,看看有没搞错
    --------------我也有这样认为的倾向