0

I need the code below to say - if myReader reads a null entry a new method is called. At the moment it will not display data from tables that contain a null value

        conDataBase.Open();
        myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {
                if (myReader["Code_CodeID"] != DBNull.Value)
                {
                string sFirst = myReader["First"].ToString();
                string sLast = myReader["Last"].ToString();
                string sAdd1 = myReader["Address1"].ToString();
                string sCode = myReader["Code"].ToString();


                txtFirst.Text = sFirst;
                txtSecond.Text = sLast;
                txtadd1.Text = sAdd1;                   
                txtDeviceIMEI.Text = sCode;
                }

            }
        }
        else
        {
            //go to a new method
        }
    }
user2631662
  • 855
  • 5
  • 12
  • 21

1 Answers1

0
if (myReader.IsDBNull(myReader.GetOrdinal("First")))

Note: It is better to use GetOrdinal than pass Column name

Science_Fiction
  • 3,403
  • 23
  • 27
  • 1
    If you want the benefits of GetOrdinal, you probably want to save the ordinals for each column in variables before the loop and then use the variables instead. [More info](http://stackoverflow.com/questions/1079366/why-use-the-getordinal-method-of-the-sqldatareader) – Tobberoth Nov 07 '13 at 13:49
  • I was just letting the poster know to use GetOrdinal by habit. But yeah in looping situations you'd want to make the call once. In the current code underneath the covers GetOrdinal is being called multiple times which is not great. – Science_Fiction Nov 07 '13 at 14:20
  • Cheers I altered the code but still cant get it to call else statement – user2631662 Nov 07 '13 at 15:58
  • Your if and else braces do not seem to match. – Science_Fiction Nov 07 '13 at 16:16