Display ASP.Net DataList Grand Total in footer using C# and VB.Net

Last Reply 27 days ago By pandeyism

Posted 27 days ago

I want total in datalist lable but i am not getting total. the code is below giving me a error of conversation fail.

 <asp:DataList ID="dlCustomers" runat="server" RepeatColumns="2" CellPadding="5"  Font-Size="20px">
    <ItemTemplate>
        <table class="table" border="0" style="width:250px;height:350px;font-size:20px" >
            <tr>
                <th colspan="3" align="left">
                    <asp:CheckBox ID="chkRow" runat="server" Font-Bold="true" Text='<%# Eval("Name")%>'
                        AutoPostBack="true" OnCheckedChanged="chkRow_CheckedChanged" />
                </th>
            </tr>
            <tr>
                <td>Farmer Id:</td>
                <td><asp:Label Text='<%#Eval("Id")%>' runat="server" ID="lblId" /></td>
            </tr>
            <tr>
                <td>Name:</td>
                <td><asp:Label Text='<%#Eval("Name")%>' runat="server" ID="Name" /></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><asp:Label Text='<%#Eval("Email")%>' runat="server" ID="Email" /></td>
            </tr>
            <tr>
                <td>State:</td>
                <td><asp:Label Text='<%#Eval("State")%>' runat="server" ID="State" /></td>
            </tr>
               <tr>
                <td>District:</td>
                <td><asp:Label Text='<%#Eval("District")%>' runat="server" ID="District" /></td>
            </tr>
            <tr>
                <td>Address:</td>
                <td><asp:Label Text='<%#Eval("Address")%>' runat="server" ID="Address" /></td>
            </tr>
            <tr>
                <td>Contact:</td>
                <td><asp:Label Text='<%#Eval("Contact")%>' runat="server" ID="Contact" /></td>
            </tr>
             <tr>
                <td>Potato Quantity:</td>
                <td><asp:Label Text='<%#Eval("PQuantity")%>' runat="server" ID="PQuantity" /></td>
            </tr>
             <tr>
                <td>Company Potato Quantity per Ton:</td>
                <td><asp:Label Text='<%#Eval("PoQuantity")%>' runat="server" ID="PoQuantity" /></td>
            </tr>
              <tr>
                <td>Total:</td>
                <td>
                    <asp:Label Text='<%#Eval("Total")%>' runat="server" ID="Total" />
                     <asp:Label ID="lblGrandTotal" runat="server" />
                </td>
            </tr>
        </table>
        <br />
        <asp:Label runat="server" ID="lblMsg" ForeColor="Red" />
    </ItemTemplate>
</asp:DataList>

 

    public partial class WebForm27 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                con.Open();
                String abc = Session["Com"].ToString();
                SqlCommand cmd1 = new SqlCommand("SELECT AddRaw_199.Id,AddRaw_199.Name,AddRaw_199.Contact,AddRaw_199.District,AddRaw_199.Address,AddRaw_199.State,AddRaw_199.Status,AddRaw_199.Email,AddRaw_199.PoQuantity,Company_Data199.PQuantity,Total=AddRaw_199.PoQuantity*Company_Data199.PQuantity FROM AddRaw_199 INNER JOIN Company_Data199 ON AddRaw_199.Id = Company_Data199.Id where AddRaw_199.Status = 0 and AddRaw_199.Company_Email = Company_Data199.Email and PoQuantity != 0; ", con);
                SqlDataReader dr = cmd1.ExecuteReader();
                dlCustomers.DataSource = dr;
                dlCustomers.CssClass = "table table-bordered table-highlight";

                dlCustomers.DataBind();
                con.Close();
            }

        }

        protected void chkRow_CheckedChanged(object sender, EventArgs e)
        {
            decimal total = 0;
            foreach (DataListItem item in dlCustomers.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("chkRow");
                Label id = (Label)item.FindControl("lblId");
                Label Name = (Label)item.FindControl("Name");
                Label Email = (Label)item.FindControl("Email");
                Label Contact = (Label)item.FindControl("Contact");
                Label Address = (Label)item.FindControl("Address");
                Label State = (Label)item.FindControl("State");
                Label District = (Label)item.FindControl("District");
                Label PQuantity = (Label)item.FindControl("PQuantity");
                Label PoQuantity = (Label)item.FindControl("PoQuantity");
                Label Total = (Label)item.FindControl("Total");
                total += Convert.ToDecimal(Total);
                Label message = (Label)item.FindControl("lblMsg");
                if (chk.Checked == true)
                {
                    message.Text = "Id = " + id.Text + "<br/>" + "Name = " + Name.Text + "Email = " + Email.Text + "Contact = " + Contact.Text + "Address = " + Address.Text + "State = " + State.Text + "District = " + District.Text + "PQuantity = " + PQuantity.Text + "PoQuantity = " + PoQuantity.Text + "Total = " + Total.Text;

                }
                else if (chk.Checked == false)
                {
                    message.Text = string.Empty;
                }
            }
            (dlCustomers.FindControl("lblGrandTotal") as Label).Text = total.ToString();
        }

Error:

Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.IConvertible'. 

Posted 27 days ago Modified on 26 days ago

Hi VinitaAkha,

Refer below sample.

HTML

<asp:DataList ID="dlCustomers" runat="server" RepeatColumns="2" CellPadding="5" Font-Size="20px"
    OnItemDataBound="dlCustomers_ItemDataBound">
    <ItemTemplate>
        <table class="table" border="0" style="width: 250px; height: 350px; font-size: 20px">
            <tr>
                <th colspan="3" align="left">
                    <asp:CheckBox ID="chkRow" runat="server" Font-Bold="true" Text='<%# Eval("Name")%>'
                        AutoPostBack="true" OnCheckedChanged="chkRow_CheckedChanged" />
                </th>
            </tr>
            <tr>
                <td>
                    Farmer Id:
                </td>
                <td>
                    <asp:Label Text='<%#Eval("Id")%>' runat="server" ID="lblId" />
                </td>
            </tr>
            <tr>
                <td>
                    Name:
                </td>
                <td>
                    <asp:Label Text='<%#Eval("Name")%>' runat="server" ID="Name" />
                </td>
            </tr>
            <tr>
                <td>
                    Potato Quantity:
                </td>
                <td>
                    <asp:Label Text='<%#Eval("PQuantity")%>' runat="server" ID="PQuantity" />
                </td>
            </tr>
            <tr>
                <td>
                    Company Potato Quantity per Ton:
                </td>
                <td>
                    <asp:Label Text='<%#Eval("PoQuantity")%>' runat="server" ID="PoQuantity" />
                </td>
            </tr>
            <tr>
                <td>
                    Total:
                </td>
                <td>
                    <asp:Label runat="server" ID="Total" />
                    <asp:Label ID="lblGrandTotal" runat="server" />
                </td>
            </tr>
        </table>
        <br />
        <asp:Label runat="server" ID="lblMsg" ForeColor="Red" />
    </ItemTemplate>
</asp:DataList>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("PQuantity", typeof(int)), new DataColumn("PoQuantity", typeof(int)) });
        dt.Rows.Add(1, "Ross", 10, 20);
        dt.Rows.Add(2, "Monica", 20, 30);
        dlCustomers.DataSource = dt;
        dlCustomers.DataBind();
    }
}

protected void chkRow_CheckedChanged(object sender, EventArgs e)
{
    decimal total = 0;
    CheckBox chk = (sender) as CheckBox;
    DataListItem item = (DataListItem)chk.NamingContainer;
    Label id = (Label)item.FindControl("lblId");
    Label Name = (Label)item.FindControl("Name");
    Label PQuantity = (Label)item.FindControl("PQuantity");
    Label PoQuantity = (Label)item.FindControl("PoQuantity");
    Label Total = (Label)item.FindControl("Total");
    total += Convert.ToDecimal(PQuantity.Text) + Convert.ToDecimal(PoQuantity.Text);
    Label message = (Label)item.FindControl("lblMsg");
    if (chk.Checked == true)
    {
        message.Text = "Id = " + id.Text + "<br/>" + "Name = " + Name.Text + "PQuantity = " + PQuantity.Text + "PoQuantity = " + PoQuantity.Text + "Total = " + Total.Text;
    }
    else if (chk.Checked == false)
    {
        message.Text = string.Empty;
    }
    (item.FindControl("lblGrandTotal") as Label).Text = total.ToString();
}

protected void dlCustomers_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Label PQuantity = (Label)e.Item.FindControl("PQuantity");
        Label PoQuantity = (Label)e.Item.FindControl("PoQuantity");
        Label Total = (Label)e.Item.FindControl("Total");
        Total.Text = (Convert.ToDecimal(PQuantity.Text) + Convert.ToDecimal(PoQuantity.Text)).ToString();
    }
}

VB.Net

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Dim dt As DataTable = New DataTable()
            dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("PQuantity", GetType(Integer)), New DataColumn("PoQuantity", GetType(Integer))})
            dt.Rows.Add(1, "Ross", 10, 20)
            dt.Rows.Add(2, "Monica", 20, 30)
            dlCustomers.DataSource = dt
            dlCustomers.DataBind()
        End If
    End Sub

    Protected Sub chkRow_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim total As Decimal = 0
        Dim chk As CheckBox = TryCast((sender), CheckBox)
        Dim item As DataListItem = CType(chk.NamingContainer, DataListItem)
        Dim id As Label = CType(item.FindControl("lblId"), Label)
        Dim Name As Label = CType(item.FindControl("Name"), Label)
        Dim PQuantity As Label = CType(item.FindControl("PQuantity"), Label)
        Dim PoQuantity As Label = CType(item.FindControl("PoQuantity"), Label)
        Dim Total As Label = CType(item.FindControl("Total"), Label)
        total += Convert.ToDecimal(PQuantity.Text) + Convert.ToDecimal(PoQuantity.Text)
        Dim message As Label = CType(item.FindControl("lblMsg"), Label)

        If chk.Checked = True Then
            message.Text = "Id = " & id.Text & "<br/>" & "Name = " + Name.Text & "PQuantity = " + PQuantity.Text & "PoQuantity = " + PoQuantity.Text & "Total = " + Total.Text
        ElseIf chk.Checked = False Then
            message.Text = String.Empty
        End If

        (TryCast(item.FindControl("lblGrandTotal"), Label)).Text = total.ToString()
    End Sub

    Protected Sub dlCustomers_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim PQuantity As Label = CType(e.Item.FindControl("PQuantity"), Label)
            Dim PoQuantity As Label = CType(e.Item.FindControl("PoQuantity"), Label)
            Dim Total As Label = CType(e.Item.FindControl("Total"), Label)
            Total.Text = (Convert.ToDecimal(PQuantity.Text) + Convert.ToDecimal(PoQuantity.Text)).ToString()
        End If
    End Sub

Screenshot