locked
FormView - update with custom code? RRS feed

  • Question

  • User260076833 posted

    Hello,

    I have a FormView, which is based on a SqlDataSource.
    The SqlDataSource has a SelectCommand, but no UpdateCommand.

    When the user edits the form and presses "update", I get an error message, because there is no update command.

    However, in this case I cannot specify a single SQL command as the update command. Instead, I have to do it in C#.

    How can I realize this?

    Thanks
    Magnus

    Friday, September 23, 2016 9:19 AM

Answers

  • User260076833 posted

    Hello and thanks for your postings!

    I am now using a FormView without data source and do the edit/update operations manually in the ItemCommand event.

    I believe that this is the cleaner solution, because there is no "record" to edit or update here. Even the SelectCommand in the data source of my first approach was a fake: The data it selected was not used by the form at all.

    Cheers
    Magnus

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2016 6:34 AM

All replies

  • User36583972 posted

    Hi Yeoman,

    I have a FormView, which is based on a SqlDataSource.
    The SqlDataSource has a SelectCommand, but no UpdateCommand.

    When you create a FormView with  SQLDataSource control. You can write Insert, Delete, update command in SQLDataSource and pass parameter.

    You can refer the following tutorial.

    FormView: Select, Insert, update and delete data in Form View using SqlDataSource:

    http://programmerskills.blogspot.sg/2015/09/aspnet-select-insert-update-and-delete.html

    Best Regards,

    Yohann Lu

    Saturday, September 24, 2016 3:34 AM
  • User409696431 posted

    "However, in this case I cannot specify a single SQL command as the update command. Instead, I have to do it in C#."

    Please explain more fully why you cannot specify a single SQL update command, and why you "have to do it in C#".  It would help people understand what you are looking for.

    Saturday, September 24, 2016 5:34 AM
  • User260076833 posted

    Hello,

    ok, here is the reason why:

    The FormView represent's a user's memberships in different groups. In the database, there is a table for all the users, and a table for all the groups. And there is another table for all the memberships:

    USER:
    idx fname  lname
    ---------------
    1   Magnus Warker
    2   Peter  Pan
    3   Donald Duck
    
    GROUP:
    idx name
    1   Administrators
    2   Room Managers
    3   Reservation Managers
    4   Telephony Managers
    5   First Aiders

    MEMBERSHIP:
    user group
    1 1
    2 3
    2 5
    3 3
    3 4


    The FormView represents all the memberships for a given user. It is shown with a CheckBoxList. There is one CheckBox for each group, and a group is checked when the given user is a member of this group.

    However, when this CheckBoxList was edited and has to be saved, I need to iterate through all the CheckBoxes and do the following:

    • when it's checked: INSERT INTO MEMBERSHIP (user,group) ...
    • when it's unchecked: DELETE FROM MEMBERSHIP...

    Nevertheless, I need a trigger that lets me execute arbitrary code behind when the form is saved.

    Thanks
    Magnus

    Saturday, September 24, 2016 10:43 PM
  • User36583972 posted

    Hi Yeoman,

    Nevertheless, I need a trigger that lets me execute arbitrary code behind when the form is saved

    The description is not very clear, you can describe in detail the effect you want.

    If the problem is a new question, I suggest you can post a new thread to Related forums.

    Best Regards,

    Yohann Lu

    Monday, September 26, 2016 11:20 AM
  • User260076833 posted

    Hello,

    I believe the description is clear. Can you tell what is missing?

    The approach I am thinking of would hook the ItemUpdating event and do the data saving there. However, there is still the error message telling that there is no UpdateCommand associated with the FormView.

    Magnus

    Monday, September 26, 2016 11:58 AM
  • User36583972 posted

    Hi  Yeoman,

    there is still the error message telling that there is no UpdateCommand associated with the FormView.

    You can share us more relevant code to help us reproduce the problem.

    Best Regards,

    Yohann Lu

    Friday, September 30, 2016 8:28 AM
  • User409696431 posted

    The message is pretty clear.  You do need an Update command for ItemUpdating to work.

    You could skip using the FormView's built-in processing for updating, and just put your own button on the page to trigger an Update, and process the data on that button's click event.

    Friday, September 30, 2016 11:18 PM
  • User260076833 posted

    Hello,

    thank you for your answer.

    I tried an approach it without a DataSource for the FormView.

    Then, I had to find out where to initially load the form data. I wanted to load it in the Page_Load event:

    protected void Page_Load(object sender, EventArgs e)
    {
     if (!IsPostBack)
     {
      MyCustonWidget wgt = (MyCustonWidget)frm.FindControl("MyCustonWidget");
      ... // load data
     }
    }

    However, there is a custom widget for my data, and when I try to load my data in the Page_Load event, this widget cannot be retrieved, it's null.

    Can you tell me how to make a FormView without DataSource? Why is my custom widget null and how can I use the custom widget in a FormView without DataSource?


    Thanks
    Magnus

    Sunday, October 2, 2016 10:24 PM
  • User409696431 posted

    You don't have to create a FormView without a datasource.  Your problem only relates to updating data from that Form, which, as I said, you could do without using FormView's update, but an independent update button triggering your own code to read the values and update the database accordingly (and then rebind the formview if the user stays on that page).

    Monday, October 3, 2016 4:45 AM
  • User260076833 posted

    Hello and thanks for your postings!

    I am now using a FormView without data source and do the edit/update operations manually in the ItemCommand event.

    I believe that this is the cleaner solution, because there is no "record" to edit or update here. Even the SelectCommand in the data source of my first approach was a fake: The data it selected was not used by the form at all.

    Cheers
    Magnus

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2016 6:34 AM