0

I need to pass an object as a parameter to the web service web method..i have no issues doing that.. the problem i am facing is.. i need to know from where i should pass the values of the class

for example i have a class person which has 2 attributes firstname and lastname..

i am passing this person object to the webmethod something like

[webmethod]
public string setData(Person p)
    {
//insert firstname and last name to database
}

now my question is from where can i assign values to the person class so that i can get it into the database?

Thanks

  • 2
    Your question is not clear: do you want to know how to insert data into a database? (The title is, 'pass an object to webservices C#' but you seem to know how to do that already?). What exactly are you having difficulties with, and what have you tried to solve it already? – Kieren Johnstone Jan 26 '12 at 20:13
  • pass the values to the webmethod..like how to pass the first and the last name.. to the webmethod.. i know how to insert into the database as well.. i mean i am not getting the test method to pass the values as object is a complex type.. –  Jan 26 '12 at 20:19
  • There should be no problem passing a complex type over to a WCF service, so long as it is serialisable. Which specific problem, error or exception are you having when trying to pass it? – Kieren Johnstone Jan 26 '12 at 20:21
  • I would think it would be as simple as instantiating a Person object, setting the values in that object, and then passing the object to the method...is there something unusual in your service or code that is preventing you from doing that? – Tim Jan 26 '12 at 20:26

1 Answers1

0

I think you should pass the values through the class to webservice. And you can extract values from the class and send to the database query. This will give you more efficiency over adding and removing parameters.

fun ServiceCall()
{
    Person P
    P.fName='First Name'
    p.lName='Last Name'
    serviceClas.setData(P)
}

public string setData(Person p)    
{ 
    //insert firstname and last name to database 

    SqlCommand command = new SqlCommand();    
    SqlCommand command = new SqlCommand  ("Proc_name", connection);      
    command.CommandType = CommandType.StoredProcedure;    
    SqlParameter parameter1 = new SqlParameter();    
    parameter.ParameterName = "@Param1";     
    parameter.SqlDbType = SqlDbType.NVarChar;
    parameter.Direction = ParameterDirection.Input;     
    parameter.Value = P.fName; 
} 
Tim
  • 28,212
  • 8
  • 63
  • 76
Rohit Ramname
  • 824
  • 2
  • 9
  • 24
  • 1
    -1 for some of the worst code I've ever seen. Use of `fun`, missing semicolons, strings with single quotes, creating a command *twice* but not executing it at all, I could go on.. – Kieren Johnstone Jan 26 '12 at 20:30
  • (Not being too picky, after all you're proposing this as code to *help* someone. It doesn't stand up) – Kieren Johnstone Jan 26 '12 at 20:31