{"id":140,"date":"2013-09-30T16:35:55","date_gmt":"2013-09-30T16:35:55","guid":{"rendered":"http:\/\/carla.romere.com\/?p=140"},"modified":"2013-09-30T16:35:55","modified_gmt":"2013-09-30T16:35:55","slug":"using-executescalar-to-populate-a-variable-in-c","status":"publish","type":"post","link":"https:\/\/carla.romere.com\/?p=140","title":{"rendered":"Using ExecuteScalar to Populate a Variable in C#"},"content":{"rendered":"<p>After many attempts and various and sundry internet searchings, I have finally have properly working code to display the result of a scalar query in tSQL using asp.net C#.<\/p>\n<p>I had to add the reference to System.Configuration in Visual Studio.<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">using System;<\/span><br \/>\n<span style=\"color: #0000ff;\">using System.Collections.Generic;<\/span><br \/>\n<span style=\"color: #0000ff;\">using System.Web;<\/span><br \/>\n<span style=\"color: #0000ff;\">using System.Web.UI;<\/span><br \/>\n<span style=\"color: #0000ff;\">using System.Web.UI.WebControls;<\/span><br \/>\n<span style=\"color: #0000ff;\">using System.Data;<\/span><br \/>\n<span style=\"color: #0000ff;\">using System.Data.Sql;<\/span><br \/>\n<span style=\"color: #0000ff;\">using System.Data.SqlClient;<\/span><br \/>\n<span style=\"color: #0000ff;\">using System.Configuration;<\/span><\/p>\n<p>Then I have this code in the PageLoad event of code behind:<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">int addcount; <span style=\"color: #339966;\">\/\/variable to hold my result<\/span><\/span><br \/>\n<span style=\"color: #0000ff;\"> string strConnection = ConfigurationManager.ConnectionStrings[&#8220;addressesConnectionString&#8221;].ConnectionString; <span style=\"color: #339966;\">\/\/ my connection string defined in web.config<\/span><\/span><br \/>\n<span style=\"color: #0000ff;\"> string strSelect = @&#8221;SELECT COUNT(AddressID) FROM mainaddr&#8221;; <span style=\"color: #339966;\">\/\/my query returning a single result<\/span><\/span><br \/>\n<span style=\"color: #0000ff;\"> SqlConnection con = new SqlConnection(strConnection);\u00c2\u00a0<\/span><br \/>\n<span style=\"color: #0000ff;\"> SqlCommand cmd = new SqlCommand();\u00c2\u00a0<\/span><br \/>\n<span style=\"color: #0000ff;\"> cmd.Connection = con;\u00c2\u00a0<\/span><br \/>\n<span style=\"color: #0000ff;\"> cmd.CommandType = CommandType.Text;<\/span><br \/>\n<span style=\"color: #0000ff;\"> cmd.CommandText = strSelect;<\/span><\/p>\n<p><span style=\"color: #0000ff;\"> con.Open();<\/span><br \/>\n<span style=\"color: #0000ff;\"> addcount = Convert.ToInt16(cmd.ExecuteScalar()); <span style=\"color: #339966;\">\/\/running the scalar query and capturing the result<\/span><\/span><br \/>\n<span style=\"color: #0000ff;\"> con.Close();<\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">if (addcount &gt;= 1)<\/span><br \/>\n<span style=\"color: #0000ff;\"> {<\/span><br \/>\n<span style=\"color: #0000ff;\"> lblCount.Text = &#8220;We currently have &#8221; + addcount.ToString() + &#8221; addresses.&#8221;; \/\/actually setting the label text to the result of the scalar query<\/span><br \/>\n<span style=\"color: #0000ff;\"> }<\/span><\/p>\n<p>I hope this helps someone and saves you a little bit of time!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After many attempts and various and sundry internet searchings, I have finally have properly working code to display the result of a scalar query in tSQL using asp.net C#. I had to add the reference to System.Configuration in Visual Studio. using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Configuration; Then I have this code in the PageLoad event of code behind: int addcount; \/\/variable to hold my result string strConnection =&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/carla.romere.com\/?p=140\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,7],"tags":[8,9],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-programming","category-technology","tag-c","tag-tsql"],"_links":{"self":[{"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=140"}],"version-history":[{"count":2,"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":142,"href":"https:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts\/140\/revisions\/142"}],"wp:attachment":[{"href":"https:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}