473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

DataSource Problem for Nested DataList

Hi All,

Thanks for reading my post. I have been working on getting nested
datalists working properly wihtin my framework for many days and I
think I'm almost there. See if you could help me get over the hump
here.

Some of our products come in different sizes - i.e. what I call
"units". Some products don't. I successfully created the first
datalist (called productList) to show our products with their general
description, but now I want to show the available units for the
products (called productUnits). So what I'm doing is nesting two
datalists, using the ItemDataBound event on the outer datalist.

I've walked through in debug mode and I am successfully retreiving the
units, but I get an error when I declare the datasource for the
datalist "productUnits". I am getting the following error:
"An invalid data source is being used for productUnits. A valid data
source must implement either IListSource or IEnumerable."

Here is the code. Can you help? Thanks is advance!!

Scott L. Smeester
TIDF

===========================
ProductList.aspx
===========================

<asp:DataList ID="productList" runat="server" RepeatColumns="1"
RepeatDirection="Horizontal" OnItemDataBound="list_ItemDataBound"
DataKeyField="ProductID">
<ItemTemplate>
<a href='<%# "~/Products/ProductPage.aspx?ProductID=" +
Eval("ProductID") %>' runat="server" >
<%# Eval("ProductName") %>
</a>
<br />
<%# Eval("BriefDesc") %>
<br />
<asp:DataList ID="productUnits" runat="server" Visible="false">
<ItemTemplate>
<asp:Label ID="lblUnitName" runat="server"><%#
Eval("UnitName") %></asp:Label>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>

===========================
ProductList.aspx.cs
===========================

protected void list_ItemDataBound(object sender, DataListItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// if this product has units, show the different sizes
bool unitsYN =
Convert.ToBoolean(((DataRowView)e.Item.DataItem).R ow.ItemArray[3].ToString());

if (unitsYN == true)
{
// get the current productID
string productID =
Convert.ToString(((DataRowView)e.Item.DataItem).Ro w.ItemArray[0].ToString());

// find the datalist
DataList productUnits =
(DataList)e.Item.FindControl("productUnits");

// retrieve the data
productUnits.DataSource =
CatalogAccess.GetProductUnits(productID);
productUnits.DataBind();

}
}
}

===========================
CatalogAccess.cs
===========================

public struct ProductUnits
{
public string UnitName;
}

public class CatalogAccess
{
// get product details
public static ProductUnits GetProductUnits(string productID)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
// set the store procedure name
comm.CommandText = "GetProductUnits";
// create a new paramater
DbParameter param = comm.CreateParameter();
param.ParameterName = "@ProductID";
param.Value = productID;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// execute the store procedure
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
// wrap retrieved data into a CategoryDetails object
ProductUnits prodUnitDetails = new ProductUnits();
if (table.Rows.Count > 0)
{
// get the first table row
DataRow dr = table.Rows[0];
// get product details
prodUnitDetails.UnitName = dr["UnitName"].ToString();
}
// return department details
return prodUnitDetails;
}
}

===========================
GenericDataAccess.cs
===========================

public class GenericDataAccess
{
// executes a command and returns the results as a DataTable object
public static DataTable ExecuteSelectCommand(DbCommand command)
{
// The DataTable to be returned
DataTable table;

// Execute the command making sure the connection gets closed
in the end
try
{
// Open the data connection
command.Connection.Open();

// Execute the command and save the results in a DataTable
DbDataReader reader = command.ExecuteReader();
table = new DataTable();
table.Load(reader);

// Close the reader
reader.Close();
}
catch (Exception ex)
{
Utilities.LogError(ex);
throw ex;
}
finally
{
// Close the connection
command.Connection.Close();
}
return table;
}
}

===========================
GetProductUnits Stored Procedure
===========================

ALTER PROCEDURE GetProductUnits
(@ProductID INT)
AS
SELECT UnitName
FROM ProductUnits
WHERE ProductID = @ProductID AND LiveYN = 'True'
RETURN

===========================
END POSTING
===========================

May 8 '06 #1
2 3415
Hello again,

Well, I've gotten a little further. I'll post what I've found out so
far but I have ran into another problem. When debugging, "pu.UnitName"
resolves to a value I am expecting, i.e. the unit name, however I think
it is throwing a null exception (ya think?). I'm getting this error
now:
"NullReferenceException was unhandled by user code
Object reference not set to an instance of an object."

Thanks for your help!

Scott

=========================================
New "list_ItemDataBound" event:
=========================================

protected void list_ItemDataBound(object sender, DataListItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// if this product has units, show the different sizes
bool unitsYN =
Convert.ToBoolean(((DataRowView)e.Item.DataItem).R ow.ItemArray[3].ToString(*));
if (unitsYN == true)
{
// get the current productID
string productID =
Convert.ToString(((DataRowView)e.Item.DataItem).Ro w.ItemArray[0].ToString()*);
// find the datalist
DataList productUnits =
(DataList)e.Item.FindControl("productUnits");

// make the datalist visible
productUnits.Visible = true;

// stores product details
ProductUnits pu;
pu = CatalogAccess.GetProductUnits(productID);

// find the label controls
Label lblUnitName =
(Label)e.Item.FindControl("lblUnitName");

// display product details
lblUnitName.Text = pu.UnitName;
}
}
}

=========================================
END POSTING
=========================================

May 9 '06 #2
I finally figured it out how to nest datalists (the way I wanted to do
it). And since I spent a lot of time researching the forums and trying
many different things, I want to make sure that the person who is
trying to do the same can see what I did. I owe this one!!

Note:
Disregard my follow up message with the "New list_ItemDataBound event"
because I could not figure out how to locate the "lblUnitName" label to
set the unit data to it and that is now I wanted to do it anyways. My
original problem was because I could not bind the data I had received
to the datalist, but that is because I was trying to use a "struct",
which doesn't work, because many of our products have more than one
unit.

Here is the complete functioning code:
===========================
ProductList.aspx
===========================

<asp:DataList ID="productList" runat="server" RepeatColumns="1"
RepeatDirection="Horizontal" OnItemDataBound="list_ItemDataBound"
DataKeyField="ProductID">
<ItemTemplate>
<a href='<%# "~/Products/ProductPage.aspx?ProductID=" +
Eval("ProductID") %>' runat="server" >
<%# Eval("ProductName") %>
</a>
<br />
<%# Eval("BriefDesc") %>
<br />
<asp:DataList ID="productUnits" runat="server" Visible="false">
<ItemTemplate>
<asp:Label ID="lblUnitName" runat="server"><%#
Eval("UnitName") %></asp:Label>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>

===========================
ProductList.aspx.cs
===========================

protected void list_ItemDataBound(object sender, DataListItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// if this product has units, show the different sizes
bool unitsYN =
Convert.ToBoolean(((DataRowView)e.Item.DataItem).R ow.ItemArray[3].ToString());

if (unitsYN == true)
{
// get the current productID
string productID =
Convert.ToString(((DataRowView)e.Item.DataItem).Ro w.ItemArray[0].ToString());

// find the datalist
DataList productUnits =
(DataList)e.Item.FindControl("productUnits");

// make the datalist visible
productUnits.Visible = true;

// locate and bind data
productUnits.DataSource =
CatalogAccess.GetProductUnits(productID);
productUnits.DataBind();

}
}
}

===========================
*REVISED* CatalogAccess.cs
===========================

public class CatalogAccess
{
// get product details
public static DataTable GetProductUnits(string productID)
{
// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();
// set the store procedure name
comm.CommandText = "GetProductUnits";
// create a new paramater
DbParameter param = comm.CreateParameter();
param.ParameterName = "@ProductID";
param.Value = productID;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
// execute the store procedure
return GenericDataAccess.ExecuteSelectCommand(comm);
}
}

===========================
GenericDataAccess.cs
===========================

public class GenericDataAccess
{
// executes a command and returns the results as a DataTable object
public static DataTable ExecuteSelectCommand(DbCommand command)
{
// The DataTable to be returned
DataTable table;

// Execute the command making sure the connection gets closed
in the end
try
{
// Open the data connection
command.Connection.Open();

// Execute the command and save the results in a DataTable
DbDataReader reader = command.ExecuteReader();
table = new DataTable();
table.Load(reader);

// Close the reader
reader.Close();
}
catch (Exception ex)
{
Utilities.LogError(ex);
throw ex;
}
finally
{
// Close the connection
command.Connection.Close();
}
return table;
}
}

===========================
GetProductUnits Stored Procedure
===========================

ALTER PROCEDURE GetProductUnits
(@ProductID INT)
AS
SELECT UnitName
FROM ProductUnits
WHERE ProductID = @ProductID AND LiveYN = 'True'
RETURN

===========================
END POSTING
===========================

May 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: V. Jenks | last post by:
What seems like a simple thing is apparently not so straightforward? I have a datalist. Inside of that datalist is an <itemtemplate> secion which contains other server controls such as a...
0
by: Marty U. | last post by:
I have two datalists nested. On the ItemDataBound Event of the first datalist I databind the second with specific info. A couple questions: 1) How can I utilize the OnItemCommand of the nested...
3
by: Derek | last post by:
I have a nested datalist with a dropdownlist. I need to capture the selectedvalue of the dropdownlist so I can update a database table. My question then is...how do I get the value from the...
2
by: Will Chamberlain | last post by:
I am currently converting some VB.NET web apps to C#. It has been pretty seamless so far but have currently run into some problems that I can't resolve. The line that generates the error is: The...
0
by: Chris | last post by:
I've been searching all over and think I am close, but keep getting the error "Index out of range" when trying to reference a nested datagrid when an OnEditCommand event is raised. When the...
1
by: needin4mation | last post by:
Hi, I have a datalist. In this datalist I have a datagrid. The datalist is the master. The datagrid is the detail. It works fine. I populate the datagrid inside of the datalist using the...
3
by: Martin | last post by:
Hi, I have a very frustrating problem that I have researched for countless hours to no avail. There are many posts asking very similar things, however none usefull in my situation. I am using VS...
0
by: H5N1 | last post by:
Hi there My problem is that in when I update GridView row, which is nested into DataList control, I want to refresh also DataList in which the GridView is nested, since after update, trigger in...
1
by: AJ | last post by:
Hi all, I am trying pass a value to the DataSource method in a DataList like below. DataSource = <%# GetData(DataBinder.Eval(Container.DataItem, "ID"))%>" I get an error with this code, and am...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.