Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use <a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader.aspx" rel="noreferrer"><code>ExecuteReader()</code></a> and not <a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx" rel="noreferrer"><code>ExecuteNonQuery()</code></a>. <code>ExecuteNonQuery()</code> returns only the number of rows affected.</p> <pre><code>try { SqlDataReader dr = cmd1.ExecuteReader(); } catch (SqlException oError) { } while(dr.Read()) { string treatment = dr[0].ToString(); } </code></pre> <p>Or better, use a <a href="https://msdn.microsoft.com/en-us/library/yh598w02.aspx" rel="noreferrer"><code>using</code> statement</a> for it.</p> <pre><code>using(SqlDataReader dr = cmd1.ExecuteReader()) { while(dr.Read()) { string treatment = dr[0].ToString(); } } </code></pre> <p>But if your <code>SqlCommand</code> returns only <code>1</code> column, you can use the <a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx" rel="noreferrer"><code>ExecuteScalar()</code></a> method. It returns first column of the first row as follows:-</p> <pre><code>cmd.CommandText = "SELECT treatment FROM appointment WHERE patientid = " + text; string str = Convert.ToString(cmd.ExecuteScalar()); </code></pre> <p>Also you can open your code to <a href="http://en.wikipedia.org/wiki/SQL_injection" rel="noreferrer">SQL Injection</a>. Always use parameterized queries. Jeff has a cool blog article called <a href="http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html" rel="noreferrer">Give me parameterized SQL, or give me death</a>. Please read it carefully. Also read <a href="http://www.dotnetperls.com/sqlparameter" rel="noreferrer">DotNetPerl SqlParameter</a> article. SQL Injection very important when you are working queries.</p>
 

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