nested datalist problems

L

Laura K

I would like to create a nested datalist inside the
selecteditemtemplate of a datalist. My hope is that when a user clicks
on a link a set of child links is displayed. For example if the user
clicks men's clothing he would get a list of sub categories like pants,
shirts, shorts etc

I have put together the following and I get the original datalist fine.
When I try to get the sublist I get the following error: Object
reference not set to an instance of an object.
The offending line is

brandSubList.DataSource = Catalog.GetSubCatsInBrands(BrandID)
from the code behind listed below.
---------------------------------
HTML code looks like this:


<asp:datalist id="brandList" Runat="server">
<ItemTemplate>
<DIV class="navcells"><B>
<asp:hyperlink id="brandLink" Runat="server" NavigateUrl= '<%#
"../default.aspx?brandID=" &amp; databinder.eval(container.dataitem,
"strBrandCode") %>' text= '<%# DataBinder.Eval( Container.DataItem,
"strBrandName" ) %>' >
</asp:hyperlink></B></DIV>
</ItemTemplate>
<SelectedItemTemplate>
<asp:DataList id="brandSubList" Runat="server">
<ItemTemplate><DIV class="navcells"><B>
<asp:LinkButton id="brandsubLink" Runat="server"
CommandName="select"><%# DataBinder.Eval( Container.DataItem,
"strSubCategory" ) %>
</asp:LinkButton></B></DIV>
</ItemTemplate>
</asp:DataList>
</SelectedItemTemplate>
</asp:datalist>

---------------------------------------------------------
My code behind looks like this:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim BrandID As String = Request.QueryString("brandID")

brandList.DataSource = Catalog.GetBrands()
brandList.DataBind()

If Not BrandID Is Nothing Then
Dim BrandSublist As DataList

brandSubList.DataSource =
Catalog.GetSubCatsInBrands(BrandID)
brandSubList.DataBind()

End If
--------------------------------------------------------
Data is pulled from a business objects doc like this

Public Shared Function GetBrands() As SqlDataReader
'Create connetion objedt
Dim Connection As New SqlConnection(connectionString)
'Create and initialize the command Object
Dim command As New SqlCommand("New_GetBrands", Connection)
command.CommandType = CommandType.StoredProcedure

'Open the connection
Connection.Open()

'Return a SqlDatatreader
Return command.ExecuteReader(CommandBehavior.CloseConnection)
End Function

Public Shared Function GetSubCatsInBrands(ByVal brandID As String) As
SqlDataReader
'create the connection string
Dim connection As New SqlConnection(connectionString)
'Create and initialize the command Object
Dim command As New SqlCommand("New_getSubCatsInBrands",
connection)
command.CommandType = CommandType.StoredProcedure

'Add an input parameter and suply a valiue for it
command.Parameters.Add("@brandID", SqlDbType.NVarChar, 5)
command.Parameters("@brandID").Value = brandID


'Open the connection, exectue the command, and clost the
connection

Try
connection.Open()
command.ExecuteNonQuery()
Finally
connection.Close()
End Try
 
B

bhawin13

Hello

If you check last few lines of your functions GetSubCatsInBrands and
GetBrands then you have come to know that in GetBrands after opening
connection
-------------------------------------------------------------------------------------
'Return a SqlDatatreader
Return command.ExecuteReader(CommandBehavior.CloseConnection)
-------------------------------------------------------------------------------------
and in function GetSubCatsInBrands after opening connection
-------------------------------------------------------------------------------------
command.ExecuteNonQuery()
-------------------------------------------------------------------------------------

So in GetSubCatsInBrands function It should be Return
command.ExecuterReader as writen in GetBrands function.

ExecuteNonQuery will not return recordset it will just execute insert,
delete or update command.

B
 
L

Laura K

Hi, Thanks. I actually realized that and got the datalist working on
it's own. I can properly catch the parameter and the list will work
EXCEPT when it is nested inside another datalist.

Any advice?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148

Latest Threads

Top