ARGH! Last hope... Passing the Current DateTime into a Parameter in

Can anyone help?

I’m trying to build a basic forum. (and i mean basic, no competition to this mighty forum!)

Now this is how my Details View and Object Data Source looks…

<asp:DetailsView ID="dtvReply" runat="server" AutoGenerateRows="False" DataKeyNames="fpId" DataSourceID="odsReply" GridLines="None" DefaultMode="Insert" >

                                        <asp:TextBox ID="txt_summary" CssClass="textbox" runat="server" Text='<%# Bind("fpSummary") %>'></asp:TextBox>
                                       <%-- <asp:RequiredFieldValidator CssClass="error" ID="rfvfpSummary" ControlToValidate="txt_summary" Display="Dynamic" ForeColor="" ValidationGroup="valReplyValidationGroup" runat="server" Text="Please supply a Summary to your reply." ErrorMessage="Please supply a Summary to your reply."></asp:RequiredFieldValidator>--%>
                                        <asp:TextBox ID="txt_comment" CssClass="textbox" runat="server" Height="200px" Columns="25" Text='<%# Bind("fpBody") %>'></asp:TextBox>

   <%--                                     <asp:RequiredFieldValidator CssClass="error" ID="rfvfpComment" ControlToValidate="txt_comment" Display="Dynamic" ForeColor="" ValidationGroup="valReplyValidationGroup" runat="server" Text="Please supply your Comments." ErrorMessage="Please supply your Comments."></asp:RequiredFieldValidator>--%>
                                        <asp:Button ID="but_submit" CssClass="button" runat="server" Text="Submit" PostBackUrl="#" CausesValidation="True" CommandName="Insert" ValidationGroup="valReplyValidationGroup"></asp:Button>

                                <asp:Label ID="lblDateTime" runat="server" Text='<%# Bind("fpDatePosted") %>' ></asp:Label>

                <asp:ObjectDataSource ID="odsReply" runat="server"
                    InsertMethod="forum_INSERT_REPLY" TypeName="dotnet.BLL.bllForum">
                        <asp:Parameter Name="fpSummary" Type="String" />
                        <asp:Parameter Name="fpUsername" Type="String" DefaultValue="TIMMY" />
                        <asp:Parameter Name="fpBody" Type="String" />
                        <asp:Parameter Name="fpDatePosted" Type="DateTime" />
                        <asp:Parameter Name="fpTopicId" Type="Int32" DefaultValue="1" />
	                    <asp:Parameter Name="fpLive" Type="Boolean" DefaultValue="True" />

NOW… as you can see its very simple, just wondering how I go about passing in the current Date and Time (ie.1/1/1753 12:00:00 AM) - its going into a MS SQL Server database btw.

I thought I should create a label (named lblDateTime) Bind it to the parameter / field and create in the partial class behind in the
dtvReply_ItemInserting sub the code to pass in todays date…

like so…

    Protected Sub dtvReply_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dtvReply.ItemInserting

        Dim lblDateTime As Label

        lblDateTime = CType(dtvReply.FindControl("lblDateTime"), Label)

        lblDateTime.Text = Date.UtcNow

    End Sub

(note i have tired Date.Now as well as Date.UtcNow, I tired Utc as I read somewhere that it includes the time with the date - which MS SQL requires.)

Anyway I get the following error…

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

Can anyone set me right?



You could just set the default value of your DateTime parameter to DateTime.Now:

<asp:Parameter Name="fpDatePosted" Type="DateTime" DefaultValue='<&#37;# DateTime.Now %>' />

Transferring dates out to strings and back is best avoided if at all possible.