locked
Row Total Missing In Email Why RRS feed

  • Question

  • User-807418713 posted

    Hello

    I used this on my aspx page

    https://forums.asp.net/post/6311807.aspx

    I have one button in that i have this below code to send email

            StringWriter stringWrite = new StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
            EPANEL.RenderControl(htmlWrite);
            string htmlStr = stringWrite.ToString();
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("a@xxxxxxxxxx.com");
            msg.To.Add("b@yyyyyyyyy.com");
           
    
            msg.Subject = "Daily Report As On:" + TextBox1.Text + " - " + TextBox2.Text;
            msg.Body = htmlStr;
            msg.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "xxxxxxxxxxxxxxx.com";
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential("abcdef@xxxxxxxxxxxx.com", "test@123");
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(msg);
            msg.Dispose();
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email Sent.. Please Check...');", true);

    This sending emails without total & other total  in email 

    How to get total & other total as well in email

    How to solve this

    Thanking You

    Monday, July 13, 2020 4:00 PM

Answers

  • User288213138 posted

    Hi Gopi.MCA,

    This sending emails without total & other total  in email 

    How to get total & other total as well in email

    That is because after clicking the button, the page will postback, so the total data back is lost.

    You can try to use the OnPreRender event in button click event.

    <asp:Button ID="Button1" runat="server" Text="Button" OnPreRender="Button1_PreRender" />
    
    protected void Button1_PreRender(object sender, EventArgs e)
            { 
    //your code
    }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 15, 2020 9:47 AM

All replies

  • User475983607 posted

    There is nothing in the code that creates a total.  Perhaps you are try basic debugging?  Set a break point and single step through your code to find where you made the coding error?

    Monday, July 13, 2020 4:11 PM
  • User288213138 posted

    Hi Gopi.MCA,

    This sending emails without total & other total  in email 

    According to your description, I cannot reproduce your problem. can you post your complete code?

    EPANEL.RenderControl(htmlWrite);

    And what is EPANEL?

    Best regards,

    Sam

    Tuesday, July 14, 2020 7:00 AM
  • User-807418713 posted

    Hi

    This my code

     <asp:Panel ID="EPANEL" runat="server"> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCreated="OnRowCreated" 
                    OnRowDataBound="GridView1_RowDataBound" ShowFooter="true">
                    <Columns>
                        <asp:BoundField DataField="Card_No" HeaderText="Card No" />
                        <asp:BoundField DataField="Day" HeaderText="Day" />
                        <asp:BoundField DataField="Dates" HeaderText="Dates" />
                        <asp:BoundField DataField="ItemName" HeaderText="ItemName" />
                        <asp:BoundField DataField="Qty" HeaderText="Qty" />
                        <asp:BoundField DataField="Value" HeaderText="Value" />
                    </Columns>
                </asp:GridView>
    </asp:panel>

    This code behind code

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
    
            private void BindData()
            {
    
                string sql = "select [Card_No],[Day],replace(CONVERT(varchar(50),Dates,106),' ','-') as Dates,ItemName,Qty,Value from AAAAAA order by [Card_No] ";
    
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                con.Open();
                SqlDataAdapter ad = new SqlDataAdapter(sql, con);
                DataTable dt = new DataTable();
                ad.Fill(dt);
                int rcount = dt.Rows.Count;
    
                
                for (int i = 0; i < rcount; i++)
                {
                    if (!checkdtRepeat(dt.Rows[i][0].ToString(), dt))
                    {
                        DataRow newRow = dt.NewRow();
                        newRow.ItemArray = dt.Rows[i].ItemArray;
                        dt.Rows.Remove(dt.Rows[i]);
                        dt.Rows.InsertAt(newRow, dt.Rows.Count + 1);
                        i -= 1;
                        rcount -= 1;
                    }
                }
    
                GridView1.DataSource = dt;
                GridView1.DataBind();
    
                AddGrandTotal();
    
    
    
            }
    
            string currentNo = "";
            int subTotalQyt = 0;
            int subTotalValue = 0;
            int subTotalRowIndex = 0;
    
            
            protected void OnRowCreated(object sender, GridViewRowEventArgs e)
            {
                subTotalQyt = 0;
                subTotalValue = 0;
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
                    string CardNo = dt.Rows[e.Row.RowIndex]["Card_No"].ToString();
                    if (CardNo != currentNo)
                    {
                        if (e.Row.RowIndex > 0)
                        {
                            for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
                            {
                                //if (checkRepeat(GridView1.Rows[i].Cells[0].Text))
                                //{
                                subTotalQyt += Convert.ToInt32(GridView1.Rows[i].Cells[4].Text);
                                subTotalValue += Convert.ToInt32(GridView1.Rows[i].Cells[5].Text);
                                //}
                            }
                            if (checkRepeat(GridView1.Rows[subTotalRowIndex].Cells[0].Text))
                            {
                                this.AddTotalRow("Total", subTotalQyt.ToString(), subTotalValue.ToString());
                            }
                            subTotalRowIndex = e.Row.RowIndex;
                        }
                        currentNo = CardNo;
    
                    }
    
                    
                } 
            }
    
            private void AddTotalRow(string labelText, string Qty, string value)
            {
                GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
                row.Cells.AddRange(new TableCell[6] { new TableCell (), new TableCell (),new TableCell (),
                                            new TableCell() { Text = "<strong>"+labelText+"</strong>"},
                                            new TableCell() { Text = "<strong>"+Qty+"</strong>"},new TableCell() { Text = "<strong>"+value+"</strong>"} });
                GridView1.Controls[0].Controls.Add(row);
            }
    
            protected bool checkRepeat(string celltext)
            {
                int flag = 0;
                foreach (GridViewRow row in GridView1.Rows)
                {
                    string No = row.Cells[0].Text;
                    if (No == celltext)
                    {
                        flag += 1;
                    }
                }
                if (flag > 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            protected bool checkdtRepeat(string celltext, DataTable dt)
            {
                int flag = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string No = dt.Rows[i][0].ToString();
                    if (No == celltext)
                    {
                        flag += 1;
                    }
                }
                if (flag > 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            //The other Total is always added after the last row, so call the DataBound event handler 
            protected void AddOthersTotal()
            {
                DataTable dt = (DataTable)GridView1.DataSource;
    
                int rcount = dt.Rows.Count;
                int othersTotalQyt = 0;
                int othersTotalValue = 0;
                int othersCount = 0;
                for (int i = 0; i < rcount; i++)
                {
                    if (!checkdtRepeat(dt.Rows[i][0].ToString(), dt))
                    {
                        othersCount++;
                        othersTotalQyt += Convert.ToInt32(dt.Rows[i]["Qty"]);
                        othersTotalValue += Convert.ToInt32(dt.Rows[i]["Value"]);
                    }
                }
    
                if (othersCount != 0)
                {
                    AddTotalRow("Others Total", othersTotalQyt.ToString(), othersTotalValue.ToString());
                }
            }
    
            //Add others total at the last row
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
                    // last row
                    if (e.Row.RowIndex == dt.Rows.Count - 1)
                    {
                        AddOthersTotal();
                    }
                }
            }
    
            private void AddGrandTotal()
            {
                DataTable dt = (DataTable)GridView1.DataSource;
    
                int grandTotalQyt = 0;
                int grandTotalValue = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //Calculate grand total before make any change
                    grandTotalQyt += Convert.ToInt32(dt.Rows[i]["Qty"]);
                    grandTotalValue += Convert.ToInt32(dt.Rows[i]["Value"]);
                }
    
                //Add Grand Total
                GridView1.FooterRow.Cells[3].Text = "<strong style='color: blue'>Grand Total</strong>";
                GridView1.FooterRow.Cells[4].Text = "<strong style = 'color: blue' >"+ grandTotalQyt.ToString() + "</strong>";
                GridView1.FooterRow.Cells[5].Text = "<strong style = 'color: blue' >" + grandTotalValue.ToString() + "</strong>";
    
    
            }

    Thanking You

    Tuesday, July 14, 2020 7:13 AM
  • User288213138 posted

    Hi Gopi.MCA,

    This sending emails without total & other total  in email 

    How to get total & other total as well in email

    That is because after clicking the button, the page will postback, so the total data back is lost.

    You can try to use the OnPreRender event in button click event.

    <asp:Button ID="Button1" runat="server" Text="Button" OnPreRender="Button1_PreRender" />
    
    protected void Button1_PreRender(object sender, EventArgs e)
            { 
    //your code
    }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 15, 2020 9:47 AM
  • User-807418713 posted

    Hello

    This tried but before clicking button it automatically sending email on every postback or page load etc

    how to send only on button click..

    Thanking You

    Thursday, July 16, 2020 8:36 AM
  • User475983607 posted

    This tried but before clicking button it automatically sending email on every postback or page load etc

    how to send only on button click..

    Seriously?  You need to build the GridView before sending the email.  You might have to refactor your code and use the Visual Studio debugger.

    Thursday, July 16, 2020 10:19 AM