tags:

views:

33

answers:

3

Can anyone help. I am using a formview in VS 2005. I have different elements in my form databound to a database and I am performing an INSERT SQL statement. No problem. The problem is that I am trying to enter the current date into the SQL statement and I am having a problem.

I can add <%now()%> to the "Text" property of the asp:Textbox. But when I do, then I can't bind the textbox to a specific database column.

How do I do both????

I can do this:

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Initiate_Date") %>' ></asp:TextBox>

Or this:

<asp:TextBox ID="TextBox2" runat="server" Text='<%# now() %>' ></asp:TextBox>

But I don't know how to do both.

A: 

Leave it bound to Initiate_Date in the aspx but in your code behind set it to Now() when you need to....

klabranche
Thanks! You led me in the right direction. I just set now() to a session variable in the code behind and then added a sessionparameter tag inside the insertparameter tag, then passed that parameter to the SQL statement.
Anthony
You are most welcome. :-)
klabranche
A: 

Handle the ItemUpdating method of your formview. There you can cancel the event and write your own db handling code by getting the values from the formview, or you can just change the data item in the formview and inject your chages (and let the formview handle teh database)

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.itemupdating.aspx

Midhat
A: 

I needed to get the now() value into the selectcommand of my sqldatasource. There was no need for a textbox.

This is what I ultimately did.

On the code behind I created a session variable and set Now() to the value.

> Protected Sub Page_Load(ByVal sender
> As Object, ByVal e As
> System.EventArgs) Handles Me.Load
>             Dim RightNow As Date = Now()
>             Dim RightNowShort As Date = RightNow.ToShortDateString
>     
>             Session("RightNow") = RightNowShort
>         End Sub

On the aspx page I use the variable in a sessionparameter tag in the InsertParameters tag of my SqlDataSource.

<InsertParameters>
   <asp:sessionparameter sessionfield="RightNow" type="String" name="RightNow" /> 
</InsertParameters>

Thanks.

Anthony