Ritu

Ritu

  • 1.4k
  • 347
  • 42.6k

Enumeration yielded no results in c#

Dec 9 2021 12:51 PM

Hi 

I have one simple query 

I want to retrieve all distinct PO's from table but when I execute a query it give recordsaffected -1

below is my query

public static string GetPO()
{
    List<string> lst = new List<string>();
    
    SqlConnection connDB = new SqlConnection(constr);
    try
    {
        connDB.Open();
        SqlDataReader reader = null;
        
        string selectcmd = " SELECT distinct PO as PurchaseOrder FROM ABC";
        SqlCommand cmd = new SqlCommand(selectcmd, connDB);
        cmd.CommandType = CommandType.Text;
        reader = cmd.ExecuteReader(); 
            while (reader.Read())
            {
                 lst.Add(reader.ToString());

            }
            reader.Close();
    }

    catch (Exception e)
    {
        Console.WriteLine(e.Message.ToString());
    }
    finally
    {
        if (connDB.State != ConnectionState.Closed)
            connDB.Close();
    }
    return lst.ToString();
}

NO records in reader, please help. thanks in advance


Answers (2)