Solved

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

Posted on 2010-11-29
4
729 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now