{"id":1908,"date":"2022-08-30T15:20:27","date_gmt":"2022-08-30T15:20:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/why-is-this-not-working-does-not-execute-code-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:27","modified_gmt":"2022-08-30T15:20:27","slug":"why-is-this-not-working-does-not-execute-code-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-is-this-not-working-does-not-execute-code-collection-of-common-programming-errors\/","title":{"rendered":"Why is this not working? Does not execute code?-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to have a textBox auto complete with values from a database. When i bullet test the code will walk though until the <code>if<\/code> statement in the function below. Could some one please tell me why my code wont execute after that <code>if<\/code>?<\/p>\n<p>I am trying to run the following but it keeps jumping out before the <code>if<\/code>:<\/p>\n<pre><code>public AutoCompleteStringCollection AutoCompleate(string dataBase, string procedure)\n{\n        AutoCompleteStringCollection namesCollection =\n            new AutoCompleteStringCollection();\n\n        SqlDataReader dReader;\n        SqlCommand cmd = new SqlCommand(procedure, con);\n        cmd.CommandType = CommandType.StoredProcedure;\n        con.Open();\n        dReader = cmd.ExecuteReader(); \/\/ This is the last place my bullet check \n                                       \/\/ gets before it hop's out!\n        if (dReader.HasRows == true)\n        {\n            while (dReader.Read())\n                namesCollection.Add(dReader[\"SystemUser\"].ToString());\n        }\n        con.Close();\n        dReader.Close();\n        return namesCollection;\n    }\n<\/code><\/pre>\n<p><em><strong>Additional information, but please focus on the above!<\/strong><\/em><\/p>\n<p><em>Please ask if you need any info. \ud83d\ude42<\/em><\/p>\n<p><strong>The Call to the above:<\/strong><\/p>\n<pre><code>textBoxUser.AutoCompleteMode = AutoCompleteMode.Suggest;\ntextBoxUser.AutoCompleteSource = AutoCompleteSource.CustomSource;\ntextBoxUser.AutoCompleteCustomSource = \n    DatabaseService.Instance.AutoCompleate(\"AuditIT\", \"AutoCompleate\");\n<\/code><\/pre>\n<p><strong>Stored Proc:<\/strong><\/p>\n<pre><code>USE [AuditIT]\nGO\n\nSET ANSI_NULLS ON\nGO\nSET QUOTED_IDENTIFIER ON\nGO\n--Allow textboxs to have a autocomplete\n ALTER procedure [dbo].[AutoCompleate] \n as\n\n Select distinct SystemUser from SchemaAudit\n             order by SystemUser asc\n<\/code><\/pre>\n<ol>\n<li>\n<p>There is an unhandled exception when you try to hit the database. Try to refactor the code as this:<\/p>\n<pre><code>try{\n\nif (dataBase.Length &gt; 0) { procedure = dataBase + \"..\" + procedure; } \/\/Set procedure to DBNAME..ProcedureName\n\nSqlDataReader dReader;\n            SqlCommand cmd = new SqlCommand(procedure, con);\n            cmd.CommandType = CommandType.StoredProcedure;\n            con.Open();\n            dReader = cmd.ExecuteReader(); \/\/ This is the last place my bullet check gets before it hop's out!\n            if (dReader.HasRows == true)\n            {\n                while (dReader.Read())\n                    namesCollection.Add(dReader[\"SystemUser\"].ToString());\n            }\n            con.Close();\n            dReader.Close();\n}\ncatch(Exception e)\n{\n    \/\/ handle the exception better than me :)\n    Console.WriteLine(e.Message);\n}\n<\/code><\/pre>\n<p>Put a messagebox or a breakpoint on the console.WriteLine and see what&#8217;s happen.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:17:19. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am trying to have a textBox auto complete with values from a database. When i bullet test the code will walk though until the if statement in the function below. Could some one please tell me why my code wont execute after that if? I am trying to run the following but it keeps [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1908","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1908"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1908\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}