Home All Groups Group Topic Archive Search About

Problem with DataList within DataList

Author
10 Oct 2007 9:16 PM
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>

Author
11 Oct 2007 7:54 AM
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 (waw***@online.microsoft.com, 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.
Author
14 Oct 2007 3:37 AM
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

On Thu, 11 Oct 2007 07:54:43 GMT, waw***@online.microsoft.com ("Walter
Wang [MSFT]") wrote:

Show quote
>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 (waw***@online.microsoft.com, 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.
Author
15 Oct 2007 7:50 AM
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 (waw***@online.microsoft.com, 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.
Author
18 Oct 2007 3:19 AM
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 (waw***@online.microsoft.com, 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.
Author
14 Oct 2007 3:37 AM
Will
[attached file: Default.aspx.cs]
[attached file: Default.aspx]

AddThis Social Bookmark Button