ASP.Net MySql非泛型方法' UpdateQuery'

时间:2018-04-14 20:33:51

标签: mysql asp.net visual-studio

我遇到了GridView和DataSet的问题,其中up​​date命令似乎在尝试向SQL查询添加太多参数。我已经搜索了谷歌和StackOverflow以寻找潜在的修复方法,但似乎空手而归。

一切都很完美,直到我在进行必要的编辑后点击GridView上的Update链接。当我按下更新按钮时,我遇到以下错误:

ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'Update' that has parameters: p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, siteID, serialNumber, installDate, testDate, lastAlarmDate, alarmState, sensorType, sensorLocation, squelched, emsNotified, original_sensorID.

我已经验证了DataSet生成的Update语句,它反映如下:

UPDATE sensors SET siteID = @p1, serialNumber = @p2, installDate = @p3, testDate = @p4, lastAlarmDate = @p5, alarmState = @p6, sensorType = @p7, sensorLocation = @p8, squelched = @p9, emsNotified = @p10 WHERE (sensorID = @p11)

似乎有额外的数据附加到更新字符串的末尾,我无法找到附加这些附加变量的位置。非常感谢任何帮助。

感谢。

GridView标记

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="sensorID" DataSourceID="ObjectDataSource1" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="sensorID" HeaderText="sensorID" InsertVisible="False" ReadOnly="True" SortExpression="sensorID" />
                <asp:BoundField DataField="siteID" HeaderText="siteID" SortExpression="siteID" />
                <asp:BoundField DataField="serialNumber" HeaderText="serialNumber" SortExpression="serialNumber" />
                <asp:BoundField DataField="installDate" HeaderText="installDate" SortExpression="installDate" />
                <asp:BoundField DataField="testDate" HeaderText="testDate" SortExpression="testDate" />
                <asp:BoundField DataField="lastAlarmDate" HeaderText="lastAlarmDate" SortExpression="lastAlarmDate" />
                <asp:BoundField DataField="alarmState" HeaderText="alarmState" SortExpression="alarmState" />
                <asp:BoundField DataField="sensorType" HeaderText="sensorType" SortExpression="sensorType" />
                <asp:BoundField DataField="sensorLocation" HeaderText="sensorLocation" SortExpression="sensorLocation" />
                <asp:BoundField DataField="squelched" HeaderText="squelched" SortExpression="squelched" />
                <asp:BoundField DataField="emsNotified" HeaderText="emsNotified" SortExpression="emsNotified" />
                <asp:CommandField ShowEditButton="True" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

ObjectDataSource标记

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="DataSetTableAdapters.sitesTableAdapter" UpdateMethod="Update" OnSelecting="ObjectDataSource1_Selecting">
            <DeleteParameters>
                <asp:Parameter Name="p1" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="p1" Type="String" />
                <asp:Parameter Name="p2" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="p1" Type="String" />
                <asp:Parameter Name="p2" Type="String" />
                <asp:Parameter Name="p3" Type="Int32" />
            </UpdateParameters>
        </asp:ObjectDataSource>

编辑:Verbiage。 编辑2:标记。 编辑3:附加标记。

1 个答案:

答案 0 :(得分:0)

根据更新数据源的SQL查询语句,它有11个参数(10个SET参数和1个WHERE子句参数):

UPDATE sensors SET siteID = @p1, serialNumber = @p2, installDate = @p3, 
                   testDate = @p4, lastAlarmDate = @p5, alarmState = @p6, 
                   sensorType = @p7, sensorLocation = @p8, squelched = @p9, emsNotified = @p10 
               WHERE (sensorID = @p11)

但是在UpdateParameters中你只声明了3个参数,你应该知道ObjectDataSource要求CRUD参数的数量与上面查询语句中声明的参数完全匹配(也使用相同的参数名称),因此您需要调整查询参数的数量,如下所示:

<UpdateParameters>
    <asp:Parameter Name="p1" Type="String" />
    <asp:Parameter Name="p2" Type="String" />
    <asp:Parameter Name="p3" Type="DateTime" />
    <asp:Parameter Name="p4" Type="DateTime" />
    <%-- other required parameters --%>
    <asp:Parameter Name="p11" Type="Int32" />
</UpdateParameters>

其次,我发现异常需要original_sensorID,它反映了属性OldValuesParameterFormatString="original_{0}"的用法。尝试替换下面给出的旧值参数格式字符串,或者只删除该属性:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                      OldValuesParameterFormatString="{0}" ...>
</asp:ObjectDataSource>

旁注:

确保此属性TypeName="DataSetTableAdapters.sitesTableAdapter"设置为数据源的正确完全限定类型名称。

其他参考资料:

GridView with ObjectDataSource Example: Insert, Edit, Update

could not find a non-generic method 'Delete' that has parameters

ObjectDataSource could not find a non-generic method that has parameters when using update and delete