Hi,
I'm trying to learn ASP.NET by doing a pilot project:
I have a DataGrid which contains a nested DataList. I want to iterate
through the SQLDataReader for the DataGrid and populate each DataList
by binding to a fresh SQLDataReader. At the moment I can only bind to
the first instance of the DataList. If I could bind the data by the
ClientID (or UniqueID), I'm sure it would work. Is there a way of
doing this? Or maybe there is a better approach to getting nested data
from separate SQLDataReaders.
Here's the relevant part of the code with extraneous bits removed (I
hope the line breaks don't make it unreadable!):
// ######### ASPX ###########
<asp:DataGrid Border="1" Runat="server" Id="dgApplicant s">
<Columns>
<asp:BoundColum n DataField="Appl icantRef" Visible="False" />
<%-- Some more BoundColumns and TemplateColumns --%>
<asp:TemplateCo lumn>
<ItemTemplate >
<tr>
<td width="30px">&n bsp;</td>
<td colspan="8">
<asp:DataList Border="0" Runat="server" Id="dgApplicati ons"
AutoGenerateCol umns="False" Height="0px" Width="100%"
AllowSorting="F alse">
<ItemTemplate >
<b>App < %# DataBinder.Eval (Container.Data Item,
"VendorNo")%>&n bsp;<%# DataBinder.Eval (Container.Data Item,
"ApplicationRef ")%> v <%# DataBinder.Eval (Container.Data Item,
"Version")%>&nb sp;<%# DataBinder.Eval (Container.Data Item,
"Status")%> </b>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
// ######### ASPX ENDS ###########
// ######### CODE-BEHIND ###########
//adoApplicantRea der is the outer loop
//adoApplicationR eader is the inner loop
if(objDuplicate PartiesApplican t.ListParties(i nputSurname, inputTown,
inputMainCPH, inputBusinessNa me, inputPostcode, inputOrderBy, ref
adoApplicantRea der) == MyAssembly.AESI S.ReturnCodes.S uccess)
{
// OK, the SQL data layer object returned "success", but did we get
some results?
if(adoApplicant Reader.HasRows)
{
// successful search: hide prompt and display Update button
lblPrompt.Visib le = false;
btnUpdate.Visib le = true;
// databinding method
this.dgApplican ts.DataSource = adoApplicantRea der;
this.dgApplican ts.DataBind();
adoApplicantRea der.Close();
// with the firehose cursor (SQLDataReader) , we have to do the query
again after it is bound to pass it's members...
if(objDuplicate PartiesApplican t.ListParties(i nputSurname, inputTown,
inputMainCPH, inputBusinessNa me, inputPostcode, inputOrderBy, ref
adoApplicantRea der) == MyAssembly.AESI S.ReturnCodes.S uccess)
{
// SQL data layer object returned "success"
if(adoApplicant Reader.HasRows)
{
while (adoApplicantRe ader.Read())
{
int intOrd = adoApplicantRea der.GetOrdinal( "ApplicantRef") ;
int intApplicantRef = (int)adoApplica ntReader.GetVal ue(intOrd);
SqlDataReader adoApplicationR eader = null;
if(objDuplicate PartiesApplicat ion.ListPartyAp plications(intA pplicantRef,
ref adoApplicationR eader) == MyAssembly.AESI S.ReturnCodes.S uccess)
{
// did we get some results?
if(adoApplicati onReader.HasRow s)
{
// find a reference to the nested DataList
foreach(DataGri dItem dgItem in dgApplicants.It ems)
{
DataList dgSelected =
(DataList)dgIte m.FindControl(" dgApplications" );
// here I need to find the reference name of each nested
datagrid
// and assign data to that...
Response.Write( "<tr><td><f ont color=#cc0000>C lientID:
"+dgSelected.Cl ientID+"</font></td></tr>"); // debug
dgSelected.Data Source = adoApplicationR eader;
dgSelected.Data Bind();
}
}
adoApplicationR eader.Close();
}
}
}
}
else
{
lblPrompt.Text = "No records returned for this search. Please try
again.";
}
// tidy up before the garbage collector gets a chance to do it...
adoApplicantRea der.Close();
}
else
{
lblPrompt.Text = "DATABASE ERROR:<br/><font color=#ff0000>\ "" +
objDuplicatePar tiesApplicant.N ativeError + "\"</font><br/>Please
report this error to AESIS support.";
}
}
// ######### CODE-BEHIND ENDS ###########
The whole thing looks ugly to me because I have to keep on getting
fresh data, is there a more flexible cursor that I can use?
My next step is to investigate using DataSets, combining the XML DOMs
and doing all the manipulation of the presentation layer via
client-side JavaScript. Is this the recommended method?
Thanks,
oafyuf