ObjectDataSource and Gridview control

Last post 07-03-2009 2:36 AM by Wencui Qian - MSFT. 5 replies.

Sort Posts:

  • ObjectDataSource and Gridview control

    06-30-2009, 1:55 PM
    • Member
      1 point Member
    • AaronW1981
    • Member since 06-30-2009, 5:42 PM
    • Posts 8

    Quick question regarding ODS and Gridview using a BLL. I have my class and function setup to add a new record to the table. I've configured the source to the method, class and everything. (all Insert parameters of the ODS were assigned in this process automatically)

    Now, I can't remember for the life of me if I need to set the CommandName of the submit button to "Insert". I tried this and it's expecting me to have an InsertMethod for the GridView. Anyone know what I am doing wrong with the Gridview?

    I get the following error after pressing the "submit" button which I would like to trigger the INSERT method of my BLL: TargetInvocationException was unhandled by user code: Exception has been thrown by the target of an invocation.


    Thanks in advance

  • Re: ObjectDataSource and Gridview control

    06-30-2009, 3:03 PM
    Answer
    • Star
      7,932 point Star
    • amit.jain
    • Member since 10-06-2008, 5:09 AM
    • Delhi India
    • Posts 1,264

    Write code like this in html
    <asp:Button ID="btnInsert" runat="server"
                    Text="Insert" CommandName="Add" />


    And this in code behind

    protected void GridView1_RowCommand
    (object sender, GridViewCommandEventArgs e)
    {
      if (e.CommandName == "Add")
      {
       string strFirstName = ((TextBox)
       GridView1.FooterRow.FindControl("txtFname")).Text;


    refer this for more info and this for ObjectDataSource

    Don't say thanks rather mark my reply as "Answer" if it helps you ,Doing so u'll get points too
    amiT jaiN

    ASP.NET C#.NET Articles
  • Re: ObjectDataSource and Gridview control

    06-30-2009, 3:29 PM
    • Member
      1 point Member
    • AaronW1981
    • Member since 06-30-2009, 5:42 PM
    • Posts 8

    I understand that. I thought the whole point of having a BLL was that the ODS could call the function written in the class and supply it the parameters. In my case, I "configured" the data source which added <insertparameters> for me automatically. Why do I still need to programmatically declare the parameters? Isn't this doubling the work?

  • Re: ObjectDataSource and Gridview control

    06-30-2009, 3:40 PM
    • Star
      7,932 point Star
    • amit.jain
    • Member since 10-06-2008, 5:09 AM
    • Delhi India
    • Posts 1,264

    you have to because u can't use Bind method for inserting and gridview is not actually for inserting records

    Don't say thanks rather mark my reply as "Answer" if it helps you ,Doing so u'll get points too
    amiT jaiN

    ASP.NET C#.NET Articles
  • Re: ObjectDataSource and Gridview control

    06-30-2009, 4:08 PM
    • Member
      1 point Member
    • AaronW1981
    • Member since 06-30-2009, 5:42 PM
    • Posts 8

    I actually made a typo in the title, I meant FormView. :)


  • Re: ObjectDataSource and Gridview control

    07-03-2009, 2:36 AM
    Answer

    Hi AaronW1981,

    AaronW1981's source code is fine. You have to validate the command name within ItemCommand method to implement insert function. In FormView, there's no direct way to call the insert method, which is different from DetailsView. Here's a sample of FormView with insert function:

    http://knowlegezone.com/documents/70/ASPNET-Formview--GridView-Coltrol-Insert-Update-Paging-And-Delete/

    Thanks.

    David Qian
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (6 items)