[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2654
  • Last Modified:

How do I use the EmptyDataTemplate of the asp.net 3.5 listview control?

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 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
  • 9
  • 8
1 Solution
 
Salim FayadCommented:
Here is why you are receiving this error. The "query" is null. The "query.FirstOrDefault()" is returning null. So, query.FirstOrDefault().TrustID will return an error. You must check whether query.FirstOrDefault() is null or not.
0
 
ShepweddAuthor Commented:
Could you supply me with some C# sample code for this check? And if the value is null will it be possible to catch the error and supply a prompt window to the user stating a message?
0
 
Salim FayadCommented:
Replace lines 18 to 22 by the following:

if(query.FirstOrDefault() != null)
{
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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
ShepweddAuthor Commented:
Hi,

I no longer get the error but my EmptyDataTemplate contents still doesn't kick in?

Thanks.
0
 
Salim FayadCommented:
What you posted is the function lvAccounts_ItemCreated which should occur OnItemCreated event which is the event after an item in the lvAccounts has been created. This means that an item has been added to the lvAccounts. Can you show me the code where you are binding to the lvAccounts?
0
 
ShepweddAuthor Commented:
Attached is my listview declaration and below that is my linqdatasource which my listview is bound to...

Thanks.
<asp:ListView 
                                        ID="lvAccounts" 
                                        DataSourceID="LinqDataSourceAccounts"
                                        runat="server"
                                        DataKeyNames="AccountID"
                                        DataMember="DefaultView" 
                                        InsertItemPosition="FirstItem"
                                        OnItemInserting="lvAccounts_ItemInserting"
                                        OnItemDataBound="lvAccounts_ItemDataBound" 
                                        OnPreRender="lvAccounts_PreRender" 
                                        OnItemCreated="lvAccounts_ItemCreated" 
                                        OnItemUpdating="lvAccounts_ItemUpdating" 
                                        OnItemEditing="lvAccounts_ItemEditing">
 
******************************************************
 
<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
 
Salim FayadCommented:
Why are you using all of the events of the listview? The LinqDataSource's job is to handle almost all of them.

It would be better to show the code of the EmptyDataTemplate.
0
 
ShepweddAuthor Commented:
I'm only actually using OnItemInserting, OnItemUpdating and OnItemCreated events for certain items that need to be declared at these points. I have the other events referenced for testing purposes only. Attached is my EmptyDataTemplate...
<EmptyDataTemplate>
                                                    <table id="item" runat="server" style="">
                                                        <tr>
                                                            <td>No data was returned.</td>
                                                        </tr>
                                                    </table>
                                                </EmptyDataTemplate>

Open in new window

0
 
Salim FayadCommented:
Try after at the end of each event to put:

lvAccounts.DataBind();

Open in new window

0
 
ShepweddAuthor Commented:
I have bound the list at the end of each event, this makes no difference.
0
 
Salim FayadCommented:
Put a breakpoint on the lvAccounts.DataBind() and when it reaches it, check how many items does the lvAccounts has.

Also, can you put the code of the function "lvAccounts_ItemDataBound"
0
 
ShepweddAuthor Commented:
Now I seem to get a "StackOverflowException was unhandled occured in mscorlib.dll" error when attempting to debug my application?

Attached is my ItemDataBound code...

This event bascially supplies my edit template with a value that is needed only when a typecode is equal to "Executry".
protected void lvAccounts_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        TrustSystemMARTINSPALDINGDataContext db = new TrustSystemMARTINSPALDINGDataContext();
 
        var query = from tt in db.Trusts
                    join ttl in db.TypeLookups on tt.TypeCode equals ttl.TypeCode
                    where tt.TrustID == Convert.ToInt32(Request.QueryString["TrustID"])
                    select new { tt.TrustID, ttl.TypeDescription };
 
        if (lvAccounts.EditIndex >= 0)
        {
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
 
            if (dataItem.DisplayIndex == lvAccounts.EditIndex)
            {
                TextBox tbTypeCode = (TextBox)dataItem.FindControl("TypeEditTextBox");
                tbTypeCode.Text = query.FirstOrDefault().TypeDescription;
                
                DropDownList TrustAccounteeEditDDL = (DropDownList)dataItem.FindControl("TrustAccounteeEditDDL");
 
                if (tbTypeCode.Text != "Executry")
                {
                    TrustAccounteeEditDDL.Enabled = false;
                }
                else
                {
                    TrustAccounteeEditDDL.Enabled = true;
                }
            }
        }
 
        lvAccounts.DataBind();
    }

Open in new window

0
 
ShepweddAuthor Commented:
Actually that error has now disappeared, I think it was due to binding my list within the selecting event of my listviews linqdatasource. Now when I attempt to debug I application just hangs? For reference my event in question now looks like the attached...
protected void lvAccounts_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        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");
        TextBox tbTypeInsert = (TextBox)lvAccounts.InsertItem.FindControl("TypeInsertTextBox");
        DropDownList ddlTrustAccounteeInsert = (DropDownList)lvAccounts.InsertItem.FindControl("TrustAccounteeInsertDDL");
 
        TrustSystemMARTINSPALDINGDataContext db = new TrustSystemMARTINSPALDINGDataContext();
 
        var query = from tt in db.Trusts
                    join tpl in db.PartnerLookups on tt.PartnerCode equals tpl.PartnerCode
                    join tfel in db.FeeExecLookups on tt.FeeExecCode equals tfel.FeeExecCode
                    join ttl in db.TypeLookups on tt.TypeCode equals ttl.TypeCode
                    where tt.TrustID == Convert.ToInt32(Request.QueryString["TrustID"])
                    select new { tt.TrustID, tt.ClientName, tt.MatterNo, tpl.PartnerDescription, tfel.FeeExecDescription, ttl.TypeDescription };
 
        //Code example 1: trying to get my EmptyDataTemplate to kick in
        if (query.FirstOrDefault() != null)
        {
            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;
            tbTypeInsert.Text = query.FirstOrDefault().TypeDescription;
        }
 
        if (tbTypeInsert.Text != "Executry")
        {
            ddlTrustAccounteeInsert.SelectedValue = null;
            ddlTrustAccounteeInsert.Enabled = false;
        }
 
        lvAccounts.DataBind();
    }

Open in new window

0
 
ShepweddAuthor Commented:
When I step through my code on an item that should have null attached to it (where the emptydatatemplate should then kick in) my if statement != null code seems to run?
0
 
ShepweddAuthor Commented:
....and the my page only seems to hang when I include "lvAccounts.DataBind();"
0
 
Salim FayadCommented:
You have to remove the lvAccounts.DataBind() from the lvAccounts_ItemDataBound. This is causing an infinite loop since the ItemDataBound is called after the DataBind of the lvAccounts. So, the lvAccounts.DataBind() is callaing again the lvAccounts_ItemDataBound
0
 
Salim FayadCommented:
Is the EmptyDataTemplate  working now?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now