locked
How to True Use SqlDataReader asp.net c# RRS feed

  • Question

  • User1038842493 posted

    Hi,

    I use sqldatareader. But ı'm searching and found multiple using. I using sqldatareader and gridview datasource,bind. No if no while  and this status working.

    But used like this in other sources. if(dr.hasrows) and while(dr.read()) . But this using not coming first data. 

    Which component do I need to use? 

    Thanks

    Wednesday, October 4, 2017 1:47 PM

All replies

  • User1120430333 posted

    I use a hasrows when only one record is returned, as shown in the example.

    https://www.codeproject.com/Articles/830129/Generically-Populate-a-Custom-NET-Class-from-a-Sql

    I use a while when I know more than one record is returned.

    It's up to you as to how you want to use hasrow or while as shown in the link.

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader

    In general, I use a while when I know I am going to use a DTO, populating the DTO and loading the DTO into a List<DTO> in the while reader loop.

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    Wednesday, October 4, 2017 5:54 PM
  • User-707554951 posted

    Hi  Asp.net Learning,

    It is not mandatory to check (dr.HasRows), if the DataReader contains any row or not. Read() will return False if there are no more rows to fetch, but Reader.HasRows is much more telling as to what it does than Read()

    So it would be a good practice to use Reader.HasRows because you may accidentally do something other than Read() which may fall into exception

    I suggest You should have if(dr.hasrows) and while(dr.read()) statement as below:

    Which avoid you falling into exception

    static void HasRows(SqlConnection connection) {
        using (connection)
        {
            SqlCommand command = new SqlCommand(
              "SELECT CategoryID, CategoryName FROM Categories;",
              connection);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
                        reader.GetString(1));
                }
            }
            else
            {
                Console.WriteLine("No rows found.");
            }
            reader.Close();
        }
    }

    https://forums.asp.net/t/1175522.aspx?sqldatareader+Hasrows+vs+Read+

    https://forums.asp.net/t/1689138.aspx?what+is+the+difference+between+DataReader+Read+and+DataReader+HasRows+

    For the usage of SqlDataReader, you could refer to the following links:

    http://www.c-sharpcorner.com/blogs/difference-between-datareader-dataset-dataadapter-and-datatable-in-c-sharp1

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader

    Best regards

    Cathy

    Thursday, October 5, 2017 3:19 AM
  • User1038842493 posted

    Hi,

    I'm not fully understand . İf hasrows okey. But i'm using while not coming first record.  Beacuse of gridview not need hasrow or while . All records automatic. 

     SqlCommand komut = new SqlCommand("select * from test", con.conn());
    
                SqlDataReader read = komut.ExecuteReader();
                if (read.HasRows)
                {
    
                    while (read.Read())
                    {
                        Repeater1.DataSource = read;
                        Repeater1.DataBind();
                    }
                   
                }

    Thursday, October 5, 2017 9:38 AM
  • User753101303 posted

    Hi,

    Read does read the first row and so you bind at best the gridview to a reader you began to read already. What if you try just :

    if (read.HasRows)
                {
    
                    //while (read.Read())
                    //{
                        Repeater1.DataSource = read;
                        Repeater1.DataBind();
                    //}
                 }
    

    for now to me this while(read.Read()) loop doesn't have any purpose.

    Thursday, October 5, 2017 10:03 AM
  • User-885936060 posted
    Hi,

    Then if compenent use databind no necessary while. Do you agree ?
    Friday, October 6, 2017 9:47 AM
  • User753101303 posted

    Yes. This the GiridView that will loop in the reader to get the data, not your own code.

    Friday, October 6, 2017 9:58 AM