Very Vanilla SQL/TableAdapter/ObjectDatasource/FormView update fails

Last post 10-05-2006 11:08 PM by codequest. 7 replies.

Sort Posts:

  • Very Vanilla SQL/TableAdapter/ObjectDatasource/FormView update fails

    09-08-2006, 7:38 PM
    • Contributor
      2,127 point Contributor
    • codequest
    • Member since 10-30-2005, 2:55 PM
    • Posts 461

    This is the error

    “ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'Update' that has parameters...”  

    I figured it out for Gridview but not for FormView (my Gridview solution was to change ID field ReadOnly to False).

     

    I tried to reduce this to a very simple test case and then apply some of the fixes posted around the web, but, so far, no joy.

     

    Anyone that has VS2005/SQLExpress running can replicate this in a few minutes.

     

    ===============


    My test procedure (I tried to make it as vanilla as possible)

     

    If needed: 

    .1)  Open a new website

    .2)  Add App_Code folder

    .3)  Create a new dataset, default name

    .4)  Add SQL DB to App_Data Folder

     

    .5)  Add Table to DB

     

    SQL Table  =   aaaTest

    ===

    ID            int         identity

    Field1      varchar(50)
    Field2      varchar(50)

    =============== 

     

    1)     Open the Dataset Designer for the dataset

    1.1)  Drag table aaaTest table to the Dataset designer

    2)     Configure using the minimum number of key strokes (next, next, .finish)

     

    3) Create Webform aaaTest

     

    4)  Drag on an ObjectDataSource

    5)  Point it at aaaTest TableAdapter

    6)  Configure with minimum key strokes (next, next, .. finish)

     

    7)  Drag on a FormView

    5)  Point it at the ObjectDataSource

    6)  Select "Enable Paging"

     

    9)  aaaTable is empty, so you won’t see a the FormView unless you do one of these

     

       a)  add a row to aaaTable from the Server explorer, or

     

       b)  Add this to the Page Code_Behind

     

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

    Me.FormView1.DefaultMode = FormViewMode.Insert

    Exit Sub

    End If

    Me.FormView1.DefaultMode = FormViewMode.ReadOnly

    End Sub

     

    10)  Run it

     

    11)   Test insert (DO A FEW inserts)  -- OK

    12)   Test delete (But don’t delete your  last record)  – OK

    13)   (lucky number) Test edit -  Get error:

     

    ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'Update' that has parameters: Field1, Field2, original_ID.

     

    Here’s the page code

     

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="aaaTest.aspx.vb" Inherits="App_Pages_zzTest_aaaTest" %>

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

     

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

        <title>Untitled Page</title>

    </head>

    <body>

        <form id="form1" runat="server">

                            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete"

                                        InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"

                                        TypeName="APP_Maint_DSTableAdapters.aaaTestTableTableAdapter" UpdateMethod="Update">

                                        <DeleteParameters>

                                                    <asp:Parameter Name="Original_ID" Type="Int32" />

                                        </DeleteParameters>

                                        <UpdateParameters>

                                                    <asp:Parameter Name="Field1" Type="String" />

                                                    <asp:Parameter Name="Field2" Type="String" />

                                                    <asp:Parameter Name="ID" Type="Int32" />

                                                    <asp:Parameter Name="Original_ID" Type="Int32" />

                                        </UpdateParameters>

                                        <InsertParameters>

                                                    <asp:Parameter Name="Field1" Type="String" />

                                                    <asp:Parameter Name="Field2" Type="String" />

                                        </InsertParameters>

                            </asp:ObjectDataSource>

                            <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="ID"

                                        DataSourceID="ObjectDataSource1">

                                        <EditItemTemplate>

                                                    ID:

                                                    <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>'></asp:Label><br />

                                                    Field1:

                                                    <asp:TextBox ID="Field1TextBox" runat="server" Text='<%# Bind("Field1") %>'>

                                                    </asp:TextBox><br />

                                                    Field2:

                                                    <asp:TextBox ID="Field2TextBox" runat="server" Text='<%# Bind("Field2") %>'>

                                                    </asp:TextBox><br />

                                                    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"

                                                                Text="Update">

                                                    </asp:LinkButton>

                                                    <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"

                                                                Text="Cancel">

                                                    </asp:LinkButton>

                                        </EditItemTemplate>

                                        <InsertItemTemplate>

                                                    Field1:

                                                    <asp:TextBox ID="Field1TextBox" runat="server" Text='<%# Bind("Field1") %>'>

                                                    </asp:TextBox><br />

                                                    Field2:

                                                    <asp:TextBox ID="Field2TextBox" runat="server" Text='<%# Bind("Field2") %>'>

                                                    </asp:TextBox><br />

                                                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"

                                                                Text="Insert">

                                                    </asp:LinkButton>

                                                    <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"

                                                                Text="Cancel">

                                                    </asp:LinkButton>

                                        </InsertItemTemplate>

                                        <ItemTemplate>

                                                    ID:

                                                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>'></asp:Label><br />

                                                    Field1:

                                                    <asp:Label ID="Field1Label" runat="server" Text='<%# Bind("Field1") %>'></asp:Label><br />

                                                    Field2:

                                                    <asp:Label ID="Field2Label" runat="server" Text='<%# Bind("Field2") %>'></asp:Label><br />

                                                    <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"

                                                                Text="Edit">

                                                    </asp:LinkButton>

                                                    <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"

                                                                Text="Delete">

                                                    </asp:LinkButton>

                                                    <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"

                                                                Text="New">

                                                    </asp:LinkButton>

                                        </ItemTemplate>

                            </asp:FormView>

        </form>

    </body>

    </html>

     

    ----------------------------------------------------------------

     

    Here’s the tableadapter update query

     

    UPDATE [dbo].[aaaTestTable] SET [Field1] = @Field1, [Field2] = @Field2 WHERE (([ID] = @Original_ID));

    SELECT ID, Field1, Field2 FROM aaaTestTable WHERE (ID = @ID)

     -------------------------------------------------------------

     

    As I mentioned, I tried several combinations of changes based on suggestions from posts here and elsewhere, and could not get it to work.  

     

    I can play with the code I can see, but since I’m functionally a “hobby programmer” I don’t have a lot of theory of internals to work with.


    Any help with getting this to work would be appreciated.   Pre-tested solutions would be much preferred, of course, since I've tried lots of combinations already.  I'll keep researching and experimenting in any case.

     

    If I find an answer I will definitely post it back here.

     

    Thanks!

  • Re: Very Vanilla SQL/TableAdapter/ObjectDatasource/FormView update fails

    09-08-2006, 9:18 PM
    Answer
    • Contributor
      2,127 point Contributor
    • codequest
    • Member since 10-30-2005, 2:55 PM
    • Posts 461
    Correction:   need to make ID a primary key in the test table, or delete won’t be generated by TableAdapter

    FIXED IT!  (I'm pretty sure)

    STEP 1)

    Original Update Parameters in ObjectDataSource (ODS)
    <UpdateParameters>
         <asp:Parameter Name="Field1" Type="String" />
         <asp:Parameter Name="Field2" Type="String" />
         <asp:Parameter Name="Original_ID" Type="Int32" />
         <asp:Parameter Name="ID" Type="Int32" />
    </UpdateParameters>


    Changed the Update Parameters in the Page Source Code to this  (removed the ID parameter):

    <UpdateParameters>
             <asp:Parameter Name="Field1" Type="String" />
             <asp:Parameter Name="Field2" Type="String" />
             <asp:Parameter Name="Original_ID" Type="Int32" />
    </UpdateParameters>


    >>>  But that’s not enough...just removing the ID parameter doesn’t stop the error

    So, STEP 2  

    Added a Query to the Table Adapter
       Selected Update Query --- it gave me most of what I needed in the Query Builder, so didn't have to reconstruct the whole query...which will help on the "real table" I'm working on, with lots of fields

       Modified what the Add Query function gave me to read like this  (so that it uses "@Original_ID" instead of "@ID" in select query:
       
    UPDATE    aaaTestTable
    SET              Field1 = @Field1, Field2 = @Field2
    WHERE     (ID = @Original_ID);
    SELECT ID, Field1, Field2 FROM aaaTestTable WHERE (ID = @Original_ID)

    Ran it...the update worked.  YAY

    (This was the originally generated UPDATE Command from the table adapter....you can see the select uses @ID)

    UPDATE [dbo].[aaaTestTable] SET [Field1] = @Field1, [Field2] = @Field2 WHERE (([ID] = @Original_ID));
    SELECT ID, Field1, Field2 FROM aaaTestTable WHERE (ID = @ID)

    SO...

    From my very newbie point of view, I speculate that the issue here is that there are two queries, an update and a select, and that they have two different method signatures.

    The automagic wiring between the tableadapter and the objectdatasource thinks the objectdatasource needs the Field1,Field2,Original_ID singature for the update, and the Field1, Field2, ID signature for the Select...so it generates parameters for all four fields.

    But then when it looks for the Update method, it finds only a method that has three parameters, and so the extra ID is superfluous, and is potentially an error.

    On the other hand, if you leave out the ID parameter, the select query blows up.

    So the ODS error “doesn’t find non-generic” may actually be an inaccurate error report.   It could be the wiring blows up as described above, and the ODS reports the error because it doesn’t have a SMARTER ERROR message.

    So maybe the heart of this nasty confusing puzzle is that the ERROR message is accurate, but not precise...(or something like that)

    Anyway I got the fix to work on my idiot test case (which I repeated, with another whole test case, just to make sure).   Now back to my complex table layout....fingers crossed.
  • Re: Very Vanilla SQL/TableAdapter/ObjectDatasource/FormView update fails

    09-08-2006, 9:32 PM
    • Contributor
      2,127 point Contributor
    • codequest
    • Member since 10-30-2005, 2:55 PM
    • Posts 461

    Oh yeah yeah yeah yeah

    STEP 3:  You gotta go back into configure ObjectDataSource and point it's update tab entry to the new updatequery

    And it DID work on my complex table....YAY!

  • Re: Very Vanilla SQL/TableAdapter/ObjectDatasource/FormView update fails

    09-08-2006, 10:03 PM
    • Contributor
      2,127 point Contributor
    • codequest
    • Member since 10-30-2005, 2:55 PM
    • Posts 461

    More on this....

    Another reason this is so elusive is that when you look at the TableAdapter > Properties > UpdateCommand > CommandText and hit the little [...] button, the Query Builder doesn't show the Select Query.   However, if you run the cursor along the CommandText String, it shows up way off to the left.   This must be the source of the "Update" starting point when you try to add a new update query.

    One thing to note about this is that you can edit this command text line by itself, without opening the Query Builder...either by copy out/past back in, or by edit in place...and so you don't have to add a new update query.

    And now that I write this, I recognize some of the then-indecipherable-to-my-newbie-brain language in some of the existing posts on this subject...which were probably pointing to this solution.

    Though as Chris Tucker put it in the 5th Element..."BUT WHO CARES!"   Glad to put this one behind me...

     

     

     

  • Re: Very Vanilla SQL/TableAdapter/ObjectDatasource/FormView update fails

    10-04-2006, 5:41 PM
    • Member
      55 point Member
    • Drummos
    • Member since 08-05-2006, 5:37 PM
    • Posts 27

    Ahhh,

    Thank you.  I knew if I searched long enough someone else would have had this issue.  And I'm glad to say, from my own newb perspective, I was on the right track to solving it.  I'm going to work through your correction and hopefully I'll be smiling soon.  :)

    Ally

  • Re: Very Vanilla SQL/TableAdapter/ObjectDatasource/FormView update fails

    10-04-2006, 6:47 PM
    • Contributor
      2,127 point Contributor
    • codequest
    • Member since 10-30-2005, 2:55 PM
    • Posts 461

    One caveat I have learned since...my use case involved Tables with Primary Key = Int, Identity.   When I was playing around with this I "thought I might have seen" an instance where it didn't work because it wasn't Primary Key = Int, Identity...but I never went back to find out.  So...just a heads up...

    Also, issue has been thoroughly folded, spindled and mutated here...

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=10833&SiteID=1

    Good luck!

  • Re: Very Vanilla SQL/TableAdapter/ObjectDatasource/FormView update fails

    10-05-2006, 5:52 PM
    • Member
      55 point Member
    • Drummos
    • Member since 08-05-2006, 5:37 PM
    • Posts 27

    Cool thanks,

     I'll check that thread out.  And, it did work by the way.  Thanks again.

    Ally

  • Re: Very Vanilla SQL/TableAdapter/ObjectDatasource/FormView update fails

    10-05-2006, 11:08 PM
    • Contributor
      2,127 point Contributor
    • codequest
    • Member since 10-30-2005, 2:55 PM
    • Posts 461
    Thanks; glad it was helpful!
Page 1 of 1 (8 items)