I am reading from a SQL datareader in C# and passing the values from the columns to a dropdownlist. There are two columns being read. Using IsDbNull, I am able to handle the null values. However, as I have the code written right now, if dr.GetString(0) is null, no values are passed along at all, while as long as only dr.GetString(1) (or neither) is null, all of the values are passed along and the null values are ignored. Here is what I have for while the datareader is reading:
while (dr.Read())
{
if (!dr.IsDBNull(0))
{
machineName.Items.Add(dr.GetString(0).ToString());
}
else if (!dr.IsDBNull(1))
{
machineName.Items.Add(dr.GetString(1).ToString());
}
}
What I need to happen is for the dropdownlist to be populated with whatever values the datareader returns, regardless of which column they are in. I've removed the using and try/catch statements in order to declutter the code. Thanks everyone.