1

This is what I want: A method that is within a class that will return iteratively the values of a certain column. This values will the be added to a combobox when the method is invoked. Here is my attempt:

  public string FillCombo()
      {
            string connstring = "Data Source=HP\\SQLEXPRESS;Initial Catalog=Arana;Integrated Security=True";
            string query = "Select * from categorias";
            SqlConnection conn = new SqlConnection(connstring);
            SqlCommand command = new SqlCommand(query, conn);
            SqlDataReader read;
                conn.Open();
                read = command.ExecuteReader();
                while (read.Read())
                {
                    string combodata = read.GetString(1);
                    return (combodata); 
                }
               return null;
        }

however, when this method is invoked, it only returns the first row into de combobox, not the other values.

3
  • 3
    How would you expect it to return multiple values? What would you expect the caller to see? It sounds like you should return a List<string> or something similar. Commented Nov 29, 2013 at 22:55
  • This will only ever return one result because you are returning the first item and exiting the function before the data reader gets a chance to read the next item. What's the result you are expecting - are you just attempting to fill a combobox with values from the database? Commented Nov 29, 2013 at 23:03
  • Instead if using return in your while loop which will return from the method, you could add each data value to a collection outside of your method. Commented Nov 29, 2013 at 23:04

2 Answers 2

1

It's called yield

http://msdn.microsoft.com/en-us/library/vstudio/9k7k7cf0.aspx

From the manual

public static System.Collections.IEnumerable Power(int number, int exponent)
{
    int result = 1;

    for (int i = 0; i < exponent; i++)
    {
        result = result * number;
        yield return result;
    }
}

yield will send a collection of return results from inside a loop after the loop has completed.

You can close data connections using a try/finally block around the loop.

  public IEnumerable FillCombo()
  {

        SqlConnection conn = new SqlConnection(connstring);
        SqlCommand command = new SqlCommand(query, conn);
        SqlDataReader read;
        conn.Open();
        read = command.ExecuteReader();
        try
        {
            while (read.Read())
            {
                yield return read.GetString(1);
            }
        }
        finally
        {
            read.close();
            conn.close();
        }
    }

A cool and often overlooked feature of C#

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

4 Comments

@MikeChristensen most data connections support IDispose so wrapping the loop in a using statement will close the reader when the function exists.
I think if the data reader is closed before you materialize the enumeration, you're gonna get an exception. I think you'd be better off returning a List<String> or String[]
@MikeChristensen what do you think of my edit? Haven't tested it but it should work.
Yea, I'd have to test that out too. If the finally block is executed after you iterate through the returned enumeration (which would be cool) then it should work! I'm curious so I'll give it a shot when I have some free time.
0

Consider using a List of string as an output. The following minor change to your code should help...

public List<string> FillCombo()
{
    List<string> comboList = new List<string>();
    string connstring = "Data Source=HP\\SQLEXPRESS;Initial Catalog=Arana;Integrated Security=True";
    string query = "Select * from categorias";
    SqlConnection conn = new SqlConnection(connstring);
    SqlCommand command = new SqlCommand(query, conn);
    SqlDataReader read;
    conn.Open();
    read = command.ExecuteReader();
    while (read.Read())
    {
        string combodata = read.GetString(1);
        comboList.Add(combodata);
    }
    return comboList;
}

Good Luck!

Comments

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.