?
Solved

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

Posted on 2010-11-29
4
Medium Priority
?
765 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

762 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