• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 465
  • Last Modified:

How do I use the EmptyDataTemplate attached to a asp.net 3.5 listview?

I have two listview controls on seperate pages. When a user clicks a link from the first listview I am passing a variable through the query string to populate the second listview. In both my listviews I have declared the EmptyDataTemplate so surely when I click the link in my first listview and there is no data attached to the query string variable being passed through (i.e. NULL) to my second listview the EmptyDataTemplate should kick in? Instead I receive the error: "Object reference not set to an instance of an object"? I am using the attached listview ItemCreated event code-behind to prepopulate controls in my second listviews InsertItemTemplate using the passed in query string variable and it is upon the "lbTrustID.Text = query.FirstOrDefault().TrustID.ToString();" line that I receive the "Object reference not set to an instance of an object" error? When there is data attached to the query string variable getting passed through the second listview populates exactly how I want it to, it's just when there's nothing attached that I receive the error, surely when there's no data that's when the EmptyItemTemplate should kick in? Note: My first listview is bound to a table called Trusts and my second to a table called TrustAccounts.
protected void lvAccounts_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        TrustSystemMARTINSPALDINGDataContext db = new TrustSystemMARTINSPALDINGDataContext();
 
        var query = from ta in db.TrustAccounts
                    join tt in db.Trusts on ta.TrustID equals tt.TrustID
                    join plt in db.PartnerLookups on tt.PartnerCode equals plt.PartnerCode
                    join felt in db.FeeExecLookups on tt.FeeExecCode equals felt.FeeExecCode
                    where tt.TrustID == Convert.ToInt32(Request.QueryString["TrustID"])
                    select new { tt.TrustID, tt.ClientName, tt.MatterNo, plt.PartnerDescription, felt.FeeExecDescription};
 
        Label lbTrustID = (Label)lvAccounts.InsertItem.FindControl("TrustIDInsertLabel");
        TextBox tbClientInsert = (TextBox)lvAccounts.InsertItem.FindControl("ClientNameInsertTextBox");
        TextBox tbMatterInsert = (TextBox)lvAccounts.InsertItem.FindControl("MatterNoInsertTextBox");
        TextBox tbPartnerInsert = (TextBox)lvAccounts.InsertItem.FindControl("PartnerInsertTextBox");
        TextBox tbFeeExecInsert = (TextBox)lvAccounts.InsertItem.FindControl("FeeExecInsertTextBox");
 
        lbTrustID.Text = query.FirstOrDefault().TrustID.ToString();
        tbClientInsert.Text = query.FirstOrDefault().ClientName;
        tbMatterInsert.Text = query.FirstOrDefault().MatterNo;
        tbPartnerInsert.Text = query.FirstOrDefault().PartnerDescription;
        tbFeeExecInsert.Text = query.FirstOrDefault().FeeExecDescription;
    }

Open in new window

0
Shepwedd
Asked:
Shepwedd
  • 6
  • 5
1 Solution
 
copyPasteGhostCommented:
try this:
lbTrustID.Text = query.FirstOrDefault() == null ? "" : query.FirstOrDefault().TrustID.ToString();
0
 
ShepweddAuthor Commented:
My EmptyDataTemplate contents still doesn't kick in?
0
 
copyPasteGhostCommented:
I think...if you want the emptydatatemplate to appear you need to bind the results to the list view.

Like this you are just setting the label to the value null. So in theory something is being assigned (all be it null) That's probably what your problem is

Ghost
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ShepweddAuthor Commented:
How would I bind the result to my listview? By using your code I no longer get the Object Reference error by the way. But what I was hoping to do was show the contents of my EmptyItemTemplate when null is attached to my query string variable. If you have some example code that would be great.
0
 
copyPasteGhostCommented:
replace lbTrustID.Text = query.FirstOrDefault() == null ? "" : query.FirstOrDefault().TrustID.ToString();

with this:

if(query.FirstOrDefault() == null ){
lvAccounts.DataSource = new ();
lvAccounts.DataBind();
}
else {
lbTrustID.Text = query.FirstOrDefault().TrustID.ToString();
        tbClientInsert.Text = query.FirstOrDefault().ClientName;
        tbMatterInsert.Text = query.FirstOrDefault().MatterNo;
        tbPartnerInsert.Text = query.FirstOrDefault().PartnerDescription;
        tbFeeExecInsert.Text = query.FirstOrDefault().FeeExecDescription;

}

Note this is untested...the point is to check if the query is null. if it's null set the datasource to an empty object. (You might need to create a new class I'm not sure if you can create an anonymous class with an empty constructor..) I just checked...and you can't. So try my example if not you will need to create a new class...

public class ListViewData {
   public int TrustID { get; set; }
   public string ClientName{ get; set; }
   public int MatterNo{ get; set; }
   public string PartnerDescription{ get; set; }
   public string FeeExecDescription{ get; set; }
}

then you set your lvAccount.DataSource = new ListViewData();

if this is complicated please let me know and I'll try to fix it for you.

Thanks
Ghost
0
 
ShepweddAuthor Commented:
Yip, you're right, I need to create a new class. This I am not sure about though, where exactly would I put your class code? Also, I currently have my listview bound to the attached linqdatasource set to accept a query string variable. My listview is populated through this linqdatasource through the OnSelecting event so I'm not too sure if I will be able to bind my listview to a new datasource? If it's too complicated I suppose I don't really have to use the EmptyDataSource, it would be nice to thats all.
<asp:LinqDataSource 
        ID="LinqDataSourceAccounts" 
        runat="server" 
        ContextTypeName="TrustSystemMARTINSPALDINGDataContext"
        EnableDelete="True" 
        EnableInsert="True" 
        EnableUpdate="True"
        TableName="TrustAccounts" 
        OnSelecting="LinqDataSourceAccounts_Selecting" 
        Where="TrustID = @trustID">
        <WhereParameters>
            <asp:QueryStringParameter 
                Type="Int32" 
                Name="TrustID" 
                QueryStringField="TrustID" />
        </WhereParameters>
    </asp:LinqDataSource>

Open in new window

0
 
copyPasteGhostCommented:
it should not be a problem you can put the class at the end of your code.

try it out :)  the best way to learn cool stuff is to play around with it :)
0
 
ShepweddAuthor Commented:
The EmptyDataTemplate is still not kicking in when null is being passed. It is within my listview's OnItemCreated event that I have placed your code and I have placed the new class code outside of this at the base of my code but within my public partial class.

Thanks.
0
 
copyPasteGhostCommented:
are you remembering to bind your data after you set the lv?

Also don't use null set the lv to this:

lvAccount.DataSource = new ListViewData ();
lvAccount.DataBind();

let me know how that goes.

Ghost
0
 
ShepweddAuthor Commented:
I am binding the data after setting my lv but it still doesn't kick in?

Are you sure this can be done in the ItemCreated event of my listview? Surely with the code being there an item will have to be created before the code will run?
0
 
copyPasteGhostCommented:
why don't you step through the code and find out if the lines are actually being executed.

If they are something else must be going on. I would recommend putting some breakpoints in your code and walk through it to find out what's going on.

Ghost
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now