0

i have a ListView in the updatePanel

     <ItemTemplate >
            <table id="TestTable" runat="server">
              <tr id="TestRow" runat="server">
                <td id="TestData" runat="server">

                 Name:
                 <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>' />
                  <br />
                  UserID:
                  <asp:Label ID="Label2" runat="server" Text='<%# Eval("UserID") %>' />
                  <br />
                  DateAndTime:
                 <asp:Label ID="Label3" runat="server" Text='<%# Eval("DateAndTime")%>' />

                </td>
               </tr>
              </table>
     </ItemTemplate>

i used the below code to change the Background of the table according to database value

protected void ListViewFlowTable_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if(e.Item.ItemType == ListViewItemType.DataItem)
            {
                ListViewDataItem ItemToDisplay = (ListViewDataItem)e.Item;
                string UserId = DataBinder.Eval(ItemToDisplay.DataItem, "UserID").ToString();
                if(UserId == "1")
                {
                    HtmlTableCell newRow = (HtmlTableCell)e.Item.FindControl("TestData");
                    newRow.BgColor = "Yellow";
                }
                else if(UserId == "2")
                {
                    HtmlTableCell newRow = (HtmlTableCell)e.Item.FindControl("TestData");
                    newRow.BgColor = "Green";
                }
            }
        }

UserID 2 has multiple posts, so it is giving the below error, UserID 1 has only one post and the color is changed to that row as expected.

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Dream
  • newRow is null (Nothing). The code one line 269 isn't producing a value. – Brandon Jan 06 '14 at 17:16
  • @Brandon My listview is in UpdatePanel, is that a problem ? – Genelia D'Souza Jan 06 '14 at 17:18
  • I'm not sure, I'm not very familiar with ASP.NET. You'll want to actually have a look at what `e` produces when it gets into that function. It's most-likely not able to find "TestRow". I agree with @paqogomez though. This is probably a duplicate. – Brandon Jan 06 '14 at 17:23

2 Answers2

1

You have a NullReference exception because you're trying to find a control that doesn't exist. Indeed, in the e.Item you don't have a HtmlTable object but instead you expect a HtmlTableRow.

You should review the cast in the line 269, modifying it, from HtmlTable to HtmlTableRow:

ListViewDataItem ItemToDisplay = (ListViewDataItem)e.Item;
int UserId = (int)DataBinder.Eval(ItemToDisplay.DataItem, "UserID");
if(UserId == 1)
{
   HtmlTableRow newRow = (HtmlTableRow)e.Item.FindControl("TestRow");
   newRow.BgColor = "Yellow";
}

As advice (I don't know if you have always results to display in your ListView), check always if you have Null objects, like suggested here.

UPDATE

I tested your code in a sample ASP.NET web application and it works properly. I think then the problem could be how you're loading data into the ListView. Can you post the code you wrote to load data into the ListView control?

Community
  • 1
  • 1
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
  • @GeneliaD'Souza Have you fixed your issue? – Alberto Solano Jan 07 '14 at 08:43
  • @GeneliaD'Souza In the ItemTemplate you define the structure of each row of a table that will be shown in the listView. Furthermore, in the question that you linked, there's no but . I updated my answer.
    – Alberto Solano Jan 10 '14 at 11:04
  • please dont read the question i linked, only check my question.i tried to find the control form UpdatePanel and also ListView controls,what am i doing is checking the userid of that data in that row and according to the id i want to change the row background,please check if my code is wrong. – Genelia D'Souza Jan 10 '14 at 11:17
  • @GeneliaD'Souza I checked the code in a sample web application and it works (I used few simple data loading lines of code). Can you post the code you wrote to load data into the ListView control? – Alberto Solano Jan 10 '14 at 11:40
  • @GeneliaD'Souza Thank you. Your data source code seems correct. Are you sure that all the parameters involved in the LINQ search and in the binded collection are not null? – Alberto Solano Jan 10 '14 at 11:57
  • when i remove OnItemDataBound the listview works fine – Genelia D'Souza Jan 10 '14 at 12:16
  • @GeneliaD'Souza Okay, understood. I don't have any other idea. Yes, removing the event the listview works fine because the exception was thrown there, and without the method there's no exception to throw. Unfortunately, I don't know other alternatives for `ListViewDataItem ItemToDisplay = (ListViewDataItem)e.Item; int UserId = (int)DataBinder.Eval(ItemToDisplay.DataItem, "UserID");` and `HtmlTableRow newRow = (HtmlTableRow)e.Item.FindControl("TestRow");`. Sorry! Check again the object `HtmlTable newRow` is still null or not. – Alberto Solano Jan 10 '14 at 14:05
  • Please check the question, it is changed, iam getting new type of errors now – Genelia D'Souza Jan 11 '14 at 05:20
  • @GeneliaD'Souza You have that exception maybe because there's something related to `UserID == 2` that is null. Debug and check the data you're using for each user. – Alberto Solano Jan 13 '14 at 08:47
  • Thanks,but i got the answer at forums.asp.net and i cant answer here because of some people marked it as duplicate . – Genelia D'Souza Jan 14 '14 at 08:25
  • @GeneliaD'Souza Ah, good for you. Which was the problem? It was a null property related to UserID? – Alberto Solano Jan 14 '14 at 08:39
  • since i was new to this itemdatabound event, i didnt know that Alternate template was problem. it was unable to find the table because alternate was displaying table with different id. – Genelia D'Souza Jan 14 '14 at 14:43
0

Your line:

  HtmlTable newRow = (HtmlTable)e.Item.FindControl("TestRow");

Is not finding that control, hence you get a NullRef error on newRow.

T McKeown
  • 12,971
  • 1
  • 25
  • 32