473,425 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Error while passing sql parameters to stored procedure in WCF service

375 256MB
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.SqlTypes.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 KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.

I provide the snapshot of the WCF service, The below is IService
Expand|Select|Wrap|Line Numbers
  1. namespace wcfstoredprocedure
  2. {
  3.     [ServiceContract]
  4.     public interface IService1
  5.     {
  6.         [OperationContract]
  7.         Employee GetReturnValues(string storedprocedure, SqlParameter[] sqlparams);
  8.    }
  9.  
  10.     [DataContract]
  11.     public class Employee
  12.     {
  13.         [DataMember]
  14.         private int empId;
  15.         public int EmpId
  16.         {
  17.             get { return empId; }
  18.             set { empId = value; }
  19.         }
  20.         [DataMember]
  21.         private string empName;
  22.         public string EmpName
  23.         {
  24.             get { return empName; }
  25.             set { empName = value; }
  26.         }
  27.         [DataMember]
  28.         private DateTime empJoinDate;
  29.         public DateTime EmpJoinDate
  30.         {
  31.             get { return empJoinDate; }
  32.             set { empJoinDate = value; }
  33.         }
  34.         [DataMember]
  35.         private int empOnDuty;
  36.         public int EmpOnDuty
  37.         {
  38.             get { return empOnDuty; }
  39.             set { empOnDuty = value; }
  40.         }
  41.     }
  42. }
  43.  
The Below code is of Service1.cs

Expand|Select|Wrap|Line Numbers
  1. namespace wcfstoredprocedure
  2. {
  3.     // 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.
  4.     public class Service1 : IService1
  5.     {
  6.         DataTable dt = new DataTable();
  7.         public Employee GetReturnValues(string storedprocedure, SqlParameter[] sqlparams)
  8.         {
  9.             Employee emp = new Employee();
  10.             using (SqlConnection sqlCon=new SqlConnection(@"server=ABC-415D0247602\SQLEXPRESS;integrated security=true;database=Employee"))
  11.             {
  12.                 using (SqlCommand sqlCom = new SqlCommand("GetEmpDuty", sqlCon))
  13.                 {
  14.  
  15.                     SqlDataAdapter da = new SqlDataAdapter(sqlCom);
  16.                      da.Fill(dt);
  17.                 }
  18.             }
  19.             emp.EmpId =Convert.ToInt32(dt.Rows[0]["Id"]);
  20.             emp.EmpJoinDate =Convert.ToDateTime(dt.Rows[0]["joindate"]);
  21.             emp.EmpName = dt.Rows[0]["Name"].ToString();
  22.             emp.EmpOnDuty = Convert.ToInt32(dt.Rows[0]["onduty"]);
  23.  
  24.             return emp;
  25.         }
  26.     }
  27. }
  28.  


The below is the code for the Client

Expand|Select|Wrap|Line Numbers
  1. ServiceReference1.Employee sq = new WebApplicationstoredprocedure.ServiceReference1.Employee();
  2. ServiceReference1.Service1Client sc = new WebApplicationstoredprocedure.ServiceReference1.Service1Client();
  3. SqlParameter[] sqlparams=new SqlParameter[1];
  4.  
  5. sqlparams[0] = new SqlParameter("@Id", SqlDbType.Int);
  6. sqlparams[0].Value = 1;
  7. sq = sc.GetReturnValues("GetEmpDuty", sqlparams);
  8. Response.Write(sq.empId.ToString());
  9. Response.Write("<br>");
  10. Response.Write(sq.empJoinDate.ToString());
  11. Response.Write("<br>");
  12. Response.Write(sq.empName.ToString());
  13. Response.Write("<br>");
  14. Response.Write(sq.empOnDuty.ToString());
  15.  
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
Nov 1 '09 #1
3 5033
Plater
7,872 Expert 4TB
Not knowing much about it, what if you took a single instance of an SqlCommand (which would have the procedure name and the parameters stored in itself) instead of a string and SqlParameter[] ?
Nov 3 '09 #2
cmrhema
375 256MB
Hi Plater ,
Thanks for the reply.
I have changed the whole service as below

My service looks as (Iservice.cs)

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.Text;
  7. using System.Data.SqlClient;
  8.  
  9. namespace wcfstoredprocedure
  10. {
  11.     [ServiceContract]
  12.     public interface IService1
  13.     {
  14.         [OperationContract]
  15.         void GetReturnValues(string storedprocedure, Employee[] empname);
  16.    }
  17.  
  18.     [DataContract]
  19.     public class Employee
  20.     {
  21.         [DataMember]
  22.         public string Paraname;
  23.         [DataMember]
  24.         public string Paravalue;
  25.         [DataMember]
  26.         public string Paratype;
  27.     }
  28. }
  29.  
Service.svc.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.Text;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9. using System.Data.SqlTypes;
  10.  
  11. namespace wcfstoredprocedure
  12. {
  13.     // 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.
  14.  
  15.     public class Service1 : IService1
  16.     {
  17.  
  18.         public void GetReturnValues(string storedprocedure, Employee[] empname)
  19.         {
  20.  
  21.             Employee emp = new Employee();
  22.             using (SqlConnection sqlCon = new SqlConnection(@"server=ABC-415D0247602\SQLEXPRESS;integrated security=true;database=Employee"))
  23.             {
  24.                 using (SqlCommand sqlCom = new SqlCommand(storedprocedure, sqlCon))
  25.                 {
  26.                     sqlCon.Open();
  27.                     for (int i = 0; i < empname.Length; i++)
  28.                     {
  29.                         sqlCom.Parameters.Add(empname[i].Paraname, empname[i].Paratype);
  30.                         sqlCom.Parameters[i].Value = empname[i].Paravalue;
  31.                     }
  32.                     sqlCom.ExecuteNonQuery();
  33.  
  34.                 }
  35.             }
  36.  
  37.         }
  38.     }
  39. }
  40.  

My Client has two files
one class file and one code behind file.

The class file
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.ServiceModel;
  6. using System.Runtime.Serialization;
  7.  
  8. namespace WebApplicationstoredprocedure
  9. {
  10.     [ServiceContract]
  11.     public interface IService1
  12.     {
  13.         [OperationContract]
  14.         void GetReturnValues(string storedprocedure, Employee[] empname);
  15.     }
  16.  
  17.     [DataContract]
  18.     public class Employee
  19.     {
  20.  
  21.        [DataMember]
  22.         public string Paraname;
  23.         [DataMember]
  24.         public string Paravalue;
  25.         [DataMember]
  26.         public string Paratype;
  27.  
  28.  
  29.     }
  30. }
The code behind file
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9. using System.Net;
  10. using System.ServiceModel;
  11.  
  12. namespace WebApplicationstoredprocedure
  13. {
  14.     public partial class _Default : System.Web.UI.Page
  15.     {
  16.         protected void Page_Load(object sender, EventArgs e)
  17.         {
  18.  
  19.  
  20.             WSHttpBinding ws = new WSHttpBinding();
  21.             EndpointAddress ep = new EndpointAddress(@"http://localhost:1150/Service1.svc");
  22.             ChannelFactory<IService1> chFactory = new ChannelFactory<IService1>(ws, ep);
  23.             IService1 channel = chFactory.CreateChannel();
  24.  
  25.             Employee[] sqlparams = new Employee[4];
  26.  
  27.             sqlparams[0] = new Employee();
  28.             sqlparams[0].Paratype = "SqlDbType.Int";
  29.             sqlparams[0].Paraname = "@Id";
  30.             sqlparams[0].Paravalue = "4";
  31.  
  32.             sqlparams[1] = new Employee();
  33.             sqlparams[1].Paratype = "SqlDbType.NVarChar";
  34.             sqlparams[1].Paraname = "@name";
  35.             sqlparams[1].Paravalue = "bb";
  36.  
  37.             sqlparams[2] = new Employee();
  38.             sqlparams[2].Paratype = "SqlDbType.DateTime";
  39.             sqlparams[2].Paraname = "@joindate";
  40.             sqlparams[2].Paravalue = "10/10/2000";
  41.  
  42.             sqlparams[3] = new Employee();
  43.             sqlparams[3].Paratype = "SqlDbType.Bit";
  44.             sqlparams[3].Paraname = "@onduty";
  45.             sqlparams[3].Paravalue = "true";
  46.             channel.GetReturnValues("InsertEmpDuty", sqlparams);
  47.  
  48.         }
  49.     }
  50. }
I am providing what exactly appears in the watch window in service
storedprocedure "InsertEmpDuty" string
empname {wcfstoredprocedure.Employee[0]} wcfstoredprocedure.Employee[]


Whereas I have passed 4 parameters to empname in client, this is the watch window of client
- sqlparams {WebApplicationstoredprocedure.Employee[4]} WebApplicationstoredprocedure.Employee[]
+ [0] {WebApplicationstoredprocedure.Employee} WebApplicationstoredprocedure.Employee
+ [1] {WebApplicationstoredprocedure.Employee} WebApplicationstoredprocedure.Employee
+ [2] {WebApplicationstoredprocedure.Employee} WebApplicationstoredprocedure.Employee
+ [3] {WebApplicationstoredprocedure.Employee} WebApplicationstoredprocedure.Employee

Its really driving me crazy.....

Regards
cmrhema
Nov 4 '09 #3
cmrhema
375 256MB
The error has been rectified
Both the service and the client were under different namespace, hence we had errors.

After sharing the same namespace, the service and client works properly
Nov 4 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bob | last post by:
I'm new to Access projects and SQL server and am not a veteran VB programmer. There's a cry for help! I'm attempting to print the current form on screen by using a command button which the user...
4
by: TJS | last post by:
trying to get a record count from a stored procedure using a supplied SQL statement Error msg: =========== "The SqlParameterCollection only accepts non-null SqlParameter type objects,not...
0
by: Mike | last post by:
Hi, I am trying to insert parameters into a stored procedure using DAAB (see code at the bottom of this post). I am getting the following error: Object reference not set to an instance of an...
1
by: Jeremy Ames | last post by:
I have a datagrid that updates the table using a stored procedure. The stored procedure is confirmed to complete correctly, yet the sql data adapter is returning an error that my application is...
0
by: Jimmy | last post by:
I have a web page that displays data from a sql server db. I'm using a stored procedure to return data from a field of type text as an OUTPUT parameter. How can I use an OUTPUT parameter of...
4
by: Magy | last post by:
What would be the best way to execute a Oracle stored procedure that excepts several input paramters, through a web method in vb.net. What would be a good way to get to the web method, the Oracle...
4
by: Ranginald | last post by:
Hi, I'm having trouble passing a parameter from my default.aspx page to my default2.aspx page. I have values from a query in a list box and the goal is to pass the "catID" from default.aspx...
7
by: Cirene | last post by:
I have a stored procedure named insertcompany. I keep getting the error: Sys.webforms.pagerequestmanagerservererrorexception: Procedure or function 'InsertCompany' expects parameter...
1
Soniad
by: Soniad | last post by:
Hello, I have stored procedure in which i am passing parameters (varchar(8000)), these parameters can have values or can be empty (''), i have written blocks of codes for each parameter , which...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.