{"id":4463,"date":"2014-03-30T11:16:31","date_gmt":"2014-03-30T11:16:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-assigning-a-sqlparameter-collection-of-common-programming-errors\/"},"modified":"2014-03-30T11:16:31","modified_gmt":"2014-03-30T11:16:31","slug":"problem-assigning-a-sqlparameter-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-assigning-a-sqlparameter-collection-of-common-programming-errors\/","title":{"rendered":"Problem assigning a SqlParameter-Collection of common programming errors"},"content":{"rendered":"<p>Hello every1, I have the following function to read records from a Table:<\/p>\n<pre>        public ArrayList LeerDatos(int pNumero)\n        {\n            ArrayList libros = new ArrayList();\n            string strSql = \"Select codLibro, nombreLibro, ubicacion, comentarios, \";\n            strSql += \"idioma, editorial, fechaPublicacion, fechaAlta from Libro \";\n\n            cmd.CommandText = strSql;\n            cmd.Connection = conn;\n            cmd.Parameters.Clear();\n            if (pNumero != 0)\n            {\n                strSql += \" where codLibro = @CODIGO\";\n                cmd.Parameters.Add(new SqlParameter(\"@CODIGO\", pNumero));\n            }\n            strSql += \" order by codLibro\";\n\n            SqlDataReader reader;\n\n            conn.Open();\n            reader = cmd.ExecuteReader();\n\n            while (reader.Read())\n            {\n                LibroBO libro = new LibroBO();\n\n                libro.CodLibro = reader.GetInt32(0);\n                libro.NombreLibro = reader.GetString(1);\n                libro.Ubicacion = reader.GetString(2);\n                libro.Comentarios = reader.GetString(3);\n\n                libros.Add(libro);\n            }\n\n            conn.Close();\n\n            return (libros);\n        }\n<\/pre>\n<p>Well, I think it doesn\u00b4t have any extrange&#8230;\u00a0it is simply this: when I need all the table records, I pass 0 (zero) to the function parameter. When I need a certain record, I pass the record key.Then, inside the function, I ask if the parameter isn&#8217;t zero, in which case I&#8217;ll add a parameter to my SqlCommand variable:<\/p>\n<pre>            if (pNumero != 0)\n            {\n                strSql += \" where codLibro = @CODIGO\";\n                cmd.Parameters.Add(new SqlParameter(\"@CODIGO\", pNumero));\n            }\n<\/pre>\n<p>Has it sense? I guess yes.But\u00a0the problem that I have is that I always get all the records from table. I had\u00a0executed step by step and I have seen that the previous code is executing right, but the parameter is not having any effect on the query results.\u00a0Does somebody knows what is going wrong?Thank you<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello every1, I have the following function to read records from a Table: public ArrayList LeerDatos(int pNumero) { ArrayList libros = new ArrayList(); string strSql = &#8220;Select codLibro, nombreLibro, ubicacion, comentarios, &#8220;; strSql += &#8220;idioma, editorial, fechaPublicacion, fechaAlta from Libro &#8220;; cmd.CommandText = strSql; cmd.Connection = conn; cmd.Parameters.Clear(); if (pNumero != 0) { strSql += [&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-4463","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4463","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=4463"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4463\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}