how to get a count on a data relation?

  • Thread starter Thread starter tfsmag
  • Start date Start date
T

tfsmag

I tried getting the record count by doing it through the ItemDataBound
on the repeater with no luck... Can someone help me out here? Thanks in
advance.

Here is the code that sets up the data relation (it works fine)

------------------------------------------------

private void getlinks()
{
string connectionInfo =
ConfigurationSettings.AppSettings["ConnectionString"];
using(SqlConnection connection = new SqlConnection(connectionInfo))

{
connection.Open();
SqlDataAdapter da = new SqlDataAdapter("EXECUTE
sp_getlinkcat",connection);
DataSet ds = new DataSet();
da.Fill(ds,"link_cat");
SqlDataAdapter da2 = new SqlDataAdapter("EXECUTE sp_getlinks"
,connection);
da2.Fill(ds,"links");
DataRelation drel;
drel = new
DataRelation("myrelation",ds.Tables["link_cat"].Columns["cat_id"],ds.Tables["links"].Columns["cat_id"]);
ds.Relations.Add(drel);

//thecount.Text=ds.Tables["links"].ChildRelations.Count.ToString();
displaycat.DataSource = ds.Tables["link_cat"];
displaycat.DataBind();
}

}

------------------------------------------

Here is the item databound I tried to generate the count

------------------------------------------

void displaycat_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType==ListItemType.Item)
{
DataRow[] drows = getthelinks.DataSource as DataRow[];
int count=0;
foreach (DataRow dr in drows)
{
count ++;
}
Literal thecount = e.Item.Controls[2] as Literal;
thecount.Text=count.ToString();
}

}

-----------------------------------------


Thanks!

Jeff
 
Hmmm that didn't seem to work I guess since i'm kind of a newbie I'm
gonna need someone to spell it out... so based on this

------------------------------------------------

private void getlinks()
{
string connectionInfo =
ConfigurationSettings.AppSettings["ConnectionString"];
using(SqlConnection connection = new
SqlConnection(connectionInfo))

{
connection.Open();
SqlDataAdapter da = new
SqlDataAdapter("EXECUTE
sp_getlinkcat",connection);
DataSet ds = new DataSet();
da.Fill(ds,"link_cat");
SqlDataAdapter da2 = new
SqlDataAdapter("EXECUTE sp_getlinks"
,connection);
da2.Fill(ds,"links");
DataRelation drel;
drel = new
DataRelation("myrelation",ds.Tables["link_cat"].Columns["cat_id"],ds.Tables["links"].Columns["cat_id"]);
ds.Relations.Add(drel);


//thecount.Text=ds.Tables["links"].ChildRelations.Count.ToString();
displaycat.DataSource =
ds.Tables["link_cat"];
displaycat.DataBind();
}

}

------------------------------------------

how do i get the row count for the child rows (the number of links in
the "links" table that are related to the cat_id in the "link_cat"
table) and stick it into a label control inside of the child repeater?

Here is the .ascx code too in case you wanted to see it

-----------------------------------------
<asp:Repeater id="displaycat" runat="server">
<ItemTemplate>
<div id="newsentry">
<p class="linkcat">
<ul>
<li class="linkcat"><strong><a
href="javascript:toggleLayer('linklist<%#DataBinder.Eval(Container.DataItem,
"cat_id")%>')"><IMG SRC="/puddin/images/plus.gif"
border="0"><%#DataBinder.Eval(Container.DataItem,
"cat_name")%></a>(<asp:Label ID="thecount" Runat="server" />)</strong>
<div id='linklist<%#DataBinder.Eval(Container.DataItem,
"cat_id")%>' class="hidden linklist">
<ul>
<asp:repeater id="getthelinks" datasource='<%#
((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("myrelation")
%>' runat="server">

<itemtemplate>

<li><a target="_blank" href='<%#
DataBinder.Eval(Container.DataItem, "[\"link_url\"]")%>'><%#
DataBinder.Eval(Container.DataItem, "[\"link_text\"]")%></a></li>

</itemtemplate>
</asp:repeater>
</ul>
</div>
</li>
</ul>
</p>
</div>

</ItemTemplate>
</asp:Repeater>
 
Hi Jeff!

Try adding this code after ds.Relations.Add(drel)...

foreach (DataRow dr in ds.Tables[0].Rows)

{

Response.Write(dr[0].ToString() + " - " + dr[1].ToString() + "<br />");

Response.Write("Child Count = " + dr.GetChildRows("myrelation").Length +
"<p />");

}

HTH,
 
okay that printed out the proper totals, but when i try to populate the
<asp:Label id="thecount"> with the count it tells me that "Object
reference not set to an instance of an object." even though i have it
decared above Page_Load as

protected System.Web.UI.WebControls.Label thecount;

let me provide some better explanation of what I'm trying to do, if you
go here

http://www.skatemag.com/puddin/?steeze=friends I have a page setup for
our "friends" links. Broken down by category. The categories are in the
first repeater, and the nested repeater contains the links for those
categories. I want to be able to put the number of links in each
category, next to the category title.

What you just gave me writes the counts in the top left corner of the
page, I cannot seem to assign the counts to a label for each iteration
of the child repeater.

thanks so much for your help so far!
 
actually, i finally figured out an easier way to do it, I did it in the
stored procedure that grabs the categories like this...

SELECT link_cat.cat_id,cat_name,COUNT(links.cat_id) as linkcount FROM
link_cat LEFT JOIN links ON link_cat.cat_id=links.cat_id GROUP BY
link_cat.cat_id,cat_name

then i just inserted the count as
<%#DataBinder.Eval(Container.DataItem, "linkcount")%>
 
Nice site Jeff! I went home after I sent my last reply yesterday so I only
had the chance to look at it just now.
 
Back
Top