O
oafyuf
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 ###########
<aspataGrid Border="1" Runat="server" Id="dgApplicants">
<Columns>
<asp:BoundColumn DataField="ApplicantRef" Visible="False" />
<%-- Some more BoundColumns and TemplateColumns --%>
<asp:TemplateColumn>
<ItemTemplate>
<tr>
<td width="30px"> </td>
<td colspan="8">
<aspataList Border="0" Runat="server" Id="dgApplications"
AutoGenerateColumns="False" Height="0px" Width="100%"
AllowSorting="False">
<ItemTemplate>
<b>App <%# DataBinder.Eval(Container.DataItem,
"VendorNo")%> <%# DataBinder.Eval(Container.DataItem,
"ApplicationRef")%> v<%# DataBinder.Eval(Container.DataItem,
"Version")%> <%# DataBinder.Eval(Container.DataItem,
"Status")%></b>
</ItemTemplate>
</aspataList>
</td>
</tr>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
// ######### ASPX ENDS ###########
// ######### CODE-BEHIND ###########
//adoApplicantReader is the outer loop
//adoApplicationReader is the inner loop
if(objDuplicatePartiesApplicant.ListParties(inputSurname, inputTown,
inputMainCPH, inputBusinessName, inputPostcode, inputOrderBy, ref
adoApplicantReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// OK, the SQL data layer object returned "success", but did we get
some results?
if(adoApplicantReader.HasRows)
{
// successful search: hide prompt and display Update button
lblPrompt.Visible = false;
btnUpdate.Visible = true;
// databinding method
this.dgApplicants.DataSource = adoApplicantReader;
this.dgApplicants.DataBind();
adoApplicantReader.Close();
// with the firehose cursor (SQLDataReader), we have to do the query
again after it is bound to pass it's members...
if(objDuplicatePartiesApplicant.ListParties(inputSurname, inputTown,
inputMainCPH, inputBusinessName, inputPostcode, inputOrderBy, ref
adoApplicantReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// SQL data layer object returned "success"
if(adoApplicantReader.HasRows)
{
while (adoApplicantReader.Read())
{
int intOrd = adoApplicantReader.GetOrdinal("ApplicantRef");
int intApplicantRef = (int)adoApplicantReader.GetValue(intOrd);
SqlDataReader adoApplicationReader = null;
if(objDuplicatePartiesApplication.ListPartyApplications(intApplicantRef,
ref adoApplicationReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// did we get some results?
if(adoApplicationReader.HasRows)
{
// find a reference to the nested DataList
foreach(DataGridItem dgItem in dgApplicants.Items)
{
DataList dgSelected =
(DataList)dgItem.FindControl("dgApplications");
// here I need to find the reference name of each nested
datagrid
// and assign data to that...
Response.Write("<tr><td><font color=#cc0000>ClientID:
"+dgSelected.ClientID+"</font></td></tr>"); // debug
dgSelected.DataSource = adoApplicationReader;
dgSelected.DataBind();
}
}
adoApplicationReader.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...
adoApplicantReader.Close();
}
else
{
lblPrompt.Text = "DATABASE ERROR:<br/><font color=#ff0000>\"" +
objDuplicatePartiesApplicant.NativeError + "\"</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
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 ###########
<aspataGrid Border="1" Runat="server" Id="dgApplicants">
<Columns>
<asp:BoundColumn DataField="ApplicantRef" Visible="False" />
<%-- Some more BoundColumns and TemplateColumns --%>
<asp:TemplateColumn>
<ItemTemplate>
<tr>
<td width="30px"> </td>
<td colspan="8">
<aspataList Border="0" Runat="server" Id="dgApplications"
AutoGenerateColumns="False" Height="0px" Width="100%"
AllowSorting="False">
<ItemTemplate>
<b>App <%# DataBinder.Eval(Container.DataItem,
"VendorNo")%> <%# DataBinder.Eval(Container.DataItem,
"ApplicationRef")%> v<%# DataBinder.Eval(Container.DataItem,
"Version")%> <%# DataBinder.Eval(Container.DataItem,
"Status")%></b>
</ItemTemplate>
</aspataList>
</td>
</tr>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
// ######### ASPX ENDS ###########
// ######### CODE-BEHIND ###########
//adoApplicantReader is the outer loop
//adoApplicationReader is the inner loop
if(objDuplicatePartiesApplicant.ListParties(inputSurname, inputTown,
inputMainCPH, inputBusinessName, inputPostcode, inputOrderBy, ref
adoApplicantReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// OK, the SQL data layer object returned "success", but did we get
some results?
if(adoApplicantReader.HasRows)
{
// successful search: hide prompt and display Update button
lblPrompt.Visible = false;
btnUpdate.Visible = true;
// databinding method
this.dgApplicants.DataSource = adoApplicantReader;
this.dgApplicants.DataBind();
adoApplicantReader.Close();
// with the firehose cursor (SQLDataReader), we have to do the query
again after it is bound to pass it's members...
if(objDuplicatePartiesApplicant.ListParties(inputSurname, inputTown,
inputMainCPH, inputBusinessName, inputPostcode, inputOrderBy, ref
adoApplicantReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// SQL data layer object returned "success"
if(adoApplicantReader.HasRows)
{
while (adoApplicantReader.Read())
{
int intOrd = adoApplicantReader.GetOrdinal("ApplicantRef");
int intApplicantRef = (int)adoApplicantReader.GetValue(intOrd);
SqlDataReader adoApplicationReader = null;
if(objDuplicatePartiesApplication.ListPartyApplications(intApplicantRef,
ref adoApplicationReader) == MyAssembly.AESIS.ReturnCodes.Success)
{
// did we get some results?
if(adoApplicationReader.HasRows)
{
// find a reference to the nested DataList
foreach(DataGridItem dgItem in dgApplicants.Items)
{
DataList dgSelected =
(DataList)dgItem.FindControl("dgApplications");
// here I need to find the reference name of each nested
datagrid
// and assign data to that...
Response.Write("<tr><td><font color=#cc0000>ClientID:
"+dgSelected.ClientID+"</font></td></tr>"); // debug
dgSelected.DataSource = adoApplicationReader;
dgSelected.DataBind();
}
}
adoApplicationReader.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...
adoApplicantReader.Close();
}
else
{
lblPrompt.Text = "DATABASE ERROR:<br/><font color=#ff0000>\"" +
objDuplicatePartiesApplicant.NativeError + "\"</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