Calculate percentage only if all Column is not null using C# in ASP.Net

Last Reply on May 08, 2018 06:32 AM By kalpesh

Posted on May 08, 2018 04:53 AM

Below is the structure of my datatable

Id Name Maths Physics Chemistry Score%
1  xyz   10      2               no need
2  abc   8       8        8      24/30*100
3  def   1                       no need   

If we see the datatable i have three records where i need to calculate percentage of score for only one record in my datatable because if a student passes all the subjects only i need to calculate the percentage if he or she fails in any ano of the subject there is no need to display the percentage how can i do this               

I had tried the below code 

                    foreach (DataRow dr1 in dt.Rows)
                    {
                        if (!DBNull.Value.Equals(dr1["Maths"]) && !DBNull.Value.Equals(dr1["Physics"]) && !DBNull.Value.Equals(dr1["Chemistry"]))
                        {
                            double m1 = Convert.ToDouble(dr1["Maths"]);
                            double m2 = Convert.ToDouble(dr1["Physics"]);
                            double m3 = Convert.ToDouble(dr1["Chemistry"]);
                            double totalscore = m1+m2+m3;
                            // Set the three values in the new table now.
                            int maximum = 40;
                            double total = (totalscore / maximum) * 100;
                            dr["Score"] = total;
                        }
                    }

here i am validating for not getting the datatable cell value empty but still even though cell value is empty it is going into the condition and i am getting error how can i resolve this

Posted on May 08, 2018 06:14 AM

Dear @kavithav,

Please try below

                foreach (DataRow dr1 in dt.Rows)
                {
                    if (!dr1.IsNull("Maths") && !dr1.IsNull("Physics") && !dr1.IsNull("Chemistry"))
                    {
                        double m1 = Convert.ToDouble(dr1["Maths"]);
                        double m2 = Convert.ToDouble(dr1["Physics"]);
                        double m3 = Convert.ToDouble(dr1["Chemistry"]);
                        double totalscore = m1 + m2 + m3;
                        // Set the three values in the new table now.
                        int maximum = 40;
                        double total = (totalscore / maximum) * 100;
                        dr["Score"] = total;
                    }
                }

 


Posted on May 08, 2018 06:32 AM Modified on on May 08, 2018 06:32 AM

Refer below code for your reference.

HTML

        <asp:GridView ID="gvGridMarks" runat="server">
        </asp:GridView>
        <br />
        <br />
        <asp:GridView ID="gvGridMarkwithScore" runat="server">
        </asp:GridView>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Id", typeof(int)));
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        dt.Columns.Add(new DataColumn("Maths", typeof(string)));
        dt.Columns.Add(new DataColumn("Physics", typeof(string)));
        dt.Columns.Add(new DataColumn("Chemistry", typeof(string)));
        dt.Columns.Add(new DataColumn("Score", typeof(string)));
        dt.Rows.Add(1, "xyz", "10", "2", "", "");
        dt.Rows.Add(2, "abc", "8", "8", "8", "");
        dt.Rows.Add(3, "def", "1", "", "", "");

        gvGridMarks.DataSource = dt;
        gvGridMarks.DataBind();

        foreach (DataRow dr in dt.Rows)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(dr["Maths"])) && !string.IsNullOrEmpty(Convert.ToString(dr["Physics"])) && !string.IsNullOrEmpty(Convert.ToString(dr["Chemistry"])))
            {
                double m1 = Convert.ToDouble(dr["Maths"]);
                double m2 = Convert.ToDouble(dr["Physics"]);
                double m3 = Convert.ToDouble(dr["Chemistry"]);
                double totalscore = m1 + m2 + m3;
                // Set the three values in the new table now.
                int maximum = 30;
                double total =  Math.Round(((totalscore / maximum) * 100),2);
                dr["Score"] = total;
            }
        }
        gvGridMarkwithScore.DataSource = dt;
        gvGridMarkwithScore.DataBind();
    }
}