Question

I designed my webpage to read a data string then display the results on labels in an html table. I am attempting to highlight the row that my database reads as a current order. My only problem is only one record is set to be active but they all highlight as if they were active. I use an array to set my data and I also use the label to get the ID I need (all is in code below). I have posted my method and where I use it in the asp page load. How can I fix my method to return correctly?

The implementing of the method in page load

if (lineData.IsCurrentOrderFind(L68.Text))
{
    myTable.Rows[1].Cells[0].BgColor = "#FE2E2E";
    myTable.Rows[1].Cells[1].BgColor = "#FE2E2E";
    myTable.Rows[1].Cells[2].BgColor = "#FE2E2E";
    myTable.Rows[1].Cells[3].BgColor = "#FE2E2E";
    myTable.Rows[1].Cells[4].BgColor = "#FE2E2E";
}

Here is method that label above gets passed to

public bool IsCurrentOrderFind(string itemNumber)
    {

        StringBuilder sqlString = new StringBuilder();
        sqlString.Append("SELECT * ");
        sqlString.Append("FROM WorkOrder ");
        sqlString.Append("WHERE LineNumber = " + ConfigurationManager.AppSettings["Line"] + " AND LineCompleted =  0 AND (ScaleGroup LIKE '%1' OR ScaleGroup LIKE '%3') ");
        sqlString.Append(" AND CaseGenNum6 = @CaseGenNum6");

        SqlDataReader reader = null;
        SqlConnection dbConn = App_Code.DBHelper.getConnection();

        SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@CaseGenNum6", itemNumber) };

        try
        {
            reader = App_Code.DBHelper.executeQuery(dbConn, sqlString.ToString(), parameters);
            while (reader.Read())
            {
                IsCurrentOrder = (reader["IsCurrentOrder"] != DBNull.Value && !string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString())) ? true : false;
            }
            reader.Close();
            reader.Dispose();
            dbConn.Close();
            dbConn.Dispose();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (dbConn != null)
            {
                try { dbConn.Close(); dbConn.Dispose(); }
                catch { }
            }
            if (reader != null)
            {
                try { reader.Close(); reader.Dispose(); }
                catch { }
            }
        }
        if (IsCurrentOrder == true) I realize this is not necessary
        {
            return true;
        }
        else
        {
            return false;
        }
    }
Was it helpful?

Solution

The problem could be with this expression:

!string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString())

Instead of calling ToString(), try simply casting it to a string:

!string.IsNullOrEmpty((string)reader["IsCurrentOrder"])

Possibly even better (the previous line might throw an exception if it's not really a string):

!string.IsNullOrEmpty(reader["IsCurrentOrder"] as string)

The reason being is that if the string is really null, calling ToString() will return a non-null string "null".

OTHER TIPS

IsCurrentOrder is not declared locally. It seems to be declared at a higher scope. When you enter this function, nothing is initializing the variable (back to false). So, it is remaining at its last setting. Try this code instead:

public bool IsCurrentOrderFind(string itemNumber)
{
    bool IsCurrentOrder = false;

    //and the rest of your source code

the line

IsCurrentOrder = (reader["IsCurrentOrder"] != DBNull.Value && !string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString())) ? true : false;
        }

It's not actually checking the value of the field, only that it's not null or empty.

Try

if(
(reader["IsCurrentOrder"] != DBNull.Value 
&& 
!string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString())) 
)
{
    IsCurrentOrder = reader["IsCurrentOrder"];
}
else
IsCurrentOrder = false;

I think there is a lot of refactoring you could do to this method though that will simplify the logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top