0

I am working on an asp.net project, it takes values from the front end, stores them as string in a matrix. When the matrix size is greater than 5 * 5, it keep returns an InvalidCastException (It works fine on 5 * 5 matrix and under).

The code is attached below, with a screen shot of Exception:

enter image description here

public partial class WebForm1 : System.Web.UI.Page
{
    public DataSet relmatrix = new DataSet();
    public DataTable rt = new DataTable();



    public Double[] inconsistency;
    public int nodersnum;
    public string strrel;



    protected void Button1_Click(object sender, EventArgs e)
    {

        nodersnum = Convert.ToInt16(count.Text);

        switch (nodersnum)
        {
            case 1:
                break;
            case 2:
                { strrel = RelationAB2.Text; }
                break;
            case 3:
                { strrel = RelationAB3.Text + " " + RelationAC3.Text + " " + RelationBC3.Text; }
                break;
            case 4:
                { strrel = RelationAB4.Text + " " + RelationAC4.Text + " " + RelationAD4.Text + " " + RelationBC4.Text + " " + RelationBD4.Text + " " + RelationCD4.Text; }
                break;
            case 5:
                { strrel = RelationAB5.Text + " " + RelationAC5.Text + " " + RelationAD5.Text + " " + RelationAE5.Text + " " + RelationBC5.Text + " " + RelationBD5.Text + " " + RelationBE5.Text + " " + RelationCD5.Text + " " + RelationCE5.Text + " " + RelationDE5.Text; }
                break;
            case 6:
                { strrel = RelationAB6.Text + " " + RelationAC6.Text + " " + RelationAD6.Text + " " + RelationAE6.Text + " " + RelationAF6.Text + " " + RelationBC6.Text + " " + RelationBD6.Text + " " + RelationBE6.Text + " " + RelationBF6.Text + " " + RelationCD6.Text + " " + RelationCE6.Text + " " + RelationCF6.Text + " " + RelationDE6.Text + " " + RelationDF6.Text + " " + RelationEF6.Text; }
                break;
            case 7:
                { strrel = RelationAB7.Text + " " + RelationAC7.Text + " " + RelationAD7.Text + " " + RelationAE7.Text + " " + RelationAF7.Text + " " + RelationAG7.Text + " " + RelationBC7.Text + " " + RelationBD7.Text + " " + RelationBE7.Text + " " + RelationBF7.Text + " " + RelationBG7.Text + " " + RelationCD7.Text + " " + RelationCE7.Text + " " + RelationCF7.Text + " " + RelationCG7.Text + " " + RelationDE7.Text + " " + RelationDF7.Text + " " + RelationDG7.Text + " " + RelationEF7.Text + " " + RelationEG7.Text + " " + RelationFG7.Text; }
                break;
            default:
                { strrel = ""; }
                break;
        }

        GenerateTable.generatetable(relmatrix, strrel, rt, nodersnum);

        Class1.generatePC(nodersnum, relmatrix);


        int num = 0;
        double maxincon = 0.0;
        int mi = 0, mj = 0, mk = 0;

        inconsistency = new Double[43] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };


        for (int i = 0; i < nodersnum - 2; i++)
        {
            for (int k = i + 1; k < nodersnum - 1; k++)
            {
                for (int j = k + 1; j < nodersnum; j++)
                {
                    if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
                    {
                        Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
                        Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix

                        //PROBLEM
                        Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix

                        inconsistency[num] = (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a)));//calculate the inconsistency value and store in the inconsistency array

                        //Get the biggest inconsistency number
                        if (inconsistency[num] >= maxincon)
                        {
                            maxincon = inconsistency[num];
                            mi = i;
                            mj = j;
                            mk = k;
                        }
                        num++;

                    }
                }
            }
        }

        Class1.sort(inconsistency);//sort inconsistency array

        while (inconsistency[0] > 0.3333333)
        {
            for (int i = 0; i < nodersnum - 2; i++)
            {
                for (int k = i + 1; k < nodersnum - 1; k++)
                {
                    for (int j = k + 1; j < nodersnum; j++)
                    {
                        if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
                        {
                            Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
                            Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix

                            // PROBLEM
                           Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix

                            if (inconsistency[0] == (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a))))//calculate the inconsistency value and store in the inconsistency array
                            {
                                if ((b * c) < a)
                                {
                                    double A = (b * c) / ((a + b + c) * (a + b + c));
                                    double B = (a + 2 * b * c) / (a + b + c);
                                    double C = b * c - a;
                                    double m = B * B - 4 * A * C;
                                    if (m < 0)
                                    {
                                        Console.Write("error");
                                        break;
                                    }
                                    else
                                    {
                                        double x1 = (-1 * B + System.Math.Sqrt(m)) / (2 * A);
                                        double x2 = (-1 * B - Math.Sqrt(m)) / (2 * A);
                                        if ((x1 > 0) && (x2 < 0))
                                        {
                                            b = (float)(b + (b * x1) / (a + b + c));
                                            c = (float)(c + (c * x1) / (a + b + c));
                                            a = (float)(a - (a * x1) / (a + b + c));
                                        }
                                        else if ((x1 < 0) && (x2 > 0))
                                        {
                                            b = (float)(b + (b * x2) / (a + b + c));
                                            c = (float)(c + (c * x2) / (a + b + c));
                                            a = (float)(a - (a * x2) / (a + b + c));
                                        }
                                        else if ((x1 > 0) && (x2 > 0))
                                        {
                                            double x = Class1.Min((float)x1, (float)x2);
                                            b = (float)(b + (b * x) / (a + b + c));
                                            c = (float)(c + (c * x) / (a + b + c));
                                            a = (float)(a - (a * x) / (a + b + c));
                                        }
                                        else if ((x1 < 0) && (x2 < 0))
                                        {
                                            break;
                                        }
                                    }
                                }
                                else if ((b * c) > a)
                                {
                                    double A = (b * c) / ((a + b + c) * (a + b + c));
                                    double B = -1 * (a + 2 * b * c) / (a + b + c);
                                    double C = b * c - a;
                                    double m = B * B - 4 * A * C;
                                    if (m < 0)
                                    {
                                        Console.Write("error");
                                        break;
                                    }
                                    else
                                    {
                                        double x1 = (-1 * B + Math.Sqrt(m)) / (2 * A);
                                        double x2 = (-1 * B - Math.Sqrt(m)) / (2 * A);
                                        if ((x1 > 0) && (x2 < 0))
                                        {
                                            b = (float)(b - (b * x1) / (a + b + c));
                                            c = (float)(c - (c * x1) / (a + b + c));
                                            a = (float)(a + (a * x1) / (a + b + c));
                                        }
                                        else if ((x1 < 0) && (x2 > 0))
                                        {
                                            b = (float)(b - (b * x2) / (a + b + c));
                                            c = (float)(c - (c * x2) / (a + b + c));
                                            a = (float)(a + (a * x2) / (a + b + c));
                                        }
                                        else if ((x1 > 0) && (x2 > 0))
                                        {
                                            double x = Class1.Min((float)x1, (float)x2);
                                            b = (float)(b - (b * x) / (a + b + c));
                                            c = (float)(c - (c * x) / (a + b + c));
                                            a = (float)(a + (a * x) / (a + b + c));
                                        }
                                        else if ((x1 < 0) && (x2 < 0))
                                        {
                                            break;
                                        }

                                    }
                                }
                            }
                            relmatrix.Tables["relTable"].Rows[i][j] = Convert.ToString(a);
                            relmatrix.Tables["relTable"].Rows[i][k] = Convert.ToString(b);
                            relmatrix.Tables["relTable"].Rows[k][j] = Convert.ToString(c);

                        }

                    }
                }
            }


            num = 0;
            maxincon = 0.0;
            mi = 0;
            mj = 0;
            mk = 0;
            for (int i = 0; i < nodersnum - 2; i++)
            {
                for (int k = i + 1; k < nodersnum - 1; k++)
                {
                    for (int j = k + 1; j < nodersnum; j++)
                    {
                        if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
                        {
                            Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
                            Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
                            Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
                            inconsistency[num] = (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a)));//calculate the inconsistency value and store in the inconsistency array

                            // Get the biggest inconsistency number
                            if (inconsistency[num] >= maxincon)
                            {
                                maxincon = inconsistency[num];
                                mi = i;
                                mj = j;
                                mk = k;
                            }
                            num++;

                        }
                    }
                }
            }
            //sort inconsistency array
            Class1.sort(inconsistency);
        }



        //Fill up the whole pairwise comparsion matrix, when row=col, the value =1, when row<col the vaule[col][row] is "1/"[col][row]
        //nodersnum is how many nodes, row is the matrix row, col is column of matrix
        for (int row = 0; row < nodersnum; row++)
        {
            for (int col = row; col < nodersnum; col++)
            {
                if (row < col)
                {
                    //set the the value of lower matrix
                    relmatrix.Tables["relTable"].Rows[col][row] = "1/" + relmatrix.Tables["relTable"].Rows[row][col];

                }
                //set the value of diagnoal 
                else if (row == col)
                {
                    relmatrix.Tables["relTable"].Rows[row][col] = "1";
                }
            }
        }


        //compute the weight of each element

        Double[] rowproduct;
        rowproduct = new Double[7] { 1, 1, 1, 1, 1, 1, 1 };

        for (int i = 0; i < nodersnum; i++)
        {
            for (int j = 0; j < nodersnum; j++)
            {
                if (i >= j)
                {
                    rowproduct[i] = rowproduct[i] / Convert.ToDouble(relmatrix.Tables["relTable"].Rows[j][i]);
                }
                else
                {
                    rowproduct[i] = rowproduct[i] * Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);
                }
            }
        }

        Double[] num1;
        num1 = new Double[7] { 0, 0, 0, 0, 0, 0, 0 };
        double numsum = 0;

        //compute each row total number(power of node number)
        for (int i = 0; i < nodersnum; i++)
        {
            num1[i] = Math.Pow(rowproduct[i], 1 / (double)nodersnum);
            numsum = numsum + num1[i];
        }

        //transfer into the number of percentage 
        Double[] weight;
        weight = new Double[7] { 0, 0, 0, 0, 0, 0, 0 };

        for (int i = 0; i < nodersnum; i++)
        {
            weight[i] = (int)Math.Round(100 * num1[i] / numsum * 100) / 100f;
            Console.WriteLine(weight[i]);
        }

        GridView2.DataSource = relmatrix;
        GridView2.DataBind();

        this.GridView2.Rows[mi].Cells[mj].BackColor = System.Drawing.Color.Red;
        this.GridView2.Rows[mi].Cells[mk].BackColor = System.Drawing.Color.Red;
        this.GridView2.Rows[mk].Cells[mj].BackColor = System.Drawing.Color.Red;

        Label3.Text = "Maximum Inconsistency after Reduction: " + Convert.ToString(inconsistency[0]);

        TextBox1.Text = Convert.ToString(weight[0]);
        TextBox2.Text = Convert.ToString(weight[1]);
        TextBox3.Text = Convert.ToString(weight[2]);
        TextBox4.Text = Convert.ToString(weight[3]);
        TextBox5.Text = Convert.ToString(weight[4]);
        TextBox6.Text = Convert.ToString(weight[5]);
        TextBox7.Text = Convert.ToString(weight[6]);
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • possible duplicate of [Object cannot be cast from DBNull to other types](http://stackoverflow.com/questions/6098646/object-cannot-be-cast-from-dbnull-to-other-types) – MikeSmithDev Mar 11 '14 at 14:44
  • and also your function is too big, since you have written all this code into your page directly you cant properly test this mathematical formulas. hard to maintain, hard to understand. – adt Mar 11 '14 at 14:52

2 Answers2

1

Looks like your matrix contains null values. You cannot cast them into Double.

Check like this:

if (relmatrix.Tables["relTable"].Rows[i][k] != DBNull.Value)

Or

if (relmatrix.Tables["relTable"].Rows[i][k] != null)
ttaaoossuuuu
  • 7,786
  • 3
  • 28
  • 58
0

Use nullable double as shown below:

var mynull = DBNull.Value;
Double? c = Convert.IsDBNull(mynull) ? (double?)null : Convert.ToDouble(mynull);

In your case:

Double? c = Convert.IsDBNull(relmatrix.tables["relTable"].Rows[k][j]) ? (double?)null : Convert.ToDouble(relmatrix.tables["relTable"].Rows[k][j]);;

Alternatively using if-else:

if(Convert.IsDBNull(relmatrix.tables["relTable"].Rows[k][j]) == true)
{
  Double? c = (double?)null;
}
else
{
  Double? c = Convert.ToDouble(relmatrix.tables["relTable"].Rows[k][j]);
}
Pramod Mangalore
  • 536
  • 5
  • 18
  • Thanks pkamathk, could you clearify clarify this statement please: "Double? c = Convert.IsDBNull(mynull) ? (double?)null : Convert.ToDouble(mynull);" – user3406596 Mar 11 '14 at 16:36
  • So the '?' sign is used to allow null values to be assigned to your variable. The ternary operator syntax is as follows: if (<>) then(?) 'return somevalue' else(:) 'return otherValue' Although for readability purposes its always best to use if-else statements rather than the ternary expressions. – Pramod Mangalore Mar 11 '14 at 18:59