Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4259178
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:44:55+00:00 2026-05-21T05:44:55+00:00

int a ; SqlCommand cmd = new SqlCommand ( Select * From items order

  • 0
int a ;

SqlCommand cmd = new SqlCommand (" Select * From items order by ItemID ", conn );

SqlDataReader reader = cmd.ExecuteReader();

while(reader.Read())
        {
            a = reader.GetInt32(0);

            if (reader.HasRows == false)
            {
                dataGridView1.Visible = false;
            }

            else
            {
                dataGridView1.Visible = true;

                DataTable dt = null;

                dt = new DataTable();

                dt.Load(reader);

                dataGridView1.DataSource = dt;

                if (reader.IsClosed == true)
                {
                        break;
                }   
            }

I want to ask that is the reader closed automatically, because here am not using reader.Close() and still it is closed? also , in my items table i have the first record as

ItemId | ItemName

 1       Bag
 2       Laptop
 8       Weighing Machine 

But, when this data is displayed in the datagridview then the row 1 , that is, the itemname “BAG” is not displayed. why so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:44:56+00:00Added an answer on May 21, 2026 at 5:44 am

    By calling Read(), you have already “claimed” the first row (hence why Bag isn’t showing – because you aren’t doing anything with it); and yet dt.Load is also going to do a while(reader.Read()). I expect you want (note I’m not calling Read here, and have no while loop):

    if(reader.HasRows)
    {
       // load via dt.Load() and show
    }
    else
    {
         // hide
    }
    

    The reason it is exiting is that once you’ve called Load you’ve already read all the data, so there is nothing else to read. I honestly don’t know whether getting to the end of the TDS stream implicitly closes the reader, but you should be using:

    using(var cmd = new SqlCommand (" Select * From items order by ItemID ", conn))
    using(var reader = cmd.ExecuteReader()) 
    {
         // everything else in here
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway of handling images from database to jquery image swapper or any
I'm trying to Update a table but I keep getting the exception : Update
I have a problem in listview.I my listview i have five columns(question_number,question_text,start_time,end_time,status). the first
I have a ASP:DropDown list which is populated by the following: public void Fill1()
First apologies if I don't explain this properly, I've been at this for hours,
I have created online exam application (web based) in asp.net c#. In my application
I call this method on form load ivent GetProducts( ); This Query works fine
I have a table name AVUKAT and it's columns ( AVUKAT , HESAP (Primary
I am wanting to know if it's possible to instantiate a null object inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.