Solved

Sumtotal in Datalist

Posted on 2006-11-02
5
2,394 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
  • 2
  • 2
5 Comments
 
LVL 11

Assisted Solution

by:ethoths
ethoths earned 100 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 24

Accepted Solution

by:
DBAduck - Ben Miller earned 400 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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.…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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