473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to get a count on a data relation?

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

Nov 19 '05 #1
6 2792
Hi Jeff!

Have you tried DataRow.GetChildRows().Length?

HTH,

"tfsmag" <tf****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Nov 19 '05 #2
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<%#DataBinde r.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>
--------------------------------------------------------------

Thanks,
Jeff

Nov 19 '05 #3
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,
"tfsmag" <tf****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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<%#DataBinde r.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>
--------------------------------------------------------------

Thanks,
Jeff

Nov 19 '05 #4
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!

Nov 19 '05 #5
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")%>

Nov 19 '05 #6
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.

"tfsmag" <tf****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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!

Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Bing Wu | last post by:
Hi, Has anyone got problem with this on large table. It seems it take ages to return the result ( half an hour :-( ). SELECT COUNT(*) FROM COORDINATE A strange thing is the tempory space...
11
by: deko | last post by:
If I release a new version of my mbd (in mde format) and users want to upgrade - how do they migrate their data? For example, if the original was released as data1.mde and then I release...
2
by: Alpha | last post by:
I have a window application. In one of the form, a datagrid has a dataview as its datasource. Initial filtering result would give the datavew 3 items. When I double click on the datagrid to edit...
11
by: Thomas A | last post by:
Hi, I fill a datgrid with data from a xml document, it works fine But.... Now I will to filter the data to the grid so only the data shows from the criteria that I set. My code now is very...
6
by: Peter W Johnson | last post by:
Hi Guys, I am trying to display a datagrid containing two related datasets. I have filled and related them on MemberID with the following code:- Dim myconnection As Odbc.OdbcConnection
1
by: andrea | last post by:
I need to get the child of parent/child relation and I've some perplexity about the best method to use. The classic (select count(*) id from table where id = a.parentid) nested into a select...
3
by: awebguynow | last post by:
with an xpath expr: step1/step2 I'm trying to get the position() of step2, using the qualifying predicate. I've seen some suggestions, of ways to do this, using count() instead, but I'm not...
5
by: sutphinwb | last post by:
Hi - This could be a simple question. When I relate two tables in a datasetet, how do I get that relation to show up in a GridView? The only way I've done it, is to create a separate table in the...
1
by: Ginger Estherskip | last post by:
I'm setting up a dataset using the GUI to have a Parent Data Table and a few Child Data Tables. I then have a form that uses that dataset, and I can fill the appropriate datatables et. al., but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.