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是对单元格里面的控件取值,就是有【编辑】等按钮在的单元格?
先谢谢了
{
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是对单元格里面的控件取值,就是有【编辑】等按钮在的单元格?
先谢谢了
解决方案 »
- 做过Jsp的进来下。
- asp.net2.0中使用ajax出现错误
- js为2个相同id的控件修改样式
- 如何导入Excel
- 知道了treeview控件节点的名字,怎样在客户端利用javascript查找该节点并处于选中状态。
- 是否System.Web.Mail不能发送邮件???????????
- 请教datagrid删除列问题~!
- 关于Response.Redirect的简单问题,请各位帮忙。
- System.Data.SqlClient.SqlException: 用户 'QHC\ASPNET' 登录失败
- Web Form上滚动字幕如何实现。
- 像网站上那种上一步、下一步是怎么实现的,数据是保存在那个地方呢,
- 为什么老出现目前访问网站的用户过多?
"',名字="+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+
而((TextBox)e.Item .Cells [4].Controls [0]).Text是对单元格里面的控件取值,就是有【编辑】等按钮在的单元格?正确无误!!
你的SQL语句写错了,建议你到查询分析器里试运行!
如,一开始删除的的那一句应该这样写:
string sql="delete from 表1 where 学号='"+e.Item .Cells [0].Text+"'";
这是最后成功的代码:
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;