i'm developing a .net webservices, but the result always return the first record only, how can i fix the while loop so it can return more than 1? below is the sample code: 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
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 () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
//public Model.customer GetCustomer(string custID)
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;
}
}
}
You have to return the List<T>
instead of single instance so change the return type of your WebMethod to List<Model.customer>.
EDIT:
Use parameters/precompiled sql statement or stored procedure to avoid the SQL injection attacks. (ie. never use hardcoded sql string.)
Always use using statement to dispose the disposable object(s) properly.
public static List<Model.customer> GetCustomers(String type)
{
string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
List<Model.customer> list=new List<Model.customer>();
using (SqlConnection cn = new SqlConnection(cs))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM member WHERE userType=@userType", cn))
{
cmd.Parameters.Add("@userType",System.Data.SqlDbType.VarChar,20).Value=type;
cn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Model.customer cust = new Model.customer();
cust.fullName = reader["fullName"].ToString();
cust.handPhone = reader["handphone"].ToString();
cust.NRIC = reader["NRIC"].ToString();
list.Add(cust);
}
}
}
}
return list;
}