[RESOLVED] Why would SQLDataReader close itself? Interesting.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: [RESOLVED] Why would SQLDataReader close itself? Interesting.

Threaded View

  1. #1
    Join Date
    Jul 2010
    Posts
    82

    [RESOLVED] Why would SQLDataReader close itself? Interesting.

    Hi all,
    Iam baffled at this - so basically, I want to get a set of records based on a query and then populate a combobox (with a field form the query) and then set the entire query result as a datagridview's datasource which displays all of its fields. Its to give the users 2 different views of their search to make it easier for them. here's the code:

    Code:
    SqlCommand cmd = new SqlCommand("Select rtrim(ndm_ndcode) as ndm_ndcode, rtrim(ndm_name) as ndm_name, rtrim(ndm_addr1) as ndm_addr1, rtrim(ndm_addr2) as ndm_addr2, rtrim(ndm_addr3) as ndm_addr3, rtrim(ndm_addr4) as ndm_addr4, rtrim(ndm_addr5) as ndm_addr5, rtrim(ndm_postcode) as ndm_postcode from ndmas where ndm_ndcode LIKE '" + sCustomer + "'", m_cnnMax); // it select x, y,z based on a condition on connection m_cnnMAX
                    SqlDataReader rdr = cmd.ExecuteReader();
                    //set datagridview's datasource
                    DataTable dt = new DataTable();
                    dt.Load(rdr);
                    dgvwCustomerInfo.DataSource = dt;
                    dgvwCustomerInfo.Refresh();
                    while (rdr.Read()) ////ERROR here - says reader closed!
                    {
                        cmbAccountCodes.Items.Add(rdr["ndm_ndcode"].ToString());
                    }     
                    rdr.Close(); rdr.Dispose();
                    cmd.Dispose();
    So, when I set the SqlDataReader object to the gridview, it works fine. If I use it to read it , then it says its already closed, even though I have not. Can you find out where the error is or Iam missing something? Please help. Thank you.

    Also, it would be very useful to you and me, if you can check out the site below. Thank you very much.
    Last edited by CuteAssistant; September 27th, 2010 at 02:12 PM. Reason: Code tag format corrected. Got it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center