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 1049015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:32:58+00:00 2026-05-16T16:32:58+00:00

I want to populate a gridview with table data from SQL. I tried just

  • 0

I want to populate a gridview with table data from SQL. I tried just returning my SqlDataReader object and using it as my datasource, but I am getting errors that the reader has been closed. I wanted to convert this to a DataSet and just return a dataset, but I couldn’t find an easy way to convert the row data to a dataset. I’ve also read that DataSets are dead in .NET 3.5/4.0, is this true?

Here’s my Data Layer method. It would be awesome if I could return something useable as a datasource:

public SqlDataReader GetSites()
{
    SqlConnection sqlCon = null;
    SqlDataReader rdr = null;
    try
    {
        sqlCon = new SqlConnection(StoredProcedures.conString);
        sqlCon.Open();
        SqlCommand cmd = new SqlCommand("GetSites", sqlCon);
        cmd.CommandType = CommandType.StoredProcedure;
        rdr = cmd.ExecuteReader();
        return rdr;
    }
    finally
    {
        if (sqlCon != null)
        {
            sqlCon.Close();
        }
        //if (rdr != null)
        //{
        //    rdr.Close();
        //}
    }
}
  • 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-16T16:32:59+00:00Added an answer on May 16, 2026 at 4:32 pm

    As Carlos Munoz has stated, you closed your sql connection. You need the SQL connection opened in order for the reader to read. Simply comment out

    if (sqlCon != null)
    {
            sqlCon.Close();
    }
    

    and you should be fine.

    Another alternative is to use SqlDataAdapter which I prefer.

    Here is an example…

    public static DataSet GetDataSet(string sql, DatabaseType database)
    {
        using ( var connection = new SqlConnection( GetConnectionString(database) ) )
        {
            using (var adapter = new SqlDataAdapter(sql, connection))
            {
                var temp = new DataSet();
                adapter.Fill(temp);
                return temp;
            }
        }
    }
    

    Just set the data source of your data grid to the Data set’s table returned.

    DGV.DataSource = DatabaseFunction.GetDataSet(sql, DatabaseType.Outage).Tables[0].DefaultView;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a table in ASP MVC and populate it with data from
I'm using a form to populate a gridview with x users. However I want
Wondering the best way to populate a GridView with my object data. I have
I am trying to populate an (editable) gridview in ASP.NET with a table from
I want to populate GridView below with images: <asp:GridView ID=GrdDynamic runat=server AutoGenerateColumns=False> <Columns> </Columns>
I am creating a form in c#.net . I want to populate the gridview
I want to populate my database with test data my user and profile models
I want to populate a table form a MYSQL database. I have defined the
I want to populate a table, defined in layout xml file through the programmatic
I want to populate a drop down menu with a column from a MySQL

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.