Note that there are some explanatory texts on larger screens.

plurals
  1. POExecuteReader() making program exit Class
    primarykey
    data
    text
    <p>I'm having a bit of trouble populating a listbox on a windows form from a table in sqlServer. The table is a single column of string values, and as I am ilkely to call this list of values many times throughout my application, I set it up as a Class, and in the default constructor I populate an ArrayList with the values from the SQL table, as follows:</p> <pre><code>class cCaseType { public ArrayList aValues = new ArrayList(); public cCaseType() { string strConnection = BuildConnectionString(); SqlConnection linkToDB = new SqlConnection(strConnection); string sqlText = "SELECT * FROM tblCaseTypes;"; SqlCommand sqlComm = new SqlCommand(sqlText, linkToDB); SqlDataReader sqlReader = sqlComm.ExecuteReader(); MessageBox.Show("Hi"); while (sqlReader.Read()) { aValues.Add(sqlReader["CaseType"].ToString()); } sqlReader.Close(); linkToDB.Close(); } public string BuildConnectionString() { cConnectionString cCS = new cConnectionString(); return cCS.strConnect; } } </code></pre> <p>Now I know the database connection is working becuase the same connection is used throughout the application. I know the SQL command text is corrext as when I execute it in SQL Management Studio it returns all the values in the table as expected. However, the program simply Exits at ExecuteReader() and so the ArrayList is never populated. </p> <p>I did wonder if the program was exiting because the 'while' loop was skipping due to an empty result from the reader, but its not even reaching the 'while' statement. I proved this by putting in the MessageBox and putting a breakpoint on it. The program never opens the messagebox proving that it is exiting the class on the ExecuteReader statement.</p> <p>What on earth am I doing wrong!</p> <p>As ever, thanks for any assistance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload