views:

427

answers:

2

I have a database table TravelRequest that contains, amongst other things, the fields SignOffName and SignOffDate. I have a table adapter TravelRequestTable based on this table. I have a DetailsView control which uses this via an ObjectDataSource. Should be all fairly standard stuff.

What I want to do is add a property called SignOffNameDate to the table adapter that combines the two fields and be able to bind to it in the DetailsView control. I want to do it programmatically rather than adding another column in the SQL because dealing with null values is tricky and depends on some other business rules.

This is what I tried:

public partial class TravelRequestDS
{
    public partial class TravelRequestRow
    {
     public string SignOffNameDate
     {
      get { return CombineNameDate(SignOffName, SignOffDate); }
     }
    }
}

This property works fine when I access it programmatically, but when I try bind to it in my aspx page:

<asp:DetailsView ID="DetailsView_TravelRequest" runat="server" AutoGenerateRows="False"
    DataKeyNames="TravelRequestID" DataSourceID="ObjectDataSource_TravelRequest">
     <Fields>
      <asp:BoundField DataField="SignOffNameDate"
       HeaderText="Sign Off" />
      ...

I get an System.Web.HttpException exception, "A field or property with the name 'SignOffNameDate' was not found on the selected data source."

Any ideas how I can do this? Is there an attribute I need to add to the property?

A: 

If you don't want to change your table structure, change the default method that loads data into your adapter (usually called Fill), most likely you are doing a Select * From [TravelRequest] or selecting the individual fields, so what you could do is change your TableAdapter query select statement to something like

Select [YourCol1],[YourCol2], SignOffNameDate as Null From [TravelRequest]

Modifying the default query, and selecting SignOffNameDate as null will give you access to set this value

RandomNoob
I'm not sure I fully understand your answer. By adding the dummy SignOffNameDate field in the SQL, the table adapter adds a property of that name in the generated code and I get a "The type 'TravelRequestDS.TravelRequestRow' already contains a definition for 'SignOffNameDate'" compiler error.
TallGuy
Is there something else I can do to override the property?
TallGuy
are you still using the class in the original post? You no longer need it.
RandomNoob
I'm afraid I still don't follow you. I need to use my CombineNameDate method to *programmatically* combine the two fields. If I remove the class, how specify the SignOffNameDate property using this method?
TallGuy
ok let me take that back, to make things easy, why don't you select the fields you want to combine via the sql statement or is it something a little more complex than that, so instead of selecting null, could you select the two fields you need via sql and eliminate the need for you combine method?
RandomNoob
For instance Select SignOffNameDate as [Field1] + ' ' + [Field2], I'm guessing a straight concat is wishful thinking or you wouldn't have asked this question in the first place
RandomNoob
nevermind, I think I approached this in the completely wrong way and totally neglected the fact that you're using an objectdatasource, I'll ponder this one again, sorry for the confusion, I kept thinking SqlDataSource
RandomNoob
A: 

If your objective is to display the combined result in a single non-editable field you can do it like this:

<asp:DetailsView ID="DetailsView_TravelRequest" runat="server" AutoGenerateRows="False"
    DataKeyNames="TravelRequestID" DataSourceID="ObjectDataSource_TravelRequest">
        <Fields>
            <asp:TemplateField HeaderText="Sign Off" SortExpression="SignOffDate">               
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("SignOffName") %>'></asp:Label>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("SignOffDate") %>'></asp:Label>
                    </ItemTemplate>
            </asp:TemplateField>  
            ... 
tiktock