private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string sql="delete from 表1 where 学号="+e.Item .Cells [0].Text;
this.myConnection .Open ();
cmd=new OleDbCommand (sql,this.myConnection );
cmd.ExecuteNonQuery ();
this.myConnection .Close  ();
this.DataGrid1 .EditItemIndex =-1;
this.DataGrid1_DataBinding ();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string sql="update 表1 set"+" 序号="+e.Item .Cells [0].Text+
",姓氏="+e.Item .Cells [1].Text+
",名字="+e.Item .Cells [2].Text+
",职务="+e.Item .Cells [3].Text+
",住址="+e.Item .Cells [4].Text+
",年龄="+e.Item .Cells [5].Text+
",性别="+e.Item .Cells [6].Text+
"where 学号="+e.Item .Cells [0].Text;
this.myConnection .Open ();
cmd=new OleDbCommand (sql,this.myConnection );
cmd.ExecuteNonQuery ();
this.myConnection .Close  ();
this.DataGrid1 .EditItemIndex =-1;
this.DataGrid1_DataBinding ();
}
这是我写的添加删除方法,但是运行出了错误:
UPDATE 语句的语法错误。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 
异常详细信息: System.Data.OleDb.OleDbException: UPDATE 语句的语法错误。
源错误: 
行 156: this.myConnection .Open ();
行 157: cmd=new OleDbCommand (sql,this.myConnection );
行 158: cmd.ExecuteNonQuery ();
行 159: this.myConnection .Close  ();
行 160: this.DataGrid1 .EditItemIndex =-1;
源文件: c:\inetpub\wwwroot\webapplication2\webform1.aspx.cs    行: 158 
堆栈跟踪: 
[OleDbException (0x80040e14): UPDATE 语句的语法错误。]
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) +41
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +174
   System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +92
   System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +65
   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +112
   System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +66
   WebApplication2.WebForm1.DataGrid1_UpdateCommand(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\webapplication2\webform1.aspx.cs:158
   System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109
   System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
   System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +121
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1277另外还想知道:
我的理解对不对
e.Item .Cells [列名序号].Text是对单元格取值
而((TextBox)e.Item .Cells [4].Controls [0]).Text是对单元格里面的控件取值,就是有【编辑】等按钮在的单元格?
先谢谢了

解决方案 »

  1.   

    "where 学号='"+e.Item .Cells [0].Text+"'";
      

  2.   

    ",姓氏='"+e.Item .Cells [1].Text+
    "',名字="+e.Item .Cells [2].Text+
    "',职务='"+e.Item .Cells [3].Text+
    "',住址='"+e.Item .Cells [4].Text+
    "',年龄='"+e.Item .Cells [5].Text+
    "',性别='"+e.Item .Cells [6].Text+
      

  3.   

    e.Item .Cells [列名序号].Text是对单元格取值
    而((TextBox)e.Item .Cells [4].Controls [0]).Text是对单元格里面的控件取值,就是有【编辑】等按钮在的单元格?正确无误!!
    你的SQL语句写错了,建议你到查询分析器里试运行!
      

  4.   

    字符串得加上引号,方法如bitsbird(一瓢,生活就是折腾)所说。
      

  5.   

    在.net中使用的sql语句跟在sql server2000中的一样,如果你用的表字段是本文类型的要在更新语句用''括起新值.
      

  6.   

    你的语法少了''应该这么写 "update 表名 set 序号="'+e.Item .Cells [0].Text+'",姓氏="'+e.Item .Cells [1].Text+'""
      

  7.   

    你的sql语句"="后面应该加上''
    如,一开始删除的的那一句应该这样写:
    string sql="delete from 表1 where 学号='"+e.Item .Cells [0].Text+"'";
      

  8.   

    谢谢楼上各位的回答,已经改好了
    这是最后成功的代码:
    private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string updatesql="update 表1 set"+//" 序号="+((TextBox)e.Item .Cells [0].Controls [0]).Text+
    " 姓氏='"+((TextBox)e.Item .Cells[1].Controls [0]).Text+
    "',名字='"+((TextBox)e.Item .Cells[2].Controls [0]).Text+
    "',职务='"+((TextBox)e.Item .Cells[3].Controls [0]).Text+
    "',住址='"+((TextBox)e.Item .Cells[4].Controls [0]).Text+
    "',年龄="+((TextBox)e.Item .Cells[5].Controls [0]).Text+
    ",性别='"+((TextBox)e.Item .Cells[6].Controls [0]).Text+
    "' where 序号="+e.Item .Cells [0].Text;