Solved

Sumtotal in Datalist

Posted on 2006-11-02
5
2,366 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys!
I'll be abel to sleep tonight!

Putting SumInvoiced outside the sub was the trick!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now