Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the else branch not get executed?

Tags:

c#

This actually is a working application. Albeit without much in the way of error trapping.

I am just trying to log the flow and am not reaching either the If branch or the else branch. What can I do to make that happen? Neither side of the branch is logging into my txt file.

while (reader.Read())
{
    if (reader.HasRows)
    {
        LogMessage("Further Inside Try2 ");
        byte[] paymentData = (byte[])reader["payment"];
        strPaymentData = ASCIIEncoding.Unicode.GetString(paymentData);
        LogMessage(strPaymentData + " strPaymentData");
    }
    else
    {
        LogMessage("Payment Retrievlal Failed ");
    }
}
like image 667
JoJo Avatar asked Nov 29 '22 03:11

JoJo


1 Answers

You don't need to check if reader has rows if you're using while reader.read(). If reader returns no rows, While loop will not execute. Hence the else will never be reached if reader has no rows.

You could rewrite your code as follows:

if (reader.HasRows)
{
    while (reader.Read())
    {    
        LogMessage("Further Inside Try2 ");
        byte[] paymentData = (byte[])reader["payment"];
        strPaymentData = ASCIIEncoding.Unicode.GetString(paymentData);
        LogMessage(strPaymentData + " strPaymentData");
    }
}
else
{
    LogMessage("Payment Retrievlal Failed ");
}
like image 79
Dimitri Avatar answered Dec 09 '22 02:12

Dimitri