GridView绑定的SqlDataSource异常
背景:我在使用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>