Adding "other" data in a formview on insert and update

Discussion in 'ASP .Net Web Controls' started by David, May 14, 2009.

  1. David

    David Guest

    Is there a "good" way, when using a formview control, to add other data to a
    database at the time of update. This might include a time of update, or
    perhaps a calculated field that comes from two other fields.

    So, when an update comes in, I want to call the update command (or insert),
    which will take all the data from the fields, and add them to the update
    statement. However, I want to also add some other data that isn't displayed.

    I came up with two ways to do this, and neither one seems very elegant. I
    figured I could either set the value of a hidden field, and bind that field
    to a parameter in the update, or I could set the defaultvalue property for
    one of the parameters. Either way, I don't like it. In part, I don't like
    it because I am going back and forth to text.

    In a Windows Forms app, I would have a SQLCommand object with a parameters
    collection, and I could set the value of the parameters to the appropriate
    object.

    Here's some sample code showing what I have done, with an example of each
    style, for the update and insert commands. The goal in each case is to save
    the time of the transaction. For an insert command, there is a
    "CreationDate" field, and for the Update command, there is a "PricingDate"
    field that I bound to a HiddenField object:


    protected void FormView1_ItemCommand1(object sender,
    FormViewCommandEventArgs e)
    {
    if (e.CommandName == "Insert")
    {
    SqlDataSource2.InsertParameters["CreationDate"].DefaultValue
    = DateTime.Now.ToString();

    }
    if (e.CommandName == "Update")
    {
    HiddenField pdatefield =
    (HiddenField)FormView1.FindControl("PricingDateField");
    pdatefield.Value = DateTime.Now.ToString();


    }

    }

    Is there a better way?
    David, May 14, 2009
    #1
    1. Advertising

  2. Hello David,

    In ASP.Net, we can only use InsertParameter, UpdateParameter or
    DeleteParameter properties to set the default value of it in ItemCommand
    event of FormView. The type of them is ParameterCollection, the value you
    set will be stored in ViewState. Based on my understanding, you want to set
    the parameter directly, instead of storing the value in ViewSate. If I have
    misunderstood you, please feel free to let me know.

    As far as I know, there are two approaches we can use.
    Firstly approach is we can set the parameter directly in Inserting event of
    SqlDataSource. (Parameter object is created before Inserting event and
    after ItemCommand, so it is available in Inserting event of SqlDataSource.)

    protected void SqlDataSource2_Inserting(object sender,
    SqlDataSourceCommandEventArgs e)
    {
    e.Command.Parameters["@CategoryName"].Value = DateTime.Now;
    }

    In another way, you can redefine the InsertCommand/UpdateCommand text
    directly in FormView1_ItemCommand.

    protected void FormView1_ItemCommand(object sender,
    FormViewCommandEventArgs e)
    {
    if (e.CommandName == "Insert")
    {
    SqlDataSource2.InsertCommand = "INSERT INTO table (XX, XX)
    VALUES (XX, '"+DateTime.Now+"')";
    SqlDataSource2.InsertCommandType =
    SqlDataSourceCommandType.Text;
    }
    }






    Sincerely,

    Vince Xu

    Microsoft Online Support


    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    MSDN Managed Newsgroup support offering is for non-urgent issues where an
    initial response from the community or a Microsoft Support Engineer within
    2 business day is acceptable. Please note that each follow up response may
    take approximately 2 business days as the support professional working with
    you may need further investigation to reach the most efficient resolution.
    The offering is not appropriate for situations that require urgent,
    real-time or phone-based interactions. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    Vince Xu [MSFT], May 15, 2009
    #2
    1. Advertising

  3. David

    David Guest

    Excellent. Exactly what I was looking for. Thanks.

    "Vince Xu [MSFT]" wrote:

    > Hello David,
    >
    > In ASP.Net, we can only use InsertParameter, UpdateParameter or
    > DeleteParameter properties to set the default value of it in ItemCommand
    > event of FormView. The type of them is ParameterCollection, the value you
    > set will be stored in ViewState. Based on my understanding, you want to set
    > the parameter directly, instead of storing the value in ViewSate. If I have
    > misunderstood you, please feel free to let me know.
    >
    > As far as I know, there are two approaches we can use.
    > Firstly approach is we can set the parameter directly in Inserting event of
    > SqlDataSource. (Parameter object is created before Inserting event and
    > after ItemCommand, so it is available in Inserting event of SqlDataSource.)
    >
    > protected void SqlDataSource2_Inserting(object sender,
    > SqlDataSourceCommandEventArgs e)
    > {
    > e.Command.Parameters["@CategoryName"].Value = DateTime.Now;
    > }
    >
    > In another way, you can redefine the InsertCommand/UpdateCommand text
    > directly in FormView1_ItemCommand.
    >
    > protected void FormView1_ItemCommand(object sender,
    > FormViewCommandEventArgs e)
    > {
    > if (e.CommandName == "Insert")
    > {
    > SqlDataSource2.InsertCommand = "INSERT INTO table (XX, XX)
    > VALUES (XX, '"+DateTime.Now+"')";
    > SqlDataSource2.InsertCommandType =
    > SqlDataSourceCommandType.Text;
    > }
    > }
    >
    >
    >
    >
    >
    >
    > Sincerely,
    >
    > Vince Xu
    >
    > Microsoft Online Support
    >
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > MSDN Managed Newsgroup support offering is for non-urgent issues where an
    > initial response from the community or a Microsoft Support Engineer within
    > 2 business day is acceptable. Please note that each follow up response may
    > take approximately 2 business days as the support professional working with
    > you may need further investigation to reach the most efficient resolution.
    > The offering is not appropriate for situations that require urgent,
    > real-time or phone-based interactions. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    > ==================================================
    >
    >
    David, May 15, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred Dag
    Replies:
    0
    Views:
    1,666
    Fred Dag
    Sep 18, 2006
  2. Larry Bud

    Formview update--one field won't update

    Larry Bud, Jul 11, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    383
    Larry Bud
    Jul 11, 2007
  3. Frank Burleigh
    Replies:
    0
    Views:
    413
    Frank Burleigh
    Aug 3, 2007
  4. David Thielen

    FormView/Repeater/ Eval(FormView data)

    David Thielen, Jun 16, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    621
    David Thielen
    Jun 16, 2006
  5. OnRails Ruby
    Replies:
    2
    Views:
    138
    bbiker
    Oct 20, 2007
Loading...

Share This Page