1

I am writing an asp.net program for creating reservations in which I have a listview control connected to an SQL database. The user enters most of the information to be sent to the database (name, phone-number, etc..) however there is some info (the current date/time for example) that I would like to be automatically populated before being added to the db. I would like to set this up in the code-behind (C#), but I cannot seem to figure out the proper way to do it. Any thoughts would be greatly appreciated!

Here is my current code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SummaryForm.ascx.cs" Inherits="RamRideOps.Controls.SummaryForm" %>

<asp:EntityDataSource ID="Rides_EDS" runat="server" 
ConnectionString="name=RamRideOpsEntities" 
DefaultContainerName="RamRideOpsEntities" EnableFlattening="False" 
EntitySetName="Rides" EntityTypeFilter="Ride" EnableDelete="True" 
EnableInsert="True" EnableUpdate="True">
</asp:EntityDataSource>

<asp:ListView ID="SummaryLV" runat="server" DataKeyNames="TimeOfCall" 
DataSourceID="Rides_EDS" InsertItemPosition="LastItem" 
onselectedindexchanged="ListView1_SelectedIndexChanged">
<AlternatingItemTemplate>
    <tr style="background-color:#FFF8DC;">           
        <td>
            <asp:Label ID="TimeOfCallLabel" runat="server" 
                Text='<%# Eval("TimeOfCall") %>' />
        </td>           
        <td>
            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
        </td>
        <td>
            <asp:Label ID="PhoneLabel" runat="server" Text='<%# Eval("Phone") %>' />
        </td>
        <td>
            <asp:Label ID="NumPatronsLabel" runat="server" 
                Text='<%# Eval("NumPatrons") %>' />
        </td>
        <td>
            <asp:Label ID="PickupAddressLabel" runat="server" 
                Text='<%# Eval("PickupAddress") %>' />
        </td>            
        <td>
            <asp:Label ID="DropoffAddressLabel" runat="server" 
                Text='<%# Eval("DropoffAddress") %>' />
        </td>
        <td>
            <asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>' />
        </td>
        <td>
            <asp:Label ID="AssignedCarLabel" runat="server" 
                Text='<%# Eval("AssignedCar") %>' />
        </td>
        <td>
            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delt" />                
        </td>
    </tr>
</AlternatingItemTemplate>
<EditItemTemplate>
    <tr style="background-color:#008A8C;color: #FFFFFF;">
        <td>
            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
        </td>
        <td>
            <asp:TextBox ID="PhoneTextBox" runat="server" Text='<%# Bind("Phone") %>' />
        </td>
        <td>
            <asp:TextBox ID="NumPatronsTextBox" runat="server" 
                Text='<%# Bind("NumPatrons") %>' />
        </td>
        <td>
            <asp:TextBox ID="PickupAddressTextBox" runat="server" 
                Text='<%# Bind("PickupAddress") %>' />
        </td>            
        <td>
            <asp:TextBox ID="DropoffAddressTextBox" runat="server" 
                Text='<%# Bind("DropoffAddress") %>' />
        </td>
        <td>
            <asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
        </td>
        <td>
            <asp:TextBox ID="AssignedCarTextBox" runat="server" 
                Text='<%# Bind("AssignedCar") %>' />
        </td>
        <td>
            <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                Text="Update" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                Text="Cancel" />
        </td>
    </tr>
</EditItemTemplate>
<EmptyDataTemplate>
    <table runat="server" 
        style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
        <tr runat="server">
            <td runat="server">
                There are currently no scheduled rides!</td>
        </tr>
    </table>
</EmptyDataTemplate>
<InsertItemTemplate>
    <tr style="">
        <td>
            <asp:TextBox ID="TimeOfCallTextBox" runat="server" 
                Text='<%# Bind("TimeOfCall") %>' Visible="False" />
        </td>
        <td>
            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
        </td>
        <td>
            <asp:TextBox ID="PhoneTextBox" runat="server" Text='<%# Bind("Phone") %>' />
        </td>
        <td>
            <asp:TextBox ID="NumPatronsTextBox" runat="server" 
                Text='<%# Bind("NumPatrons") %>' />
        </td>
        <td>
            <asp:TextBox ID="PickupAddressTextBox" runat="server" 
                Text='<%# Bind("PickupAddress") %>' />
        </td>
        <td>
            <asp:TextBox ID="DropoffAddressTextBox" runat="server" 
                Text='<%# Bind("DropoffAddress") %>' />
        </td>
        <td>
            <asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' Visible="True" />
        </td>
        <td>
            <asp:TextBox ID="AssignedCarTextBox" runat="server" 
                Text='<%# Bind("AssignedCar") %>' />
        </td>
        <td>
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                Text="Add" OnClientClick="addButton_Click" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                Text="Clear" />
        </td>
    </tr>
</InsertItemTemplate>
<ItemTemplate>
    <tr style="background-color:#DCDCDC;color: #000000;">     
        <td>
            <asp:Label ID="TimeOfCallLabel" runat="server" 
                Text='<%# Eval("TimeOfCall") %>' />
        </td>           
        <td>
            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
        </td>
        <td>
            <asp:Label ID="PhoneLabel" runat="server" Text='<%# Eval("Phone") %>' />
        </td>
        <td>
            <asp:Label ID="NumPatronsLabel" runat="server" 
                Text='<%# Eval("NumPatrons") %>' />
        </td>
        <td>
            <asp:Label ID="PickupAddressLabel" runat="server" 
                Text='<%# Eval("PickupAddress") %>' />
        </td>            
        <td>
            <asp:Label ID="DropoffAddressLabel" runat="server" 
                Text='<%# Eval("DropoffAddress") %>' />
        </td>
        <td>
            <asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>' />
        </td>
        <td>
            <asp:Label ID="AssignedCarLabel" runat="server" 
                Text='<%# Eval("AssignedCar") %>' />
        </td>
        <td>
            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delt" />                
        </td>
    </tr>
</ItemTemplate>

<LayoutTemplate>
    <table runat="server">
        <tr runat="server">
            <td runat="server">
                <table ID="itemPlaceholderContainer" runat="server" border="1" 
                    style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                    <tr runat="server" style="background-color:#DCDCDC;color: #000000;">
                        <th runat="server" style="width:auto">
                            Call Time</th>                            
                        <th runat="server" style="width:auto">
                            Name</th>
                        <th runat="server" style="width:5px">
                            Phone</th>
                        <th runat="server" style="width:auto">
                            Size</th>
                        <th runat="server" style="width:auto">
                            Pick-Up</th>
                        <th runat="server" style="width:auto">
                            Drop-Off</th>
                        <th runat="server" style="width:auto">
                            Status</th>
                        <th runat="server" style="width:auto">
                            Car</th>
                        <th id="Th1" runat="server">
                            </th>
                    </tr>
                    <tr ID="itemPlaceholder" runat="server">
                    </tr>
                </table>
            </td>
        </tr>
        <tr runat="server">
            <td runat="server" 
                style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
                <asp:DataPager ID="DataPager1" runat="server">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                            ShowLastPageButton="True" />
                    </Fields>
                </asp:DataPager>
            </td>
        </tr>
    </table>
</LayoutTemplate>

2
  • Can you show the code you're currently using to perform the database insert? There are multiple ways to do it, and a helpful answer will depend on what you're doing. Commented Jan 11, 2012 at 20:27
  • Sure... code added to my post. Commented Jan 11, 2012 at 20:41

2 Answers 2

2

Use the EntityDataSource.Inserting or EntityDataSource.Updating event to access the object you are saving.

You can access the entity property to change the values in your object like this:

// E.g. Inserting
protected void Rides_EDS_DataSource_Inserting(object sender, EntityDataSourceChangingEventArgs e)
{
    var rides = (e.Entity as Rides);
    rides.Date = DateTime.Now;
    rides.Total = Fee + Tax;
    ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

Votes up. I do something very similar with SqlDataSource to set column values that can't be done as calculated columns/triggers etc.
Alright, this is looking like what I need! But what event in the listview triggers this method? I think I have the code-behind syntax correct but the method never gets called. Do I have to set a value in the .aspx page to call this?
The CommandName properties for the Insert/Update buttons in the respective ItemTemplates are there so they should trigger the eventhandler when the object is persisted by the datasource. Make sure you have set the OnUpdating & OnInserting properties on the datasource. Try going to the designer, click the datasource and view the events and double click the Inserting and Updating events to wire the eventhandler automatically.
w00t, got it! just had to add the OnInserting="Rides_EDS_Inserting" attribute to the entity data source. Thanks for all the help, you guys are awesome!
1

Why not just have the fields you want to auto populate (like datetime in your example) be set in the database? (I think SQL Server has a GetDate function that returns the current datetime you could use with a default column value)

5 Comments

Thanks for the auto-populate suggestion, that may indeed work for the date/time, however I will also be adding some fields that will involve some calculation so I still need to figure out how to set values from the code-behind.
Check this site out - I bet you could probably find the answer: maconstateit.net/tutorials
Ok, so I got the getdate() fnc to insert the date, however it changes every entry in that column to the current date/time each time I insert a new row. Any idea how to make it set the current date/time for that cell and then leave it alone?
Are you allowing nulls in the column? They may have what looks like the current date/time because the values were all filled in at once.
Ah, my bad. I think you'll want to use a trigger and update that field on insert/update. See msdn.microsoft.com/en-us/library/aa258254%28v=sql.80%29.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.