Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sumtotal in Datalist

Posted on 2006-11-02
5
Medium Priority
?
2,448 Views
Last Modified: 2008-02-01
Hi Experts!

I was hoping you could help with an issue, I have been struggling with.

I have a datalist with several invoice sums.
I would like Sum Total in the Footer of the Datalist.
Unfortunately, I'm not able to make it work. I do not get any errormessage, but the Sum total does not show.
Could someone be kind enough to take a look at this code.

Sub bind()
        Dim dt As DataTable
        Dim dr As DataRow
        Dim SumInvoiced As Integer

        dt = New DataTable()
        dt.Columns.Add(New DataColumn("ContryCode", GetType(String)))
        dt.Columns.Add(New DataColumn("CompanyName", GetType(String)))
        dt.Columns.Add(New DataColumn("TotalInvoiced", GetType(String)))
        dt.Columns.Add(New DataColumn("TotInvoiced", GetType(String)))

        Dim mySqlConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
        Dim MySqlCmd As SqlCommand = New SqlCommand("Select ContryCode, CompanyName from Tab_Campaigns WHERE ContryCode=" + Request.QueryString("KlientNr"), mySqlConn)
        MySqlCmd.CommandType = CommandType.Text
        mySqlConn.Open()
        Dim Reader7 As SqlDataReader = MySqlCmd.ExecuteReader()
        While Reader7.Read()
            dr = dt.NewRow()
            dr(0) = getKlientNr(Reader7("ContryCode"))
            dr(1) = Reader7("CompanyName")
            dr(2) = TotalInvoiced(Reader7("CampaignCode"))
           
            SumInvoiced += TotalInvoiced(Reader7("CampaignCode"))
            dr(3) = TotInvoiced
           
           dt.Rows.Add(dr)
        End While

        Reader7.Close()
        mySqlConn.Close()
        dbList.DataSource = New DataView(dt)
        dbList.DataBind()
end sub

###Datalist

<asp:datalist id="dbList" runat="server" Width="985px" ShowFooter="true" RepeatLayout="Flow" RepeatDirection="Horizontal" RepeatColumns="2" BorderColor="Black" BorderStyle="None" CellSpacing="1" BackColor="White" CellPadding="3" BorderWidth="1px" GridLines="Both">
         <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
      <HeaderTemplate>
            <TABLE id="AutoNumber5" style="BORDER-COLLAPSE: collapse" borderColor="#111111" cellSpacing="0" cellPadding="0" width="100%" border="0">
               <TR>
                                       <td width="10%">Contry</td>
                  <td width="10%">Company Name</td>
                  <td width="10%">Total invoiced</td>
              </TR>
            </TABLE>
      </HeaderTemplate>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
      <ItemStyle HorizontalAlign="Justify" ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
            <ItemTemplate>
                  <TABLE id="AutoNumber6" style="BORDER-COLLAPSE: collapse" borderColor="#111111" cellSpacing="0" cellPadding="0" width="100%" border="0">
                               <TR align="center">
                  <TD width="10%"><SPAN style="FONT-SIZE: 9pt"><FONT face="Arial" color="#666666"><%# DataBinder.Eval(Container.DataItem, "ContryCode") %>&nbsp;</FONT></SPAN></TD>
                  <TD width="10%"><SPAN style="FONT-SIZE: 9pt"><FONT face="Arial" color="#666666"><%# DataBinder.Eval(Container.DataItem, "CompanyName") %>&nbsp;</FONT></SPAN></TD>
                  <TD width="10%"><SPAN style="FONT-SIZE: 9pt"><FONT face="Arial" color="#666666"><%# DataBinder.Eval(Container.DataItem, "TotalInvoiced") %>&nbsp;</FONT></SPAN></TD>
                  </TR>
                  <TR>
                  <TD width="50%" colSpan="2">&nbsp;</TD>
                  </TR>
                  </TABLE>
            </ItemTemplate>
            <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="Gray"></HeaderStyle>
                <FooterTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TotInvoiced") %>'></asp:Label>
                </FooterTemplate>
</asp:datalist>                        
0
Comment
Question by:gfk76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 11

Assisted Solution

by:ethoths
ethoths earned 400 total points
ID: 17859421
Surley
<asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TotInvoiced") %>'></asp:Label>


Should be
<asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SumInvoiced") %>'></asp:Label>

also, since the sumInvouiced is a calcualted field it cannot be databound in this way. Why not simply

<asp:Label ID="Label2" runat="server" Text='<%=SumInvoiced %>'></asp:Label>

0
 

Author Comment

by:gfk76
ID: 17859627
I tried that, but still nothing.
No value what so ever in the label....


0
 
LVL 25

Accepted Solution

by:
DBAduck - Ben Miller earned 1600 total points
ID: 17860260
So you can do a couple of things.  It depends on how you want to code it.

1) You can use SumInvoiced with the <%= SumInvoiced %>, but you have to put the Dim SumInvoiced as Integer outside of the bind() because SumInvoiced becomes a Local Variable to that procedure and will contain nothing outside, especially for the DataList.

2) You can just pull the data into a DataTable and use the Compute function to get the total, and use it like SumInvoiced (kind of the same thing as you are doing above, just different steps)

3) You can use ItemDataBound on the DataList and capture the Data and sum it up, and when it comes to the ItemType = ListItemType.Footer, then you could insert it into the footer.

<asp:DataList id="something" OnItemDataBound="dataList_ItemBound" ..... />

Dim SumInvoiced as Integer

Protected Sub dataList_ItemBound(sender as Object, e as DataListItemEventArgs)

    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        SumInvoiced += CInt(CType(e.Item.DataItem, DataRow)("TotalInvoiced"))
    ElseIf e.Item.ItemType = ListItemType.Footer Then
        Dim lbl as Label
        lbl = CType(e.Item.FindControl("Label2"), Label)
        lbl.Text = SumInvoiced.ToString()
    End If
End Sub

Hope that helps.  
0
 
LVL 11

Expert Comment

by:ethoths
ID: 17860487
Yes dbaduck is right. The SumInvoiced need to be a class level variable. Dim it at the class level and everything will be fine.

dbaducks other suggestions will also work but they take a bit more effort since you're already gone this far.
0
 

Author Comment

by:gfk76
ID: 17860861
Thanks guys!
I'll be abel to sleep tonight!

Putting SumInvoiced outside the sub was the trick!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question