-1

i am using following code to generate ordered list. this code create exception on line number 73, 106, 108 when this page is refreshed during load testing. menu gets created in development and testing environment. it crashes on production side or during load testing **

try
{
    dtDepartments = DB.GetDataSet("[dbo].[Departments]", AppLogic.CachingOn).Tables[0];
    dtCategories = DB.GetDataSet("[dbo].[Categories]", AppLogic.CachingOn).Tables[0];

    foreach (DataRow drDept in dtDepartments.Rows)
    {
        dtCategories.DefaultView.RowFilter = "[SEName] Like '" + drDept["SEName"].ToString() + "-%' And [ParentCategoryId] = 0";
        CategoryView = null;
        CategoryView = dtCategories.DefaultView;


        foreach (DataRowView CategoryRowView in CategoryView)
        {
            DataRow CategoryRow = CategoryRowView.Row;  \\line number 73

            dtCategories.DefaultView.RowFilter = "[SEName] Like '" + drDept["SEName"].ToString() + "-%' And [ParentCategoryId] = " + CategoryRow["CategoryID"].ToString();

            CategoryViewTwoCount = dtCategories.DefaultView.Count;

            CategoryViewTwo = null;
            if (CategoryViewTwoCount > 0)
            {
                CategoryViewTwo = dtCategories.DefaultView;
            }
            else
            {
            }

            if (CategoryViewTwoCount > 0)
            {
                foreach (DataRowView CategoryRowViewTwo in CategoryViewTwo)    \\line number 106
                {
                    DataRow CategoryRowTwo = CategoryRowViewTwo.Row;           \\line number 108
                }
            }
        }
    }

    TreeMenu = HTMLContents.ToString();

    if (CategoryViewTwo != null)
    {
        CategoryViewTwo = null;
    }
    if (CategoryView != null)
    {
       CategoryView = null;
    }

    HTMLContents = null;
    dtCategories.Dispose();
    dtDepartments.Dispose();
    dtCategories = null;
    dtDepartments = null;
}
catch (Exception ex)
{
    //Log for staging
    //AppLogic.WriteAppLogs(string.Format("Exception:\n{0}\nMessage:\n{1}Inner Exception:\n{2}\nStack Trace:{3}", ex.ToString(), ex.Message.ToString(), ex.InnerException.ToString(), ex.StackTrace.ToString()));

    AppLogic.WriteAppLogs("Source " + ex.Source + ex.Message +  ex.StackTrace);

    //Log for production
    //DB.ExecuteSQL(String.Format("INSERT INTO ErrorLog (source, errormsg) VALUES ({0}, {1})", DB.SQuote(CommonLogic.GetThisPageName(true)), DB.SQuote(ex.ToString())));
}

return TreeMenu;

Here is the Exception Log

Source App_Web_treeviewnew.aspx.cdcab7d2.de1ldkd2 Object reference not set to an instance of an object. at treeview.DisplayTreeMenu() in treeviewNew.aspx.cs:line 108 6/14/2013 5:25:19 PM

Source mscorlib The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at System.Data.DataView.CopyTo(DataRowView[] array, Int32 index) at System.Data.DataView.GetEnumerator() at treeview.DisplayTreeMenu() in treeviewNew.aspx.cs:line 106 6/14/2013 5:26:03 PM

Source mscorlib The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at System.Data.DataView.CopyTo(DataRowView[] array, Int32 index) at System.Data.DataView.GetEnumerator() at treeview.DisplayTreeMenu() in treeviewNew.aspx.cs:line 106 6/14/2013 5:26:38 PM

Source mscorlib The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at System.Data.DataView.CopyTo(DataRowView[] array, Int32 index) at System.Data.DataView.GetEnumerator() at treeview.DisplayTreeMenu() in treeviewNew.aspx.cs:line 106 6/14/2013 5:26:52 PM

Source mscorlib The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at System.Data.DataView.CopyTo(DataRowView[] array, Int32 index) at System.Data.DataView.GetEnumerator() at treeview.DisplayTreeMenu() in treeviewNew.aspx.cs:line 106 6/14/2013 5:27:04 PM

Source App_Web_treeviewnew.aspx.cdcab7d2.de1ldkd2 Object reference not set to an instance of an object. at treeview.DisplayTreeMenu() in treeviewNew.aspx.cs:line 108 6/14/2013 5:28:01 PM

Source mscorlib The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at System.Data.DataView.CopyTo(DataRowView[] array, Int32 index) at System.Data.DataView.GetEnumerator() at treeview.DisplayTreeMenu() in treeviewNew.aspx.cs:line 106 6/14/2013 5:29:45 PM

Source App_Web_treeviewnew.aspx.cdcab7d2.de1ldkd2 Object reference not set to an instance of an object. at treeview.DisplayTreeMenu() in treeviewNew.aspx.cs:line 108 6/14/2013 5:31:09 PM

Source App_Web_treeviewnew.aspx.cdcab7d2.de1ldkd2 Object reference not set to an instance of an object. at treeview.DisplayTreeMenu() in treeviewNew.aspx.cs:line 73 6/14/2013 5:32:14 PM

Source mscorlib The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at System.Data.DataView.CopyTo(DataRowView[] array, Int32 index) at System.Data.DataView.GetEnumerator() at treeview.DisplayTreeMenu() in treeviewNew.aspx.cs:line 106 6/14/2013 5:32:27 PM

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jun 14 '13 at 23:47

1 Answers1

0

The exception means a null value somewhere, most likely here: CategoryRowView.Row, review if this variable is defined before the exception is thrown, and do the same in the other lines you get this exception.

Rafael
  • 2,827
  • 1
  • 16
  • 17
  • it seems this exception is due to cashing, when cashing consumes allowed memory, it is recycled and hence so exception is generated – Shujaat Kagathra Jun 17 '13 at 15:22
  • why my post is marked as -1 ? i had checked that if object is not null before assing it, or before traversing it with foreach loop, but still having same issues & exceptions. – Shujaat Kagathra Jun 17 '13 at 15:23