Avatar of troycomp
troycomp

asked on 

ObjectDataSource error

I have a gridview (points of interest)

DataKeyNames="ID"
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ItemStyle-Width="100" />
<asp:BoundField DataField="DESCRIPTION" HeaderText="Description" SortExpression="DESCRIPTION" />

bound to an objectdatasource

<asp:ObjectDataSource ID="dataSource" runat="server"
                DeleteMethod="Delete"
                InsertMethod="Insert"
                SelectMethod="Get"
                TypeName="MyData.MyClass"
                UpdateMethod="Update"
                >
                <SelectParameters>
                    <asp:Parameter Name="descrip" Type="String" />
                </SelectParameters>
                  <DeleteParameters>
                        <asp:Parameter Name="id" Type="Int32" />
                  </DeleteParameters>
                  <UpdateParameters>
                        <asp:FormParameter Name="id" Type="Int32" />
                    <asp:FormParameter Name="descrip" Type="String" />
                  </UpdateParameters>
                  <InsertParameters>
                    <asp:Parameter Name="descrip" Type="String" />
                  </InsertParameters>
            </asp:ObjectDataSource>

This is the method being called to update my table
      public void Update(int id, string descrip)
        {

            // create input parameter
            SqlParameter[] inparms = new SqlParameter[2];
            inparms[0] = new SqlParameter("@ID", SqlDbType.Int);
            inparms[1] = new SqlParameter("@DESCRIPTION", SqlDbType.VarChar);

            inparms[0].Value = id;
            inparms[1].Value = descrip;

            using (DAL data = new DAL())
            {
                data.ReturnNonQuery("up_NWCS_Admin_UpdateCommodity", inparms);
            }
        }

And this is the function takling to the database:
      public  int ReturnNonQuery(string sproc, SqlParameter[] p)
        {
            using (SqlConnection con = new SqlConnection(strConn))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = sproc;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddRange(p);
                    con.Open();
                    int i = cmd.ExecuteNonQuery();
                    return i;
                }
            }
        }

When i click update on my gridview i get the following error

ObjectDataSource 'dataSource' could not find a non-generic method 'Update' that has parameters: id, descrip, ID, DESCRIPTION.

Yes i tried adding OldValuesParameterFormatString="{0}" but it still fails with the same error. I looked everywhere now im turning to the experts.
.NET ProgrammingASP.NETC#

Avatar of undefined
Last Comment
troycomp
ASKER CERTIFIED SOLUTION
Avatar of NazoUK
NazoUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of troycomp
troycomp

ASKER

NazoUK thats why you're the expert. It worked!!! I'm assuming i do this for my deletes and inserts?
Avatar of troycomp
troycomp

ASKER

I got the insert to work as well, but delete is a slightly different task. Here is my delete method:

       public void DeleteCommodity(int ID)
        {
            // create input parameter
            SqlParameter[] inparms = new SqlParameter[1];
            inparms[0] = new SqlParameter("@ID", SqlDbType.Int);

            inparms[0].Value = ID;

            using (DAL data = new DAL())
            {
                data.ReturnNonQuery("up_NWCS_Admin_DeleteCommodity", inparms);
            }

        }

The ID is the number from the ID column which is stored in the gridview. Since this is a value the user wont be typing in, how do i pass in the ID of the specified row to the objectdatasource?
Avatar of NazoUK
NazoUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Gridview doesn't natively support inserting so you might need to handle that event yourself, but for other databound controls yes, it would be the same.
For deleting I think you need to add the delete parameter to the DataKeyNames attribute in the Gridview's definition and pass it using the delete parameters. Not 100% certain on that though.
Avatar of troycomp
troycomp

ASKER

Nevermind..i figured it out....Thanks
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo