Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to have exactly one value from one single record, you can use the <a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx" rel="nofollow noreferrer"><code>ExecuteScalar</code></a> method of the <code>SqlCommand</code> class:</p> <pre><code>string title = null; using (SqlConnection conn = new SqlConnection("your-connection-string")) using (SqlCommand cmd = new SqlCommand( "select ContentTitle from {put table name here} where id = 4", conn)) { conn.Open(); title = (string)conn.ExecuteScalar(); } if (!string.IsNullOrEmpty(title)) { // assign title to suitable asp.net control property } </code></pre> <p>If you want to be able to do this for various ids, <em>do not just concatenate a new sql string</em>. I will repeat that: <strong>do not just concatenate a new sql string</strong>. Use parameters instead:</p> <pre><code>string title = null; using (SqlConnection conn = new SqlConnection("your-connection-string")) using (SqlCommand cmd = new SqlCommand( "select ContentTitle from {put table name here} where id = @id", conn)) { SqlParameter param = new SqlParameter(); param.ParameterName = "@id"; param.Value = yourIdGoesHere; cmd.Parameters.Add(param); conn.Open(); title = (string)conn.ExecuteScalar(); } if (!string.IsNullOrEmpty(title)) { // assign title to suitable asp.net control property } </code></pre> <hr> <p><strong>Update</strong><br> Sample aspx page. First some markup (let's say the file is called example.aspx):</p> <pre><code>&lt;body&gt; &lt;form id="Form1" runat="server"&gt; Title: &lt;asp:Label id="_titleLabel" Text="{no title assigned yet}" runat="server"/&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p>...and in the code-behind (that would be called example.aspx.cs; I have included only the Page_Load event for simplicity):</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { int id; try { if (int.TryParse(Request.QueryString["id"], out id)) { _titleLabel.Text = GetContentTitle(id); } else { _titleLabel.Text = "no id given; cannot look up title"; } } catch (Exception ex) { // do something with the exception info } } private static string GetContentTitle(int id) { using (SqlConnection conn = new SqlConnection("your-connection-string")) using (SqlCommand cmd = new SqlCommand( "select ContentTitle from {put table name here} where id = @id", conn)) { SqlParameter param = new SqlParameter(); param.ParameterName = "@id"; param.Value = yourIdGoesHere; cmd.Parameters.Add(param); conn.Open(); return (string)conn.ExecuteScalar(); } } </code></pre> <p><em>Disclaimer: the code is written directly into the answer window and not tested (I don't have access to a development environment right now) so there may be errors</em></p>
    singulars
    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