473,714 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error while passing sql parameters to stored procedure in WCF service

375 Contributor
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
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 5047
Plater
7,872 Recognized Expert Expert
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 Contributor
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 "InsertEmpD uty" string
empname {wcfstoredproce dure.Employee[0]} wcfstoredproced ure.Employee[]


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

Its really driving me crazy.....

Regards
cmrhema
Nov 4 '09 #3
cmrhema
375 Contributor
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
5270
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 clicks once they have selected the desired record. The button calls a report which uses a stored procedure as its record source. The SP has 2 input parameters, one of which is a datetime data type. In the input parameters data field of the...
4
1643
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 String objects." SPROC: ========
0
1437
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 object. 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:...
1
1592
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 catching. Everything within my stored procedure is contained within a transaction. When utilizing the sql profiler, we can see that the transaction successfully begins and commits. The data in the sql table is updated at this point. My data...
0
1291
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 datatype text being returned from a stored procedure? I need to use it's value to display comments text on a profile page. The comments will be more than 8000 characters so using varchar is not an option. Maybe I'm on the wrong track...if so, then...
4
2347
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 stored procedure's definition; the name of the proc. and all of the parameter information such as name, value, type and value. I'm looking for the best method. Any ideas. Magy
4
2756
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 to a stored procedure on the details2.aspx page. I can successfully pass the values from the listbox control to a
7
1343
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 '@ApptPhone1Required', which was not supplied. But, I DID supply it along with all others.... (here's a snippet) MyCommand.Parameters.AddWithValue("@InstructionsOperator",
1
4777
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 executes only if parameter has values, it should not enter the block if parameter is empty, how to detect whether the parameter is empty or not. and also i am having output parameter (varchar(1)) that is returned by stored procedure, if all of...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9015
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5947
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.