web services return more than 1 recordhttp://forums.asp.net/t/1802988.aspx/1?web+services+return+more+than+1+recordTue, 15 May 2012 12:40:13 -040018029884977508http://forums.asp.net/p/1802988/4977508.aspx/1?web+services+return+more+than+1+recordweb services return more than 1 record <p>im dealing with web services and i face some problem with the List&lt;&gt;</p> <p>DAL.cs</p> <pre class="prettyprint">using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Collections.Generic; namespace Data { public class DAL { public static Model.customer GetCustomer(string custID) { string cs = ConfigurationManager.ConnectionStrings[&quot;ConnectionString&quot;].ConnectionString; SqlConnection conn = null; SqlDataReader reader = null; try { conn = new SqlConnection(cs); string sql = &quot;SELECT * FROM member WHERE userType = '&quot; &#43; custID &#43; &quot;'&quot;; SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); reader = cmd.ExecuteReader(); List&lt;Model.customer&gt; list = new List&lt;Model.customer&gt;(); while (reader.Read()) { Model.customer cust = new Model.customer(); cust.customerID = reader[&quot;fullName&quot;].ToString(); cust.contactName = reader[&quot;handphone&quot;].ToString(); cust.companyName = reader[&quot;NRIC&quot;].ToString(); list.Add(cust); } //error is here return list; //end of error } catch (Exception e) { HttpContext.Current.Trace.Warn(&quot;Error&quot;, &quot;error in getcustomer()&quot;, e); } finally { if (reader != null) reader.Close(); if (conn != null &amp;&amp; conn.State != ConnectionState.Closed) conn.Close(); } return null; } } }</pre> <pre class="prettyprint"><br /><br /></pre> <p>customer.cs</p> <pre class="prettyprint">using System; namespace Model { public class customer { private string _customerID; private string _companyName; private string _contactName; public string customerID { get { return _customerID; } set { _customerID = value; } } public string companyName { get { return _companyName; } set { _companyName = value; } } public string contactName { get { return _contactName; } set { _contactName = value; } } } }<br /><br /></pre> <p>Service.cs</p> <pre class="prettyprint">using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Collections.Generic; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { public Service () { } [WebMethod] public static List&lt;Model.customer&gt; GetCustomer(string custID) { return Biz.BAL.GetCustomer(custID); } }</pre> <p>BAL.cs</p> <pre class="prettyprint">using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Collections.Generic; namespace Biz { public class BAL { public static List&lt;Model.customer&gt; GetCustomer(string custID) { Model.customer cust = Data.DAL.GetCustomer(custID); cust.companyName = cust.companyName; List&lt;Model.customer&gt; myList = new List&lt;Model.customer&gt;(); myList.Add(cust); return myList; } } }</pre> <p>error:&nbsp;CS0029: Cannot implicitly convert type 'System.Collections.Generic.List&lt;Model.customer&gt;' to 'Model.customer'</p> 2012-05-12T07:13:10-04:004977603http://forums.asp.net/p/1802988/4977603.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>Make sure that return type of this method is correct. It should be List&lt;Model.customer&gt; not Model.customer</p> <p></p> 2012-05-12T09:06:17-04:004977637http://forums.asp.net/p/1802988/4977637.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>hi, i update the first thread with all the class file, please advice me :)</p> <p>im new to List&lt;&gt;</p> 2012-05-12T10:00:59-04:004977683http://forums.asp.net/p/1802988/4977683.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>Change the GetCustomer function like the following:</p> <pre class="prettyprint">namespace Data { public class DAL { public static List&lt;Model.customer&gt; GetCustomer(string custID) { string cs = ConfigurationManager.ConnectionStrings[&quot;ConnectionString&quot;].ConnectionString; SqlConnection conn = null; SqlDataReader reader = null; try { conn = new SqlConnection(cs); string sql = &quot;SELECT * FROM member WHERE userType = '&quot; &#43; custID &#43; &quot;'&quot;; SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); reader = cmd.ExecuteReader(); List&lt;Model.customer&gt; list = new List&lt;Model.customer&gt;(); while (reader.Read()) { Model.customer cust = new Model.customer(); cust.customerID = reader[&quot;fullName&quot;].ToString(); cust.contactName = reader[&quot;handphone&quot;].ToString(); cust.companyName = reader[&quot;NRIC&quot;].ToString(); list.Add(cust); } return list; } catch (Exception e) { HttpContext.Current.Trace.Warn(&quot;Error&quot;, &quot;error in getcustomer()&quot;, e); } finally { if (reader != null) reader.Close(); if (conn != null &amp;&amp; conn.State != ConnectionState.Closed) conn.Close(); } return null; } } }</pre> <p>The above method is returning a List of customers. If you want to return one customer, then keep the function return type as Model.customer and do not use List. Because List is only used when you are dealing with multiple recrods. Here it seems custID will have only one record.<br> <br> </p> <p></p> 2012-05-12T10:36:48-04:004977706http://forums.asp.net/p/1802988/4977706.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>(actually the custID is memberType, i not yet change the variable, i wanted to list down all the member Type = 'member')</p> <p>thanks for the reply, i used ur code, but now the error is transfer to BAL.cs so i modifed the code as below, unfortunately still got error.&nbsp;</p> <pre class="prettyprint">public static List&lt;Model.customer&gt; GetCustomer(string custID) { Model.customer cust = Data.DAL.GetCustomer(custID); cust.companyName = cust.companyName; return cust; }</pre> <p><br> <br> </p> 2012-05-12T10:58:21-04:004977777http://forums.asp.net/p/1802988/4977777.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>i think i need to update the customer.cs, but i duno how to.. &nbsp;:(</p> 2012-05-12T12:29:24-04:004977819http://forums.asp.net/p/1802988/4977819.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record Pay attention to your method return types. Earlier your method expected a single customer and you returned a list. Now your method expects a list, and you are returning a single customer. You may want to consider F#, which has strong type inference so that you don't have to keep declaring the return type incorrectly. 2012-05-12T13:24:53-04:004978112http://forums.asp.net/p/1802988/4978112.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>i never learn f# before, and new to List&lt;&gt; ...</p> <p>or can i modified the customer.cs to something like this?</p> <pre class="prettyprint">private string _customerID; private string _companyName; private string _contactName; List&lt;int&gt; list = new List&lt;int&gt;(); List.Add(_customerID); List.Add(_companyName); List.Add(_contactName); public string customerID { get { return _customerID; } set { _customerID = value; } } public string companyName { get { return _companyName; } set { _companyName = value; } } public string contactName { get { return _contactName; } set { _contactName = value; } }</pre> <p><br> <br> </p> 2012-05-13T05:22:34-04:004979665http://forums.asp.net/p/1802988/4979665.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>@melvintcs</p> <p>Your function is declared to return an object fo type &quot;List&lt;Model.customer&gt;&quot;. It currently returns an object of type &quot;Model.customer&quot;</p> <p>To return a list change the code:</p> <pre class="prettyprint">public static List&lt;Model.customer&gt; GetCustomer(string custID) { Model.customer cust = Data.DAL.GetCustomer(custID); cust.companyName = cust.companyName; //return cust;</pre> <pre class="prettyprint">//create the list object List&lt;Model.customer&gt; myList = new List&lt;Model.customer&gt;(); </pre> <pre class="prettyprint">//add the customer object to the list. You can add more if you like.</pre> <pre class="prettyprint">myList.Add(cust);</pre> <pre class="prettyprint">//return the list return myList; }</pre> <p><br> <br> </p> 2012-05-14T11:16:50-04:004979750http://forums.asp.net/p/1802988/4979750.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>Thanks, i took ur advice and modified the code, but still but minor error at DAL.cs, have a look :)</p> <p></p> 2012-05-14T11:55:41-04:004979970http://forums.asp.net/p/1802988/4979970.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>@melvintcs</p> <p>You're welcome :) Please mark the topic as answered if you're satisfied with the help.</p> <p></p> 2012-05-14T13:45:15-04:004980753http://forums.asp.net/p/1802988/4980753.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>@fred-dk</p> <p>how to solve that minor error? should i modified the customer.cs to List&lt;&gt; also?&nbsp;</p> <p>how can i do so?&nbsp;</p> 2012-05-15T04:01:57-04:004981297http://forums.asp.net/p/1802988/4981297.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>Yes, since we actually return a List&lt;Model.customer&gt; the signature should read&nbsp;</p> <pre class="prettyprint">public static List&lt;Model.customer&gt; GetCustomer(string custID) { ... }</pre> <p><br> <br> </p> 2012-05-15T09:17:03-04:004981514http://forums.asp.net/p/1802988/4981514.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>i already apply this line of code at DAL.cs and BAL.cs,&nbsp;</p> <p>but i cant apply at customer.cs :( , already import&nbsp;System.Collections.Generic</p> 2012-05-15T10:54:47-04:004981535http://forums.asp.net/p/1802988/4981535.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>What language/platform are you coming from? You mentioned you were new to List&lt;T&gt;, but most of your questions indicate you are somewhat new to C#. A method named GetCustomer indicates you want a single customer, but you are returning a list instead. Why? Perhaps by knowing where you are coming from will help us provide answers that make more sense.</p> 2012-05-15T11:05:46-04:004981577http://forums.asp.net/p/1802988/4981577.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>yes, it's a c#. returning a list of record is what i wanted. &nbsp;result &gt;1 record</p> <p>i think i almost there, but a little problem with the DAL.cs, got some minor error at that class.</p> 2012-05-15T11:27:03-04:004981643http://forums.asp.net/p/1802988/4981643.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>You are still specifying your return type as different than the actual returned value. Here, you return cust, which is of type Model.customer. Why then do you put List&lt;Model.customer&gt;? A List&lt;&gt; is like an array or a collection of items. Don't return a collection if you only want to return one.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>melvintcs</h4> <p></p> <pre class="prettyprint">public static List&lt;Model.customer&gt; GetCustomer(string custID) { Model.customer cust = Data.DAL.GetCustomer(custID); cust.companyName = cust.companyName; return cust; }</pre> <p></blockquote></p> <p>should be:</p> <pre class="prettyprint">public static Model.customer GetCustomer(string custID) { Model.customer cust = Data.DAL.GetCustomer(custID); cust.companyName = cust.companyName; return cust; }</pre> <p><br> <br> </p> </blockquote> 2012-05-15T11:56:22-04:004981650http://forums.asp.net/p/1802988/4981650.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>Your return type is also wrong here. I assume this is the problem you still face.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>melvintcs</h4> <p></p> <p>im dealing with web services and i face some problem with the List&lt;&gt;</p> <p>DAL.cs</p> <pre class="prettyprint">using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Collections.Generic; namespace Data { public class DAL { public static Model.customer GetCustomer(string custID) { string cs = ConfigurationManager.ConnectionStrings[&quot;ConnectionString&quot;].ConnectionString; SqlConnection conn = null; SqlDataReader reader = null; try { conn = new SqlConnection(cs); string sql = &quot;SELECT * FROM member WHERE userType = '&quot; &#43; custID &#43; &quot;'&quot;; SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); reader = cmd.ExecuteReader(); List&lt;Model.customer&gt; list = new List&lt;Model.customer&gt;(); while (reader.Read()) { Model.customer cust = new Model.customer(); cust.customerID = reader[&quot;fullName&quot;].ToString(); cust.contactName = reader[&quot;handphone&quot;].ToString(); cust.companyName = reader[&quot;NRIC&quot;].ToString(); list.Add(cust); } //error is here return list; //end of error } catch (Exception e) { HttpContext.Current.Trace.Warn(&quot;Error&quot;, &quot;error in getcustomer()&quot;, e); } finally { if (reader != null) reader.Close(); if (conn != null &amp;&amp; conn.State != ConnectionState.Closed) conn.Close(); } return null; } } }</pre> <p>error:&nbsp;CS0029: Cannot implicitly convert type 'System.Collections.Generic.List&lt;Model.customer&gt;' to 'Model.customer'</p> <p></blockquote></p> <p>This error means that you are trying to return a list, but your declared return type is Model.customer. You should use:</p> <pre class="prettyprint">using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Collections.Generic; namespace Data { public class DAL { // Here I've changed your return type from Model.customer to List&lt;Model.customer&gt;. public static List&lt;Model.customer&gt; GetCustomer(string custID) { string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection conn = null; SqlDataReader reader = null; try { conn = new SqlConnection(cs); string sql = "SELECT * FROM member WHERE userType = '" + custID + "'"; SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); reader = cmd.ExecuteReader(); List&lt;Model.customer&gt; list = new List&lt;Model.customer&gt;(); while (reader.Read()) { Model.customer cust = new Model.customer(); cust.customerID = reader["fullName"].ToString(); cust.contactName = reader["handphone"].ToString(); cust.companyName = reader["NRIC"].ToString(); list.Add(cust); } //error is here return list; //end of error } catch (Exception e) { HttpContext.Current.Trace.Warn("Error", "error in getcustomer()", e); } finally { if (reader != null) reader.Close(); if (conn != null &amp;&amp; conn.State != ConnectionState.Closed) conn.Close(); } return null; } } } </pre> <p>You were almost there. Pay attention to your return types and make sure they are the same as the value you are returning.</p> <p></p> </blockquote> 2012-05-15T11:59:34-04:004981751http://forums.asp.net/p/1802988/4981751.aspx/1?Re+web+services+return+more+than+1+recordRe: web services return more than 1 record <p>i uploaded my file, have a look</p> <p><a href="http://max-5.com.my/scripts/App_Code.rar" title="http://max-5.com.my/scripts/App_Code.rar">http://max-5.com.my/scripts/App_Code.rar</a></p> 2012-05-15T12:40:13-04:00