im dealing with web services and i face some problem with the List<>
DAL.cs
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["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<Model.customer> list = new List<Model.customer>();
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 && conn.State != ConnectionState.Closed) conn.Close();
}
return null;
}
}
}
customer.cs
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; }
}
}
}
Service.cs
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<Model.customer> GetCustomer(string custID)
{
return Biz.BAL.GetCustomer(custID);
}
}
BAL.cs
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<Model.customer> GetCustomer(string custID)
{
Model.customer cust = Data.DAL.GetCustomer(custID);
cust.companyName = cust.companyName;
List<Model.customer> myList = new List<Model.customer>();
myList.Add(cust);
return myList;
}
}
}
error: CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<Model.customer>' to 'Model.customer'
Change the GetCustomer function like the following:
namespace Data
{
public class DAL
{
public static List<Model.customer> 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<Model.customer> list = new List<Model.customer>();
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);
}
return list;
}
catch (Exception e)
{
HttpContext.Current.Trace.Warn("Error", "error in getcustomer()", e);
}
finally
{
if (reader != null) reader.Close();
if (conn != null && conn.State != ConnectionState.Closed) conn.Close();
}
return null;
}
}
}
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.
Please Mark As Answer if it helped.
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
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.
melvintcs
Member
182 Points
238 Posts
web services return more than 1 record
May 12, 2012 07:13 AM|LINK
im dealing with web services and i face some problem with the List<>
DAL.cs
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["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<Model.customer> list = new List<Model.customer>(); 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 && conn.State != ConnectionState.Closed) conn.Close(); } return null; } } }customer.cs
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; } } } }
Service.cs
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<Model.customer> GetCustomer(string custID) { return Biz.BAL.GetCustomer(custID); } }BAL.cs
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<Model.customer> GetCustomer(string custID) { Model.customer cust = Data.DAL.GetCustomer(custID); cust.companyName = cust.companyName; List<Model.customer> myList = new List<Model.customer>(); myList.Add(cust); return myList; } } }error: CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<Model.customer>' to 'Model.customer'
adeelehsan
All-Star
18221 Points
2724 Posts
Re: web services return more than 1 record
May 12, 2012 09:06 AM|LINK
Make sure that return type of this method is correct. It should be List<Model.customer> not Model.customer
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
melvintcs
Member
182 Points
238 Posts
Re: web services return more than 1 record
May 12, 2012 10:00 AM|LINK
hi, i update the first thread with all the class file, please advice me :)
im new to List<>
adeelehsan
All-Star
18221 Points
2724 Posts
Re: web services return more than 1 record
May 12, 2012 10:36 AM|LINK
Change the GetCustomer function like the following:
namespace Data { public class DAL { public static List<Model.customer> 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<Model.customer> list = new List<Model.customer>(); 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); } return list; } catch (Exception e) { HttpContext.Current.Trace.Warn("Error", "error in getcustomer()", e); } finally { if (reader != null) reader.Close(); if (conn != null && conn.State != ConnectionState.Closed) conn.Close(); } return null; } } }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.
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
melvintcs
Member
182 Points
238 Posts
Re: web services return more than 1 record
May 12, 2012 10:58 AM|LINK
(actually the custID is memberType, i not yet change the variable, i wanted to list down all the member Type = 'member')
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.
public static List<Model.customer> GetCustomer(string custID) { Model.customer cust = Data.DAL.GetCustomer(custID); cust.companyName = cust.companyName; return cust; }melvintcs
Member
182 Points
238 Posts
Re: web services return more than 1 record
May 12, 2012 12:29 PM|LINK
i think i need to update the customer.cs, but i duno how to.. :(
panesofglass
Member
730 Points
237 Posts
Re: web services return more than 1 record
May 12, 2012 01:24 PM|LINK
melvintcs
Member
182 Points
238 Posts
Re: web services return more than 1 record
May 13, 2012 05:22 AM|LINK
i never learn f# before, and new to List<> ...
or can i modified the customer.cs to something like this?
private string _customerID; private string _companyName; private string _contactName; List<int> list = new List<int>(); 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; } }fred-dk
Member
37 Points
16 Posts
Re: web services return more than 1 record
May 14, 2012 11:16 AM|LINK
@melvintcs
Your function is declared to return an object fo type "List<Model.customer>". It currently returns an object of type "Model.customer"
To return a list change the code:
public static List<Model.customer> GetCustomer(string custID) { Model.customer cust = Data.DAL.GetCustomer(custID); cust.companyName = cust.companyName; //return cust;melvintcs
Member
182 Points
238 Posts
Re: web services return more than 1 record
May 14, 2012 11:55 AM|LINK
Thanks, i took ur advice and modified the code, but still but minor error at DAL.cs, have a look :)