Hi,

I have a IF-Else statement, seems not getting the values populated for the first 4 of my cases, the values from 0-3.
the following is my code.

int MST = Convert.ToInt32(Convert.ToInt32(DetailsView3.Rows[0].Cells[1].Text));

if (MST == 0)
{
    DetailsView2.Rows[3].Cells[1].ForeColor = Color.Red;
    DetailsView2.Rows[3].Cells[1].Text = "Closed";
}

else if (MST == 1)
{
    DetailsView2.Rows[3].Cells[1].ForeColor = Color.Green;
    DetailsView2.Rows[3].Cells[1].Text = "Order";
}
else if (MST == 2)
{
    DetailsView2.Rows[3].Cells[1].ForeColor = Color.Green;
    DetailsView2.Rows[3].Cells[1].Text = "Order";
}
else if (MST == 3)
{
    DetailsView2.Rows[3].Cells[1].ForeColor = Color.Green;
    DetailsView2.Rows[3].Cells[1].Text = "Open";
}
else if (MST == 4)
{
    DetailsView2.Rows[3].Cells[1].ForeColor = Color.Green;
    DetailsView2.Rows[3].Cells[1].Text = "Open";
}
else if (MST == 5)
{
    DetailsView2.Rows[3].Cells[1].ForeColor = Color.Red;
    DetailsView2.Rows[3].Cells[1].Text = "Closed";
}
else if (MST == 6)
{
    DetailsView2.Rows[3].Cells[1].ForeColor = Color.Red;
    DetailsView2.Rows[3].Cells[1].Text = "Closed";
}
else if (MST == 7)
{
    DetailsView2.Rows[3].Cells[1].ForeColor = Color.Red;
    DetailsView2.Rows[3].Cells[1].Text = "Closed";
}
else if (MST == 8)
{
    DetailsView2.Rows[3].Cells[1].ForeColor = Color.Red;
    DetailsView2.Rows[3].Cells[1].Text = "Closed";
}
else if (MST == 9)
{
    DetailsView2.Rows[3].Cells[1].ForeColor = Color.Red;
    DetailsView2.Rows[3].Cells[1].Text = "Closed";
}

I have also tried to catch the values in MST using :

Respnose.Write(MST.ToString());

Thanks all.

First of all the line

int MST = Convert.ToInt32(Convert.ToInt32(DetailsView3.Rows[0].Cells[1].Text));

must throw an exception. Take a look and you'll find out why.
Second, using a switch is the preferred coding practice in such cases.
Third, it is not possible that the code does not work. If you doubt this put a breakpoint on the first line (the erroneous one) and look what you have in MST. You may also place a tracepoint and look in VS Output tool window.
Fourth, and maybe last, it is a wrong practice to name local variables MST. Use mst instead. Remember, code is more read than written.
Happy coding :)

This article has been dead for over six months. Start a new discussion instead.