What language/platform are you coming from? You mentioned you were new to List<T>, 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.
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<Model.customer>? A List<> is like an array or a collection of items. Don't return a collection
if you only want to return one.
Your return type is also wrong here. I assume this is the problem you still face.
melvintcs
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;
}
}
}
error: CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<Model.customer>' to 'Model.customer'
This error means that you are trying to return a list, but your declared return type is Model.customer. You should use:
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<Model.customer>.
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);
}
//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;
}
}
}
You were almost there. Pay attention to your return types and make sure they are the same as the value you are returning.
fred-dk
Member
37 Points
16 Posts
Re: web services return more than 1 record
May 14, 2012 01:45 PM|LINK
@melvintcs
You're welcome :) Please mark the topic as answered if you're satisfied with the help.
melvintcs
Member
182 Points
238 Posts
Re: web services return more than 1 record
May 15, 2012 04:01 AM|LINK
@fred-dk
how to solve that minor error? should i modified the customer.cs to List<> also?
how can i do so?
fred-dk
Member
37 Points
16 Posts
Re: web services return more than 1 record
May 15, 2012 09:17 AM|LINK
Yes, since we actually return a List<Model.customer> the signature should read
public static List<Model.customer> GetCustomer(string custID) { ... }melvintcs
Member
182 Points
238 Posts
Re: web services return more than 1 record
May 15, 2012 10:54 AM|LINK
i already apply this line of code at DAL.cs and BAL.cs,
but i cant apply at customer.cs :( , already import System.Collections.Generic
panesofglass
Member
730 Points
237 Posts
Re: web services return more than 1 record
May 15, 2012 11:05 AM|LINK
What language/platform are you coming from? You mentioned you were new to List<T>, 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.
melvintcs
Member
182 Points
238 Posts
Re: web services return more than 1 record
May 15, 2012 11:27 AM|LINK
yes, it's a c#. returning a list of record is what i wanted. result >1 record
i think i almost there, but a little problem with the DAL.cs, got some minor error at that class.
panesofglass
Member
730 Points
237 Posts
Re: web services return more than 1 record
May 15, 2012 11:56 AM|LINK
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<Model.customer>? A List<> is like an array or a collection of items. Don't return a collection if you only want to return one.
should be:
public static Model.customer GetCustomer(string custID) { Model.customer cust = Data.DAL.GetCustomer(custID); cust.companyName = cust.companyName; return cust; }panesofglass
Member
730 Points
237 Posts
Re: web services return more than 1 record
May 15, 2012 11:59 AM|LINK
Your return type is also wrong here. I assume this is the problem you still face.
This error means that you are trying to return a list, but your declared return type is Model.customer. You should use:
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<Model.customer>. 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); } //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; } } }You were almost there. Pay attention to your return types and make sure they are the same as the value you are returning.
melvintcs
Member
182 Points
238 Posts
Re: web services return more than 1 record
May 15, 2012 12:40 PM|LINK
i uploaded my file, have a look
http://max-5.com.my/scripts/App_Code.rar