using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace APPRECWS
{
///
/// Summary description for Service1.
///
public class AplicationRecordWS : System.Web.Services.WebService
{
public AplicationRecordWS()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5
// [WebMethod]
// public string HelloWorld()
// {
// return "Hello World";
// }
[WebMethod]
public DataResult GetData(SearchInformation InputData)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand cmd = new SqlCommand("spFAWebServiceWC",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@BusinessSeg",InputData.BusinessSeg);
cmd.Parameters.Add("@BusinessBan",InputData.BusinessBan);
cmd.Parameters.Add("@BusinessType",InputData.BusinessType);
cmd.Parameters.Add("@ApplicatioName",InputData.ApplicatioName);
cmd.Parameters.Add("@ApplicationShortName",InputData.ApplicationShortName);
cmd.Parameters.Add("@RBS",InputData.RBS);
conn.Open();
SqlDataReader dados = cmd.ExecuteReader();
conn.Close();
DataResult resultado = new DataResult();
ApplicationInformation appInfo;
while(dados.Read())
{
appInfo = new ApplicationInformation();
appInfo.ApplicationID = int.Parse(dados["ApplicationID"].ToString());
appInfo.BusinessBan = int.Parse(dados["BusinessBan"].ToString());
appInfo.BusinessSeg = int.Parse(dados["BusinessSeg"].ToString());
appInfo.BusinessType = dados["BusinessType"].ToString();
appInfo.ApplicationName = dados["ApplicationName"].ToString();
appInfo.ApplicationShortName = dados["ApplicationShortName"].ToString();
appInfo.RBS = dados["RBS"].ToString();
appInfo.SystemType = dados["SystemType"].ToString();
appInfo.Status = dados["Status"].ToString();
resultado.Add(appInfo);
}
return resultado;
}
public class SearchInformation
{
public string BusinessSeg;
public string BusinessBan;
public string BusinessType;
public string ApplicatioName;
public string ApplicationShortName;
public string RBS;
}
public class ApplicationInformation
{
public int ApplicationID;
public int BusinessSeg;
public int BusinessBan;
public string BusinessType;
public string ApplicationName;
public string ApplicationShortName;
public string RBS;
public string SystemType;
public string Status;
}
//http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx
public class DataResult : CollectionBase, IEnumerable
{
public void Add(ApplicationInformation umRegisto)
{
List.Add(umRegisto);
}
public void Add(DataResult oRegistos)
{
for(int x=0;x
List.Add(oRegistos.Item(x));
}
}
{
if (index > Count - 1 || index < 0)
{
throw new Exception("Index out of bounds");
}
List.RemoveAt(index);
}
public ApplicationInformation Item(int index)
{
if (index > Count - 1 || index < 0)
{
throw new Exception("Index out of bounds");
}
return (ApplicationInformation) List[index];
}
}
}
}
and it gives no errors when debugging but when it tries to open the page this error happens:
Server Error in '/APPRECWS' Application.
You must implement a default accessor on APPRECWS.AplicationRecordWS+DataResult because it inherits from ICollection.
I'm using VS2003, Can anyone help me please?
Thanks in advance
Ricardo Rosa
Raaj KumarPosted Feb 26, 2010, 9:04 AM
You can do what Amit said. But if you still want to go by your way which you have done, do the following steps,
1. Since you have inherited from CollectionBase it requires a default accessor. So you have to create a property like this,
public ApplicationInformation this[int index]
{
get
{
return ((ApplicationInformation)List[index]);
}
set { ;}
}
2. Change the method name from Item to Items since this is having a conflict. So the final code of your DataResult class will be like this,
public class DataResult : CollectionBase, IEnumerable
{
public void Add(ApplicationInformation umRegisto)
{
List.Add(umRegisto);
}
public void Add(DataResult oRegistos)
{
for (int x = 0; x < oRegistos.Count; x++)
{
List.Add(oRegistos.Items(x));
}
}
public ApplicationInformation this[int index]
{
get
{
return ((ApplicationInformation)List[index]);
}
set { ;}
}
public void Remove(int index)
{
if (index > Count - 1 || index < 0)
{
throw new Exception("Index out of bounds");
}
List.RemoveAt(index);
}
public ApplicationInformation Items(int index)
{
if (index > Count - 1 || index < 0)
{
throw new Exception("Index out of bounds");
}
return (ApplicationInformation)List[index];
}
}
Try this and let me know if you face any problem
Regards,
raaj
ricardo rosaPosted Feb 26, 2010, 11:09 AM
Thanks a lot for your precious help.
Raaj, i did like that and my problem is solved.
When you need something from Portugal, just let me know.
Ricardo Rosa
Amit ChoudharyPosted Feb 26, 2010, 8:23 AM
This problem is i guess with your List as it is not getting serialized.
Generics allow you to create strong type collections of objects, List(T) is the generics equivalent to ArrayList, the important part here is that you can serialize List(T) so you can use it as the return value of your webmethod.
check this link:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
ricardo rosaPosted Feb 26, 2010, 8:00 AM
Thank you for your concern.
Here goes the full error.
Server Error in '/APPRECWS' Application.
You must implement a default accessor on APPRECWS.AplicationRecordWS+DataResult because it inherits from ICollection.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: You must implement a default accessor on APPRECWS.AplicationRecordWS+DataResult because it inherits from ICollection.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.Stack Trace:
Amit ChoudharyPosted Feb 26, 2010, 7:58 AM
Please give the detailed error information about it... like copy and paste the error with stack trace and the causing lines.