Solved

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

Posted on 2010-11-29
4
759 Views
Last Modified: 2012-05-10
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.  
0
Comment
Question by:Codeaddict7423
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:jagssidurala
ID: 34235944
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
 
LVL 29

Accepted Solution

by:
Kumaraswamy R earned 500 total points
ID: 34238209
0
 

Author Comment

by:Codeaddict7423
ID: 34238690
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
 

Author Comment

by:Codeaddict7423
ID: 34239129
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question