简体   繁体   中英

Passing lot of parameters into Web Service

I have a web service in ASP.NET a simple one but the web method takes 5 parameters. Is there any other way to pass the parameters? like array or list?. I would like to limit it to 3.

I tried SetMethod(string param) and dint work.

Thanks.

Why don't you create a Class that has those parameters as it's properties, then you would pass an instance of the class as a parameter to the web method.

Changing something like this..

[WebMethod]
public void SomeMethod(string param1, string param2, string param3)
{
    //some code
}

to something like this...

[WebMethod]
public void SomeMethod(SomeClass myClass)
{
    //some code
}

public class SomeClass
{
    public string Param1 { get; set; }
    public string Param2 { get; set; }
    public string Param3 { get; set; }
}

and you would use it like so...

SomeClass myClass = new SomeClass();
myClass.Param1 = "some data";
myClass.Param2 = "more data";
myClass.Param3 = "even more";

// make the webservice call
someObject.SomeMethod(myClass);
    List<Student> list = new List<Student>();
    [WebMethod]
    public string InsertUserDetails(Student  userInfo)
    { 
        list.Add(userInfo);
        //string Message;
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=True");
        con.Open();
        foreach (Student s in list)
        {
            SqlCommand cmd = new SqlCommand("insert into userdetail(username,password,country,email) values(@UserName,@Password,@Country,@Email)", con);
            cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
            cmd.Parameters.AddWithValue("@Password", userInfo.Password);
            cmd.Parameters.AddWithValue("@Country", userInfo.Country);
            cmd.Parameters.AddWithValue("@Email", userInfo.Email);

            int result = cmd.ExecuteNonQuery();
        }

            //if (result == 1)
            //{
            //    Message = userInfo.UserName + " Details inserted successfully";
            //}
            //else
            //{
            //    Message = userInfo.UserName + " Details not inserted successfully";
            //}
            //con.Close();

           // return Message;
        return "done";

    }

you can do like this>>>>>>>>>>>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM