Problem with DataList within DataList

W

Will

Hi Guys -

I am having a problem with one my datalist dlProjectCustomFields that
is 2 layers deep from my primary dlAccounts datalist. All my
datalists populate with the correct values withou any issues except
for the dlProjectCustomFields. All the data within the
dlProjectCustomFields is identical for each new item that is created
in my dlProjects datalist and I am not sure why. I have an
OnItemCreated function that kicks off and has a unique SONumber
associated with each one, and my DataLists when I assign them to the
dlProjectCustomFields are unique, however somewhere along the line all
the values are becoming identical. I have included my asp code, as
well as my dlProjects_OnItemCreated function below. Any assistance is
greatly appreciated as I am stumped. Thanks... Will

protected void dlProjects_OnItemCreated(Object sender,
DataListItemEventArgs e)
{
SqlDataSource sdsCustomValue = new
SqlDataSource(strConnection, "sc_GetCustomFieldValues");
sdsCustomValue.SelectCommandType =
SqlDataSourceCommandType.StoredProcedure;
Parameter pm = new Parameter("AccNumber", TypeCode.Int32,
((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString());
pm.Direction = ParameterDirection.Input;
sdsCustomValue.SelectParameters.Add(pm);
sdsCustomValue.DataSourceMode = SqlDataSourceMode.DataSet;
DataView dvSOCustomValues =
(DataView)sdsCustomValue.Select(DataSourceSelectArguments.Empty);

DataView dvCurr = new DataView(dvProjectCustomFields.Table);
if (dvSOCustomValues.Count > 0)
{
foreach (DataRowView drv2 in dvSOCustomValues)
{
dvCurr.Table.Rows.Find(drv2.Row["CustomFieldDefinitionKeyID"])[3] =
drv2.Row["CustomFieldValue"];
}
}
DataList dl =
(DataList)e.Item.FindControl("dlProjectCustomFields");
dl.DataSource = new DataView(dvCurr.Table);
dl.DataBind();
}

<asp:DataList ID="dLAccounts" GridLines="both"
OnItemCreated="dlAccounts_OnItemCreated" runat="server">
<ItemTemplate>
<%# Eval("AccountName") %>
<%# Eval("AccountNumber") %>
<asp:DataList ID="AccCustomFields" runat="server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,
"CustomFieldLabel")%>
<%#DataBinder.Eval(Container.DataItem,"CustomFieldValue")
%>
</ItemTemplate>
</asp:DataList>
<asp:DataList ID="dlProjects" runat="server"
DataKeyField="SONumber" OnItemCreated="dlProjects_OnItemCreated">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"SONumber") %></td>
<%# DataBinder.Eval(Container.DataItem,"BriefDescription")
%>
<asp:DataList RepeatColumns="2"
ID="dlProjectCustomFields" runat="server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,
"CustomFieldLabel")%>
<%#DataBinder.Eval(Container.DataItem,"CustomFieldValue")
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
 
W

Walter Wang [MSFT]

Hi Will,

I'm having trouble to reproduce the issue from your posted code. I think a
complete reproducible project will be better to troubleshoot such issues.
Would you please send me one? Thank you for your effort.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Will

Hi Walter -

I have attached the cs and aspx file to this post. Please note that I
use a database to get the data, however I can't add this due to
security issues.

Wil
 
W

Walter Wang [MSFT]

Hi Will,

Thanks for the code, it helped me to see more clearly about your objective,
although I cannot debug it since I don't have your database.

protected void dlProjects_OnItemCreated(Object sender,
DataListItemEventArgs e)
{
SqlDataSource sdsCustomValue = new SqlDataSource(strConnection,
"sc_GetCustomFieldValues");
sdsCustomValue.SelectCommandType =
SqlDataSourceCommandType.StoredProcedure;
Parameter pm = new Parameter("AccNumber", TypeCode.Int32,
((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString());
pm.Direction = ParameterDirection.Input;
sdsCustomValue.SelectParameters.Add(pm);
sdsCustomValue.DataSourceMode = SqlDataSourceMode.DataSet;
DataView dvSOCustomValues =
(DataView)sdsCustomValue.Select(DataSourceSelectArguments.Empty);

DataView dvCurr = new DataView(dvProjectCustomFields.Table);
if (dvSOCustomValues.Count > 0)
{
foreach (DataRowView drv2 in dvSOCustomValues)
{

dvCurr.Table.Rows.Find(drv2.Row["CustomFieldDefinitionKeyID"])[3] =
drv2.Row["CustomFieldValue"];
}
}
DataList dl = (DataList)e.Item.FindControl("dlProjectCustomFields");
dl.DataSource = new DataView(dvCurr.Table);
dl.DataBind();
}

From above code, I can see you're using the member variable
dvProjectCustomFields.Table to create a new DataView and from there you
modified the data inside it. Please note this approach is directly
modifying the shared copy of DataTable from multiple
dlProjects_OnItemCreated invocations. That's why you're seeing the same
data.

The correct way is to create a copy of the dvProjectCustomFields.Table:

DataView dvCurr = new DataView(dvProjectCustomFields.Table.Copy());


Please try this and let me know the result. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Will,

I'm writing to check the status of this post. Please feel free to let me
know if there's anything else I can help. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top