Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Web Controls (http://www.velocityreviews.com/forums/f63-asp-net-web-controls.html)
-   -   Adding "other" data in a formview on insert and update (http://www.velocityreviews.com/forums/t781020-adding-other-data-in-a-formview-on-insert-and-update.html)

David 05-14-2009 07:33 PM

Adding "other" data in a formview on insert and update
 
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("PricingDateFie ld");
pdatefield.Value = DateTime.Now.ToString();


}

}

Is there a better way?


Vince Xu [MSFT] 05-15-2009 04:35 AM

RE: Adding "other" data in a formview on insert and update
 
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/subs...#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/subs.../aa948874.aspx
==================================================


David 05-15-2009 08:00 PM

RE: Adding "other" data in a formview on insert and update
 
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/subs...#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/subs.../aa948874.aspx
> ==================================================
>
>



All times are GMT. The time now is 04:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57