Hi,
Kindly excuse if I am posting in the wrong place.
I am using Visual Studio 2008, .net framework 3.5, asp.net , c# and sql server 2005.
I am supposed to pass stored procedures from client to wcf service.
The WCF service should execute the stored procedure and return the result.
When I pass the stored procedure which does not have any parameter, it works, but the moment I pass any parameter, it throws error as below
There was an error while trying to serialize parameter http://tempuri.org/:sqlparams. The InnerException message was 'Type 'System.Data.Sq lTypes.SqlInt32 ' with data contract name 'int:http://www.w3.org/2001/XMLSchema' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttrib ute attribute or by adding them to the list of known types passed to DataContractSer ializer.'. Please see InnerException for more details.
I provide the snapshot of the WCF service, The below is IService
The Below code is of Service1.cs
The below is the code for the Client
Please do let me know where am I going wrong.
I tried to add [Serializable], but did not work.
Changed sqlparameters to object, but did not work.
Any suggestions
Regards
cmrhema
Kindly excuse if I am posting in the wrong place.
I am using Visual Studio 2008, .net framework 3.5, asp.net , c# and sql server 2005.
I am supposed to pass stored procedures from client to wcf service.
The WCF service should execute the stored procedure and return the result.
When I pass the stored procedure which does not have any parameter, it works, but the moment I pass any parameter, it throws error as below
There was an error while trying to serialize parameter http://tempuri.org/:sqlparams. The InnerException message was 'Type 'System.Data.Sq lTypes.SqlInt32 ' with data contract name 'int:http://www.w3.org/2001/XMLSchema' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttrib ute attribute or by adding them to the list of known types passed to DataContractSer ializer.'. Please see InnerException for more details.
I provide the snapshot of the WCF service, The below is IService
Code:
namespace wcfstoredprocedure { [ServiceContract] public interface IService1 { [OperationContract] Employee GetReturnValues(string storedprocedure, SqlParameter[] sqlparams); } [DataContract] public class Employee { [DataMember] private int empId; public int EmpId { get { return empId; } set { empId = value; } } [DataMember] private string empName; public string EmpName { get { return empName; } set { empName = value; } } [DataMember] private DateTime empJoinDate; public DateTime EmpJoinDate { get { return empJoinDate; } set { empJoinDate = value; } } [DataMember] private int empOnDuty; public int EmpOnDuty { get { return empOnDuty; } set { empOnDuty = value; } } } }
Code:
namespace wcfstoredprocedure { // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file. public class Service1 : IService1 { DataTable dt = new DataTable(); public Employee GetReturnValues(string storedprocedure, SqlParameter[] sqlparams) { Employee emp = new Employee(); using (SqlConnection sqlCon=new SqlConnection(@"server=ABC-415D0247602\SQLEXPRESS;integrated security=true;database=Employee")) { using (SqlCommand sqlCom = new SqlCommand("GetEmpDuty", sqlCon)) { SqlDataAdapter da = new SqlDataAdapter(sqlCom); da.Fill(dt); } } emp.EmpId =Convert.ToInt32(dt.Rows[0]["Id"]); emp.EmpJoinDate =Convert.ToDateTime(dt.Rows[0]["joindate"]); emp.EmpName = dt.Rows[0]["Name"].ToString(); emp.EmpOnDuty = Convert.ToInt32(dt.Rows[0]["onduty"]); return emp; } } }
The below is the code for the Client
Code:
ServiceReference1.Employee sq = new WebApplicationstoredprocedure.ServiceReference1.Employee(); ServiceReference1.Service1Client sc = new WebApplicationstoredprocedure.ServiceReference1.Service1Client(); SqlParameter[] sqlparams=new SqlParameter[1]; sqlparams[0] = new SqlParameter("@Id", SqlDbType.Int); sqlparams[0].Value = 1; sq = sc.GetReturnValues("GetEmpDuty", sqlparams); Response.Write(sq.empId.ToString()); Response.Write("<br>"); Response.Write(sq.empJoinDate.ToString()); Response.Write("<br>"); Response.Write(sq.empName.ToString()); Response.Write("<br>"); Response.Write(sq.empOnDuty.ToString());
I tried to add [Serializable], but did not work.
Changed sqlparameters to object, but did not work.
Any suggestions
Regards
cmrhema
Comment