Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2006, 10:50 AM
Authorized User
 
Join Date: Oct 2006
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
Default Binding a date field in InsertItemTemplate fails

I'm using a GridView/DetailsView combo to view, edit and insert records. It appears to work for most fields, but I can't get past a problem on a value that is a date. My access db has the field set as DateTime, and the input is coming thru a simple textbox. I can view fields that already exist, and I can change dates quite easily, but every time I try to insert a new record, I get a null date being passed.

Is this not supported for dates? What am I doing wrong? (I've really trimmed the code back in the following example to make it easier to debug.)

 <asp:DetailsView AutoGenerateRows="False"
  ID="DetailsView1" runat="server"
  EnableViewState="True"
  CssClass="admintable"
  Width="350"
  Cellpadding="3"
  Cellspacing="0"
  Font-Name="Arial, Helvetica, sans-serif"
  Font-Size="12px"
  DataKeyNames="Rec_ID" DataSourceID="AccessDataSourceDetails"
        HeaderText="Event Details"
  OnItemInserting="DetailsView1_ItemInserting"
  OnItemUpdated="DetailsView1_ItemUpdated"
  OnItemInserted="DetailsView1_ItemInserted"
  >
 <HeaderStyle BackColor="#dddddd"
  Font-Bold="True"
  Font-Size="11"
  HorizontalAlign="Center" />
 <FooterStyle BackColor="#666666" Font-Bold="True" HorizontalAlign="Center" />
        <Fields>

  <%-- Event Name --%>
   <asp:TemplateField HeaderText="Name">
       <ItemTemplate>
     <asp:Label ID="EventName" Runat="Server" Text='<%# Eval("Name") %>' />
     </ItemTemplate>
    <EditItemTemplate>
     <asp:textbox id="EventName" runat="server"
      TextMode="SingleLine" Columns="35" text='<%#Bind("Name")%>' />
        </EditItemTemplate>
       <InsertItemTemplate>
     <asp:textbox id="InsertEventName" runat="server"
      TextMode="SingleLine" Columns="35" text='<%#Bind("Name")%>' />
     <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
      runat="server" ControlToValidate="InsertEventName"
      ErrorMessage="Event Name is missing">*
      </asp:RequiredFieldValidator>
     </InsertItemTemplate>
     </asp:TemplateField>

   <%-- Event Date --%>
   <asp:TemplateField HeaderText="Event Date">
       <ItemTemplate>
          <asp:Label ID="EventDateLabel" Runat="Server"
                   Text='<%# Eval("Date1", "{0:d}") %>' />
        </ItemTemplate>
       <InsertItemTemplate>
     <asp:textbox id="InsertEventDate" runat="server"
      TextMode="SingleLine" Columns="10" Text='<%# Bind("Date1", "{0:d}") %>' />
     <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
      runat="server" ControlToValidate="InsertEventDate"
      ErrorMessage="Event Date is missing">*
      </asp:RequiredFieldValidator>
       <asp:CustomValidator id="CustomValidator1" runat="server"
      ControlToValidate="InsertEventDate"
      OnServerValidate="ServerValidateED"
      Display="Static"
      ErrorMessage="Event Date is invalid">*
     </asp:CustomValidator>
     </InsertItemTemplate>

       <EditItemTemplate>
     <asp:textbox id="EditEventDate" runat="server"
      TextMode="SingleLine" Columns="10" Text='<%# Bind("Date1", "{0:d}") %>' />
     <asp:RequiredFieldValidator ID="RequiredFieldValidator3"
      runat="server" ControlToValidate="EditEventDate"
      ErrorMessage="Event Date is missing">*
      </asp:RequiredFieldValidator>
       <asp:CustomValidator id="CustomValidator2" runat="server"
      ControlToValidate="EditEventDate"
      OnServerValidate="ServerValidateED"
      Display="Static"
      ErrorMessage="Event Date is invalid">*
     </asp:CustomValidator>
     </EditItemTemplate>
      </asp:TemplateField>


              <asp:CommandField ShowEditButton="True" ShowInsertButton="True" ShowDeleteButton="True" />
            </Fields>
          </asp:DetailsView>

          <asp:AccessDataSource ID="AccessDataSourceDetails" runat="server"
         datafile="~/App_Data/WVEvents.mdb"
            SelectCommand="SELECT * FROM [EventHH] WHERE ([Rec_id] = @Rec_id)"
            UpdateCommand="UPDATE EventHH SET Name=@Name, Date1=@Date1
    WHERE Rec_ID = @Rec_ID"
            InsertCommand="INSERT INTO EventHH (Name,TrueEvent,Date1) VALUES (@Name,True,@Date1)"
    >
            <SelectParameters>
             <asp:ControlParameter ControlID="GridView1" Name="Rec_id" PropertyName="SelectedValue"
                 Type="String" />
             </SelectParameters>
            <UpdateParameters>
             <asp:Parameter Name="Name" Type="String" />
             <asp:Parameter Name="Date1" Type="DateTime" />
             </UpdateParameters>
            <InsertParameters>
             <asp:Parameter Name="Name" Type="String" />
              <asp:Parameter Name="TrueEvent" Type="Boolean" />
             <asp:Parameter Name="Date1" Type="DateTime" />
             </InsertParameters>
          </asp:AccessDataSource>









Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating a Date field based on another field arholly Access VBA 6 November 22nd, 2006 11:19 AM
Datagrid fails to sort date column correctly ysiline ASP.NET 1.0 and 1.1 Professional 2 July 11th, 2006 12:09 PM
getingValue of Unbounded Field from a binding cont Raz Muhammad Visual Studio 2005 1 November 29th, 2005 03:12 PM
Crystal Reports Dynamic binding field objects velkropie BOOK: Professional Crystal Reports for VS.NET 0 November 30th, 2004 11:35 PM
DTS Import ( Date string to Date field) gfowajuh SQL Server 2000 1 September 30th, 2003 06:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.