set value of date field in an asp:details view control

Hello,
I have a page built using Visual Studio 2005 on C# with a sql 2008 backend.
In one page I have a detailsview boundfield that I would like to display the current date and time.
My code follows:
---------
<table style="width:99%;" cellpadding="2" cellspacing="0" border="0">
 
   <tr>
  <td  valign="top" align="right" style="width: 3px">&nbsp;</td>
    <td  valign="top" align="left"> &nbsp;
        <asp:DetailsView ID="dv_pstamessage" runat="server" AutoGenerateRows="False" CellPadding="4" DataKeyNames="ID" DataSourceID="ds_postamessage" Font-Names="Verdana" Font-Size="Small" ForeColor="#333333" GridLines="None" Height="50px" Width="400px" DefaultMode="Insert" >
       
            <FooterStyle BackColor="#082AA6" Font-Bold="True" ForeColor="White" />
            <CommandRowStyle BackColor="White" Font-Bold="True" />
            <RowStyle BackColor="#EFF3FB" />
            <FieldHeaderStyle BackColor="White" Font-Bold="True" />
            <PagerStyle BackColor="#082AA6" ForeColor="White" HorizontalAlign="Center" />
           
            <Fields>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />
               
                <asp:BoundField DataField="DatePosted" HeaderText="Date Posted" SortExpression="DatePosted"  DataFormatString="{0:mm/dd/yyyy}"  >
                    <ControlStyle Width="250px" />
                    <ItemStyle Width="250px" HorizontalAlign=Left VerticalAlign=Top />
                </asp:BoundField>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" >
                    <ControlStyle Width="250px" />
                    <ItemStyle Width="250px" />
                </asp:BoundField>
                <asp:BoundField DataField="Subject" HeaderText="Subject" NullDisplayText="Subject"
                    SortExpression="Subject">
                    <ItemStyle Width="250px" />
                    <ControlStyle Width="250px" />
                </asp:BoundField>
                <asp:BoundField DataField="Message" HeaderText="Message" SortExpression="Message" NullDisplayText="Your Message Here" >
                    <ControlStyle Height="75px" Width="250px" />
                    <HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" />
                    <ItemStyle VerticalAlign="Top" Width="250px" HorizontalAlign=Left Wrap=False  />
                    <FooterStyle HorizontalAlign="Right" VerticalAlign="Bottom" />
                </asp:BoundField>
                <asp:CommandField InsertText="Submit" ShowInsertButton="True" ButtonType=Button >
                    <HeaderStyle HorizontalAlign="Right" VerticalAlign="Top" />
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
                </asp:CommandField>
               
            </Fields>
            <HeaderStyle BackColor="White" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:DetailsView>
       
        <asp:SqlDataSource ID="ds_postamessage" runat="server" ConnectionString="<%$ ConnectionStrings:HGAC_PRODConnectionString1 %>"
             OnInserted = "ds_postamessage_inserted"
           
            InsertCommand="INSERT INTO [tblDisaster] ([DatePosted], [Subject], [Message], [Name]) VALUES (@DatePosted, @Subject, @Message, @Name)"
           
            OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [ID], [DatePosted], [Subject], [Message], [Name] FROM [tblDisaster] ORDER BY [DatePosted] DESC"
            UpdateCommand="UPDATE [tblDisaster] SET [DatePosted] = @DatePosted, [Subject] = @Subject, [Message] = @Message, [Name] = @Name WHERE [ID] = @original_ID AND (([DatePosted] = @original_DatePosted) OR ([DatePosted] IS NULL AND @original_DatePosted IS NULL)) AND (([Subject] = @original_Subject) OR ([Subject] IS NULL AND @original_Subject IS NULL)) AND (([Message] = @original_Message) OR ([Message] IS NULL AND @original_Message IS NULL)) AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL))">
           
           
         
            <InsertParameters>
                <asp:Parameter Name="DatePosted" Type="DateTime" />
                <asp:Parameter Name="Subject" Type="String" />                
                <asp:Parameter Name="Message" Type="String" />
                <asp:Parameter Name="Name" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
    </td>
 
  </tr>
   <tr>
  <td colspan="2" valign="top" align="center"><img src="images/spacer.gif" width="925";  height="1"  /> </td>
  </tr>
  <tr>

    <td  colspan="2" valign="top" align="left" style="height: 20px">
   
    <asp:GridView ID="gv_postamsg" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" DataSourceID="ds_postamsg" Font-Names="Verdana" Font-Size="Small" ForeColor="#333333" GridLines="None" Width="100%"  >

            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />
                <asp:BoundField DataField="DatePosted" HeaderText="Date Posted" SortExpression="DatePosted" >
                    <ControlStyle Width="250px" />
                </asp:BoundField>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                 
                <asp:BoundField DataField="Subject" HeaderText="Subject" SortExpression="Subject" />
                <asp:BoundField DataField="Message" HeaderText="Message" SortExpression="Message" />
             
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="ds_postamsg" runat="server" ConnectionString="<%$ ConnectionStrings:HGAC_PRODConnectionString1 %>"
            DeleteCommand="DELETE FROM [tblDisaster] WHERE [ID] = @ID" InsertCommand="INSERT INTO [tblDisaster] ([DatePosted], [Subject], [Message], [Name]) VALUES (@DatePosted, @Subject, @Message, @Name)"
            SelectCommand="SELECT [ID], [DatePosted], [Subject], [Message], [Name] FROM [tblDisaster] ORDER BY [DatePosted] DESC"
            UpdateCommand="UPDATE [tblDisaster] SET [DatePosted] = @DatePosted, [Subject] = @Subject, [Message] = @Message, [Name] = @Name WHERE [ID] = @ID">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="DatePosted" Type="DateTime" />
                <asp:Parameter Name="Subject" Type="String" />
                <asp:Parameter Name="Message" Type="String" />
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="DatePosted" Type="DateTime" />
                <asp:Parameter Name="Subject" Type="String" />
                <asp:Parameter Name="Message" Type="String" />
                <asp:Parameter Name="Name" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
    </td>
 
  </tr>
  </table>
---------------------
a working example: http://ntis12:99/DRTEST/post_msg.aspx 
How can I display the current date and time on this detailsview "DatePosted" bound field?
Perhaps there is a way to set this field in the SQL DataSource call..

ANY help would be greatly appreciated.  
Codeaddict7423Asked:
Who is Participating?
 
jagssiduralaCommented:
Get one more column called "currentdate" using "GetDate()(predefined function in SQLserver)" and assign this to your bound filed.

Select     GetDate() as currentdate,
                 .
                 .
                 .
                 .
                 .
From    your tablename........................
0
 
Codeaddict7423Author Commented:
rkworlds,
Thank you for the suggested reading. I implemented the suggested solution as follows:
Codebehind:
---------
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (dv_pstamessage.CurrentMode == DetailsViewMode.Insert)
            {
                TextBox tbdate = (TextBox)dv_pstamessage.Rows[0].FindControl("DatePosted");
                tbdate.Text = DateTime.Now.ToString();
            }
                    //Call FillGridView() method to populate the PostMsg GridView.
            FillGridview();
        }
    }
---------
My aspx code is as follows:
---------
 <asp:DetailsView ID="dv_pstamessage" runat="server" AutoGenerateRows="False" CellPadding="4" DataKeyNames="ID" DataSourceID="ds_postamessage" Font-Names="Verdana" Font-Size="Small" ForeColor="#333333" GridLines="None" Height="50px" Width="400px" DefaultMode="Insert" >
       
            <FooterStyle BackColor="#082AA6" Font-Bold="True" ForeColor="White" />
            <CommandRowStyle BackColor="White" Font-Bold="True" />
            <RowStyle BackColor="#EFF3FB" />
            <FieldHeaderStyle BackColor="White" Font-Bold="True" />
            <PagerStyle BackColor="#082AA6" ForeColor="White" HorizontalAlign="Center" />
           
            <Fields>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />
               
                <asp:BoundField DataField="DatePosted" HeaderText="Date Posted" SortExpression="DatePosted"  DataFormatString="{0:mm/dd/yyyy}"  >
               
                    <ControlStyle Width="250px" />
                    <ItemStyle Width="250px" HorizontalAlign=Left VerticalAlign=Top />
                </asp:BoundField>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" >
                    <ControlStyle Width="250px" />
                    <ItemStyle Width="250px" />
                </asp:BoundField>
                <asp:BoundField DataField="Subject" HeaderText="Subject" NullDisplayText="Subject"
                    SortExpression="Subject">
                    <ItemStyle Width="250px" />
                    <ControlStyle Width="250px" />
                </asp:BoundField>
                <asp:BoundField DataField="Message" HeaderText="Message" SortExpression="Message" NullDisplayText="Your Message Here" >
                    <ControlStyle Height="75px" Width="250px"  />
                    <HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Wrap=True />
                    <ItemStyle VerticalAlign="Top" Width="250px" HorizontalAlign=Left Wrap="True" Height=75px   />
                    <FooterStyle HorizontalAlign="Right" VerticalAlign="Bottom" />
                </asp:BoundField>
                <asp:CommandField InsertText="Submit" ShowInsertButton="True" ButtonType=Button >
                    <HeaderStyle HorizontalAlign="Right" VerticalAlign="Top" />
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
                </asp:CommandField>
               
            </Fields>
            <HeaderStyle BackColor="White" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:DetailsView>
       
        <asp:SqlDataSource ID="ds_postamessage" runat="server" ConnectionString="<%$ ConnectionStrings:HGAC_PRODConnectionString1 %>"
             OnInserted = "ds_postamessage_inserted"
           
            InsertCommand="INSERT INTO [tblDisaster] ([DatePosted], [Subject], [Message], [Name]) VALUES (@DatePosted, @Subject, @Message, @Name)"
           
            OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [ID], [DatePosted], [Subject], [Message], [Name] FROM [tblDisaster] ORDER BY [DatePosted] DESC"
            UpdateCommand="UPDATE [tblDisaster] SET [DatePosted] = @DatePosted, [Subject] = @Subject, [Message] = @Message, [Name] = @Name WHERE [ID] = @original_ID AND (([DatePosted] = @original_DatePosted) OR ([DatePosted] IS NULL AND @original_DatePosted IS NULL)) AND (([Subject] = @original_Subject) OR ([Subject] IS NULL AND @original_Subject IS NULL)) AND (([Message] = @original_Message) OR ([Message] IS NULL AND @original_Message IS NULL)) AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL))">
           
           
         
            <InsertParameters>
                <asp:Parameter Name="DatePosted" Type="DateTime" />
                <asp:Parameter Name="Subject" Type="String" />                
                <asp:Parameter Name="Message" Type="String" />
                <asp:Parameter Name="Name" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
-------

I am gettint the folllowing compilation error:
------
Object reference not set to an instance of an object.
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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 33:             {
Line 34:                 TextBox tbdate = (TextBox)dv_pstamessage.Rows[0].FindControl("DatePosted");
Line 35:                 tbdate.Text = DateTime.Now.ToString();
Line 36:             }
----------
 
Line 35 seems to be throwing an error.   Can you please review?
0
 
Codeaddict7423Author Commented:
rkworlds,
I found a solution that seems to be working and I'm posting it in case it can be of  use to others.
Codebehind:
----------
    if (dv_pstamessage.CurrentMode == DetailsViewMode.Insert)
            {
                TextBox tbdate = (TextBox)dv_pstamessage.Rows[0].FindControl("DatePosted");
                tbdate.Text = DateTime.Now.ToString();
            }
----------
details view code
---------
 <asp:DetailsView ID="dv_pstamessage" runat="server" AutoGenerateRows="False" CellPadding="4" DataKeyNames="ID" DataSourceID="ds_postamessage" Font-Names="Verdana" Font-Size="Small" ForeColor="#333333" GridLines="None" Height="50px" Width="400px" DefaultMode="Insert" >
       
            <FooterStyle BackColor="#082AA6" Font-Bold="True" ForeColor="White" />
            <CommandRowStyle BackColor="White" Font-Bold="True" />
            <RowStyle BackColor="#EFF3FB" />
            <FieldHeaderStyle BackColor="White" Font-Bold="True" />
            <PagerStyle BackColor="#082AA6" ForeColor="White" HorizontalAlign="Center" />
           
            <Fields>
           
           
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />
               
                <asp:TemplateField HeaderText="Date Posted" >
                <InsertItemTemplate>
                <asp:TextBox ID="DatePosted" runat="server" Width="250" Text='<%# Bind("DatePosted") %>'></asp:TextBox>
                </InsertItemTemplate>
                </asp:TemplateField>
               
                             
               
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" >
                    <ControlStyle Width="250px" />
                    <ItemStyle Width="250px" />
                </asp:BoundField>
                <asp:BoundField DataField="Subject" HeaderText="Subject" NullDisplayText="Subject"
                    SortExpression="Subject">
                    <ItemStyle Width="250px" />
                    <ControlStyle Width="250px" />
                </asp:BoundField>
                <asp:BoundField DataField="Message" HeaderText="Message" SortExpression="Message" NullDisplayText="Your Message Here" >
                    <ControlStyle Height="75px" Width="250px"  />
                    <HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Wrap=True />
                    <ItemStyle VerticalAlign="Top" Width="250px" HorizontalAlign=Left Wrap="True" Height=75px   />
                    <FooterStyle HorizontalAlign="Right" VerticalAlign="Bottom" />
                </asp:BoundField>
                <asp:CommandField InsertText="Submit" ShowInsertButton="True" ButtonType=Button >
                    <HeaderStyle HorizontalAlign="Right" VerticalAlign="Top" />
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
                </asp:CommandField>
               
            </Fields>
            <HeaderStyle BackColor="White" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:DetailsView>
--------
Thank you for the suggested reading, it put me on the right track.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.