0

i use SqlDataReader to read data from query result,i run the debugger and the query is working. hasrows is true, but it shows Enumeration yielded no results.

this is my code :

SqlCommand selectLastData = new SqlCommand("SELECT TOP 1 * FROM tbl_transaksi ORDER BY id DESC", conn);
using (SqlDataReader rdr = selectLastData.ExecuteReader())
{
    if (rdr.HasRows)
    {
        while (rdr.Read())
        {
           string id = rdr["id"].ToString();

        }
        rdr.Close();
    }
}

enter image description here anyone can help? thank you

Mamen
  • 1,322
  • 5
  • 21
  • 44
  • Did you mean to comment out the `Console.WriteLine`? – mjwills Mar 07 '18 at 12:27
  • no i have edited the question, i mean the rdr hasrows but data is empty – Mamen Mar 07 '18 at 12:29
  • @Mamen: please explain what's going on, what means "data is empty"? – Tim Schmelter Mar 07 '18 at 12:30
  • can you please provide exception and stack trace here? – Mihir Dave Mar 07 '18 at 12:31
  • the HasRows property is showing true value it means there is some data within it, but the ResultView is showing Enumeration yielded no results and the while is skipped. – Mamen Mar 07 '18 at 12:32
  • @MihirDave there is no error – Mamen Mar 07 '18 at 12:33
  • 1
    @Mamen: what _ResultView_? A `SqlDataReader` is not a LINQ query. Output the `id` in the loop and you will see it: `Console.WriteLine(id);` – Tim Schmelter Mar 07 '18 at 12:34
  • @TimSchmelter i add the picture of my error, the loop even not executed – Mamen Mar 07 '18 at 12:39
  • @MihirDave the data is available in database there are 7 rows – Mamen Mar 07 '18 at 12:39
  • there are 7 records in table @PanagiotisKanavos – Mamen Mar 07 '18 at 12:42
  • The `id` variable is never used, do you know if it is ever written to; have debugged and stepped into the while loop and seen if the code is executed? Maybe the error is that you never use the value? By the way, you should really use, `ExecuteScalar()` method instead of `ExecuteReader()` when your purpose is to read a single value – mortb Mar 07 '18 at 12:42
  • See this discussion and answer: https://stackoverflow.com/a/20275727/8695782 – Alexandru Clonțea Mar 07 '18 at 12:44
  • @Mamen what happens when control reaches to dr.Read() ? – Mihir Dave Mar 07 '18 at 12:44
  • @MihirDave it skipped the loop and goes to rdr.Close() – Mamen Mar 07 '18 at 12:45
  • I have close voted already because it was unclear, but this is the duplicate: https://stackoverflow.com/questions/13117343/sqldatareader-enumeration-yielded-no-results Maybe someone else can close-vote it as dup. – Tim Schmelter Mar 07 '18 at 12:47
  • @mortb the executescalar also return null – Mamen Mar 07 '18 at 12:47
  • Remove all existing breakpoints and watch window entries. Add a breakpoint to `string id = rdr["id"].ToString();` Run the code. Do you hit the breakpoint (don't guess, give it a try). – mjwills Mar 07 '18 at 12:52
  • What if you run the code directly without any breakpoints? Does print anything in Console? – Chetan Mar 07 '18 at 12:54
  • no it doesnt hit id @mjwills – Mamen Mar 07 '18 at 12:56
  • Have you run the query in sql server management studio, against the same database? If you do, are there any rows returned? Are you sure you are using the correct connection string so you're not connected to a database where the table is empty? – mortb Mar 07 '18 at 13:08
  • Off topic: [SqlCommand](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(v=vs.110).aspx) also implements `IDisposable` so it should be in a `using` statement too. – Lews Therin Mar 07 '18 at 14:47

1 Answers1

0

Use,

While(rdr.read()){
//Read your Columns values using Id or Name Here..
}

read() function allows to read the populated data from database.

Jack Sparrow
  • 57
  • 1
  • 10