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

D

David

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?
 
V

Vince Xu [MSFT]

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
==================================================
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,334
Latest member
66Zeinab9

Latest Threads

Top