0

i essentially have a query that generates a list of items (column 1) and their descriptions (column 2). There are 3 different items (out of 170) that do not have a description, so i hard coded my program to adjust for those 3 items. However, whenever my reader gets to the first item that has a null column, the reader is not even able to read the Item. Instead it says "Enumeration Yielded No Results"

var reader = command.ExecuteReader();
while (reader.Read())
{
    var node = reader[0] as string;
    string fullNodeName = string.Empty; 
    if (string.IsNullOrEmpty((string)reader[1]))
    {
        switch ((string)reader[0])
        {
            case "xxx":
                fullNodeName = "jhhfgnfh";
                break;
            case "xxx":
                fullNodeName = "fhnfgndfgdh";
                break;
            case "xxx":
                fullNodeName = "werqrqwerq";
                break;
        }
    }
    else
    {
        fullNodeName = reader[1] as string;
    }
    _nodeTokenList.Add(new Carriers.NodeToken(node, string.Format("{0} - {1}", node, fullNodeName)));

}

the data looks something like

Node   Description

XXX  ||  YYYYYYY YYY YYY

XXX  ||  YYYYYYY YYY YYY

XXX  || YYYYYYY YYY YYY

XXX  ||  YYYYYYY YYY YYY

XXX  ||                

XXX  ||  YYYYYYY YYY YYY

the row with the null description field is when the program starts acting up. Whats weird is that it enters the while loop for this row, but the exception gets caught at var node = reader[0] as string; (the field that is NOT null)

13
  • Please pay more attention to code formatting when you ask a question. Most of your code was indented miles over, making it much harder to read. Before posting, always read over what you're about to post and ask yourself whether it's formatted the way you'd want to read it if you were trying to answer. Commented Dec 15, 2016 at 19:41
  • thanks, will keep in mind Commented Dec 15, 2016 at 19:41
  • "Enumeration Yielded No Results" sounds like a debugger message. What happens when you actually run the code? Commented Dec 15, 2016 at 19:42
  • It captures all of the members up until the row with the empty field then it throws an exception "Unable to cast object of type 'System.DBNull' to type 'System.String'."} ...the yieleded no results is what is stored in the reader when i inspected the variable Commented Dec 15, 2016 at 19:43
  • 1
    You have to check DBNull before convert to string Commented Dec 15, 2016 at 19:45

1 Answer 1

2

You should swap out the (string)reader[1] with Convert.ToString(reader[1]). It contains a DBNull value, not null, and casting to string is what is causing your error. The Convert class can handle conversion better:

var reader = command.ExecuteReader();
reader.Read();
while (reader.Read())
{
    var node = Convert.ToString(reader[0]);
    string fullNodeName = string.Empty; 
    if (string.IsNullOrEmpty(Convert.ToString(reader[1])))
    {
        switch ((string)reader[0])
        {
            case "xxx":
                fullNodeName = "jhhfgnfh";
                break;
            case "xxx":
                fullNodeName = "fhnfgndfgdh";
                break;
            case "xxx":
                fullNodeName = "werqrqwerq";
                break;
        }
    }
    else
    {
        fullNodeName = reader[1] as string;
    }
    _nodeTokenList.Add(new Carriers.NodeToken(node, string.Format("{0} - {1}", node, fullNodeName)));

}
Sign up to request clarification or add additional context in comments.

2 Comments

I like this answer but the exception is getting triggered 2 lines above, with var node = reader[0]
Updated, just use Convert.ToString() again on the convert of reader[0].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.