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

Last Reply 7 months ago By pandeyism

Posted 7 months 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'. 

This question does not have replies that have been liked.