September 27th, 2010, 01:46 PM
[RESOLVED] Why would SQLDataReader close itself? Interesting.
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:
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.
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();
dgvwCustomerInfo.DataSource = dt;
while (rdr.Read()) ////ERROR here - says reader closed!
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
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!