title: nested ListView not getting values from parent listview
body: <p>My site is in c# &amp; is fully dynamic. The Menu bar is also dynamic. Each navigation menu has sub menus.. Because Menus needs to be build from database I have used Listview Control. In my website for menus I need to use 2 child Listview inside one parent Listview. So it look like this.</p>
  <p>Parent Menu Listview has Sub menu listview &amp; sub Menu listviews has another child Listview in which I need to show any one product related parent Listview Category..</p>
  <p>My Sub Menu has builded properly but inside that If I tries to bind another Listview then it shows me the error (Object reference not set as an instance of an object.) Then I tried with manual sql query without getting where clause from ItemDataBound defined then too it shows same error.</p>
  <p>How my menus looks like (the empty space(right side) in sub menu where I want to display one product of power bank category)
  <a href="https://i.stack.imgur.com/jPc6H.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jPc6H.jpg" alt="enter image description here" /></a></p>
  <pre><code>&lt;asp:ListView ID=&quot;mainMenu&quot; runat=&quot;server&quot; DataKeyNames=&quot;name&quot; OnItemDataBound=&quot;OnItemDataBound&quot;&gt;
       &lt;ItemTemplate&gt;
            &lt;li&gt;&lt;asp:HyperLink ID=&quot;mainLinks&quot; runat=&quot;server&quot; NavigateUrl='&lt;%# Eval(&quot;name&quot;, &quot;~/{0}&quot;) %&gt;' Text='&lt;%# Eval(&quot;name&quot;) %&gt;'&gt;&lt;/asp:HyperLink&gt;
  
                &lt;ul class=&quot;super-child&quot;&gt;
                    &lt;asp:ListView ID=&quot;childMenu&quot; runat=&quot;server&quot; DataKeyNames=&quot;name&quot;&gt;
                         &lt;ItemTemplate&gt;
                              &lt;div class=&quot;super-child-inner&quot;&gt;
                                   &lt;div class=&quot;list-child&quot;&gt;
                                   &lt;li&gt;&lt;asp:HyperLink ID=&quot;cat3&quot; runat=&quot;server&quot; NavigateUrl='&lt;%# Eval(&quot;category&quot;) &amp; Eval(&quot;name&quot;, &quot;/{0}&quot;) %&gt;' Text='&lt;%# Eval(&quot;name&quot;) %&gt;'&gt;&lt;/asp:HyperLink&gt;&lt;/li&gt;
                              &lt;/div&gt;
  
                              &lt;div class=&quot;recommended&quot;&gt;
                              &lt;asp:ListView ID=&quot;recommendedProducts&quot; runat=&quot;server&quot;&gt;
                                   &lt;ItemTemplate&gt;
                                       &lt;div class=&quot;rec-img&quot;&gt;
                                            &lt;asp:Image ID=&quot;recImg&quot; runat=&quot;server&quot; ImageUrl='&lt;%# Eval(&quot;image&quot;) %&gt;' /&gt;
                                       &lt;/div&gt;
                                       &lt;p class=&quot;rec-name&quot;&gt;&lt;asp:HyperLink ID=&quot;recLink&quot; runat=&quot;server&quot; NavigateUrl='&lt;%# Eval(&quot;category&quot;) &amp; Eval(&quot;name&quot;, &quot;/{0}&quot;) %&gt;' Text='&lt;%# Eval(&quot;name&quot;) %&gt;'&gt;&lt;/asp:HyperLink&gt;&lt;/p&gt;
                                   &lt;/ItemTemplate&gt;
                                &lt;/asp:ListView&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/ItemTemplate&gt;
                &lt;/asp:ListView&gt;
             &lt;/ul&gt;
          &lt;/li&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:ListView&gt;
  </code></pre>
  <p>ItemDataBound Code</p>
  <pre><code>protected void onItemDataBound(object sender, ListViewItemEventArgs e)
  {
      if (e.Item.ItemType == ListViewItemType.DataItem) {
          ListViewDataItem itm = (ListViewDataItem)e.Item;
          string name = mainMenu.DataKeys(itm.DataItemIndex)(&quot;name&quot;);
          ListView childMenu = e.Item.FindControl(&quot;childMenu&quot;) as ListView;
          ListView recommendedProducts = e.Item.FindControl(&quot;recommendedProducts&quot;) as ListView;
          string constr = ConfigurationManager.ConnectionStrings(&quot;conio&quot;).ConnectionString;
          using (MySqlConnection con = new MySqlConnection(constr)) {
              using (MySqlCommand cmd = new MySqlCommand()) {
                  cmd.CommandText = &quot;SELECT * FROM product_categories WHERE category = '&quot; + name + &quot;' and status = 'active'&quot;;
                  cmd.Connection = con;
                  using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd)) {
                      DataTable dt = new DataTable();
                      sda.Fill(dt);
                      childMenu.DataSource = dt;
                      childMenu.DataBind();
                  }
              }
          }
  
          try {
              string constr2 = ConfigurationManager.ConnectionStrings(&quot;conio&quot;).ConnectionString;
              using (MySqlConnection con = new MySqlConnection(constr2)) {
                  using (MySqlCommand cmd = new MySqlCommand()) {
                      cmd.CommandText = &quot;SELECT ID,slug,product_name,image FROM products WHERE (category Like @category) and status = 'active' Limit 1&quot;;
                      cmd.Connection = con;
                      cmd.Parameters.AddWithValue(&quot;@categoty&quot;, &quot;%&quot; + name + &quot;%&quot;);
                      using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd)) {
                          DataTable dt = new DataTable();
                          sda.Fill(dt);
                          recommendedProducts.DataSource = dt;
                          recommendedProducts.DataBind();
                      }
                  }
              }
          } catch (Exception ex) {
              Response.Write(ex);
          }
      }
  }
  </code></pre>
  <p>UPDATE (Exception)</p>
  <blockquote>
  <p>System.NullReferenceException: Object reference not set to an instance of an object. at MasterPage.onItemDataBound(Object sender, ListViewItemEventArgs e) in E:\MY WEB\brandstik.com1\MasterPage.master.vb:line 79System.NullReferenceException: Object reference not set to an instance of an object. at MasterPage.onItemDataBound(Object sender, ListViewItemEventArgs e) in E:\MY WEB\brandstik.com1\MasterPage.master.vb:line 79System.NullReferenceException: Object reference not set to an instance of an object. at MasterPage.onItemDataBound(Object sender, ListViewItemEventArgs e) in E:\MY WEB\brandstik.com1\MasterPage.master.vb:line 79System.NullReferenceException: Object reference not set to an instance of an object. at MasterPage.onItemDataBound(Object sender, ListViewItemEventArgs e) in E:\MY WEB\brandstik.com1\MasterPage.master.vb:line 79System.NullReferenceException: Object reference not set to an instance of an object. at MasterPage.onItemDataBound(Object sender, ListViewItemEventArgs e) in E:\MY WEB\brandstik.com1\MasterPage.master.vb:line 79System.NullReferenceException: Object reference not set to an instance of an object. at MasterPage.onItemDataBound(Object sender, ListViewItemEventArgs e) in E:\MY WEB\brandstik.com1\MasterPage.master.vb:line 79System.NullReferenceException: Object reference not set to an instance of an object. at MasterPage.onItemDataBound(Object sender, ListViewItemEventArgs e) in E:\MY WEB\brandstik.com1\MasterPage.master.vb:line 79System.NullReferenceException: Object reference not set to an instance of an object. at MasterPage.onItemDataBound(Object sender, ListViewItemEventArgs e) in E:\MY WEB\brandstik.com1\MasterPage.master.vb:line 79System.NullReferenceException: Object reference not set to an instance of an object. at MasterPage.onItemDataBound(Object sender, ListViewItemEventArgs e) in E:\MY WEB\brandstik.com1\MasterPage.master.vb:line 79System.NullReferenceException: Object reference not set to an instance of an object. at MasterPage.onItemDataBound(Object sender, ListViewItemEventArgs e) in E:\MY WEB\brandstik.com1\MasterPage.master.vb:line 79</p>
  </blockquote>