GridView绑定的SqlDataSource异常

lbxx159753 2012-03-26 09:37:14
背景:我在使用GridView时,绑定了一个SqlDataSource,然后利用CommandField自带的更新(就是CommandName="Update"的按钮)进行数据库更新时遇到了个很奇怪的问题。一直提示System.Data.SqlClient.SqlException: '(' 附近有语法错误。但是我检查了写的语句,并没发现什么问题啊。而且GridView的数据绑定没有任何问题,这应该能说明数据库能连通并且SqlDataSource的Select语句能使用,但是Update就不行,Delete也不行。

下面是我在运行时候出现的错误。根据红色的堆栈调用信息来看,问题应该出在Update的sql语句上,但是我的Update语句是UpdateCommand="SELECT * FROM aspnet_Roles WHERE (1 = 2)",最最奇怪的是,如果我写个错误的Sql语句(比如UpdateCommand="SELECT")依然提示(附近有语法错误;还有如果我让UpdateCommand=""或者直接删掉这一句,出现的错误则为"除非指定了 UpdateCommand,否则数据源“SqlDataSource_GoodsInfo”不支持更新操作"。我把我的代码发到最后面,大侠们帮我分析分析哪里错误了吧。


“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------

'(' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.SqlClient.SqlException: '(' 附近有语法错误。

源错误:

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

堆栈跟踪:


[SqlException (0x80131904): '(' 附近有语法错误。]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1950298
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4856459
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1121
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +200
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +175
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +386
System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +325
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +92
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +907
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +704
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +123
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


这里是GridView和SqlDataSource的页面代码
<asp:GridView ID="GridView_GoodsInfo" runat="server" AllowSorting="True" CellPadding="4"
DataSourceID="SqlDataSource_GoodsInfo" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="id">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton_Delete" runat="server" CausesValidation="False" CommandName="Delete" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="名称" HeaderText="名称" SortExpression="名称" />
<asp:BoundField DataField="长(mm)" HeaderText="长(mm)" SortExpression="长(mm)" />
<asp:BoundField DataField="宽(mm)" HeaderText="宽(mm)" SortExpression="宽(mm)" />
<asp:BoundField DataField="高(mm)" HeaderText="高(mm)" SortExpression="高(mm)" />
<asp:BoundField DataField="体积" HeaderText="体积" SortExpression="体积" ReadOnly="true" />
<asp:BoundField DataField="货架位置" HeaderText="货架位置" SortExpression="货架位置" ReadOnly="true" />
<asp:BoundField DataField="外包装形式" HeaderText="外包装形式" SortExpression="外包装形式" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton_Edit" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton_Update" runat="server" CausesValidation="True" CommandName="Update" Text="更新"></asp:LinkButton>
 
<asp:LinkButton ID="LinkButton_Cancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource_GoodsInfo" runat="server" ConnectionString="<%$ ConnectionStrings:SaConnectionString %>"
SelectCommand="SELECT id, name AS 名称, length AS [长(mm)], width AS [宽(mm)], height AS [高(mm)], volume AS 体积, position AS 货架位置, type AS 外包装形式 FROM AFrame_GoodsInfo"
UpdateCommand="SELECT * FROM aspnet_Roles WHERE (1 = 2)">
</asp:SqlDataSource>


这里是Web.config里面的连接字符串

<connectionStrings>
<add name="ASPNETDBConnectionString" connectionString="Data Source=LENOVO-PC\SQLEXPRESS;AttachDbFilename=F:\workspace\AFrame\AFrame\App_Data\ASPNETDB.MDF;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="SaConnectionString" connectionString="Data Source=LENOVO-PC\SQLEXPRESS;Initial Catalog=F:\WORKSPACE\AFRAME\AFRAME\APP_DATA\ASPNETDB.MDF;Persist Security Info=True;User ID=sa;Password=sa;"
providerName="System.Data.SqlClient" />

</connectionStrings>
...全文
146 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lbxx159753 2012-03-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 netstudy0105 的回复:]

你的1=2是什么,你让update更新那条数据到底,你改成根据主键更新看看吧
[/Quote]
我的1=2就是让条件永远为假。因为我想自己定义Update语句的内容,但又不想让GridView编辑模式下的更新对数据库进行操作,所以就写了个不会执行任何一条结果sql语句。我记得以前写这样的语句的时候,有个公共表能够使用,相当于对这个公共表进行查询操作就等于一个空操作。但是现在忘掉了。
lbxx159753 2012-03-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bkq421511585 的回复:]

这个提问 看的蛋疼。。。你整理下吧。。
[/Quote]
我写得比较多,不好意思啊。主要的意思就是我检查了页面代码里面的Gridview和SqlDataSource设置,web.config里面的连接字符串的设置。都没有发现明显的语句错误,但是依然出现了错误提示,而且这个错误根本不知道在哪里发生的。我用debug模式都跟踪不到此错误,应该属于外部代码里面的错误。所以就发个帖子请求帮助下。
netstudy0105 2012-03-26
  • 打赏
  • 举报
回复
你的1=2是什么,你让update更新那条数据到底,你改成根据主键更新看看吧
冰镇宝贝321 2012-03-26
  • 打赏
  • 举报
回复
这个提问 看的蛋疼。。。你整理下吧。。

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧