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