How to pass multiple parameters on WCF Restful service using C# in ASP.Net

Aashish682
 
on Dec 05, 2016 02:17 AM
7223 Views

I have bbe searching for this a while but didnt found any solution

Any help is Appreciate.

 

  [OperationContract]
        [WebInvoke(Method = "POST",
        UriTemplate = "Addusers/{name}/{email}/{mobile}/{password}/{cityid}", ResponseFormat = WebMessageFormat.Json)]
        List<Users> Addusers(string name, string email, string mobile, string password, string cityid);

Is this method is possible to insert data in database

 

[DataContract]
    public class Users
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Email { get; set; }

        [DataMember]
        public string Mobile { get; set; }

        [DataMember]
        public string Password { get; set; }

        [DataMember]
        public string CityID { get; set; }

        [DataMember]
        public string UserID { get; set; }

 

 public List<Users> Addusers(string name, string email, string mobile, string password, string cityid)
        {
            List<Users> register = SQL.Addusers(name, email, mobile, password, cityid);
            return register;
        }

 SQL

 public static List<Users> Addusers(string name, string email, string mobile, string password, string cityid)
        {
            List<Users> register = new List<Users>();
            string connstring = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;

            using (SqlConnection connection = new SqlConnection(connstring))
            {
                using (SqlCommand command = new SqlCommand("SP_01Registeruser", connection))
                    
                {
                    connection.Open();
                    
                    DataTable dt = new DataTable();
                    command.Parameters.AddWithValue("@name", name);
                    command.Parameters.AddWithValue("@email", email);
                    command.Parameters.AddWithValue("@mobile", mobile);
                    command.Parameters.AddWithValue("@password", password);
                    command.Parameters.AddWithValue("@cityid", cityid);
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    da.Fill(dt);
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        register.Add(new Users { CityID = reader["cityid"].ToString(), UserID = reader["userid"].ToString() });

                    }
                    reader.Close();
                }
            }

            return register;
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
AnandM
 
on Dec 05, 2016 02:39 AM
Aashish682 says:
public static List<Users> Addusers(string name, string email, string mobile, string password, string cityid)
       {
           List<Users> register = new List<Users>();
           string connstring = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
 
           using (SqlConnection connection = new SqlConnection(connstring))
           {
               using (SqlCommand command = new SqlCommand("SP_01Registeruser", connection))
                    
               {
                   connection.Open();
                    
                   DataTable dt = new DataTable();
                   command.Parameters.AddWithValue("@name", name);
                   command.Parameters.AddWithValue("@email", email);
                   command.Parameters.AddWithValue("@mobile", mobile);
                   command.Parameters.AddWithValue("@password", password);
                   command.Parameters.AddWithValue("@cityid", cityid);
                   SqlDataAdapter da = new SqlDataAdapter(command);
                   da.Fill(dt);
                   SqlDataReader reader = command.ExecuteReader();
                   while (reader.Read())
                   {
                       register.Add(new Users { CityID = reader["cityid"].ToString(), UserID = reader["userid"].ToString() });
 
                   }
                   reader.Close();
               }
           }
 
           return register;
       }

 the above code which you have used for inserting the data is not correct because you are inserting the data and not returning any values so you need to use the code like below

public static void Addusers(Users users)
{
    string connstring = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;

    using (SqlConnection connection = new SqlConnection(connstring))
    {
        using (SqlCommand command = new SqlCommand("SP_01Registeruser", connection))
        {
            connection.Open();
            command.Parameters.AddWithValue("@name", users.Name);
            command.Parameters.AddWithValue("@email", users.Email);
            command.Parameters.AddWithValue("@mobile", users.Mobile);
            command.Parameters.AddWithValue("@password", users.Password);
            command.Parameters.AddWithValue("@cityid", users.CityID);
            command.ExecuteNonQuery();
            connection.Close();
        }
    }
}

and if you are returning something after inserting data then you need to do like below

public static void Addusers(Users users)
{
    string connstring = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;

    using (SqlConnection connection = new SqlConnection(connstring))
    {
        using (SqlCommand command = new SqlCommand("SP_01Registeruser", connection))
        {
            connection.Open();
            command.Parameters.AddWithValue("@name", users.Name);
            command.Parameters.AddWithValue("@email", users.Email);
            command.Parameters.AddWithValue("@mobile", users.Mobile);
            command.Parameters.AddWithValue("@password", users.Password);
            command.Parameters.AddWithValue("@cityid", users.CityID);
            
            //If you are returning single value for eg. if you are returning UserID.
            object returnedValue = command.ExecuteScalar();

            //If you are returning multiple list of users then use like below.
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt);
            List<Users> register = new List<Users>();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                register.Add(new Users { CityID = reader["cityid"].ToString(), UserID = reader["userid"].ToString() });

            }
            //use list of users where you want to use.
            connection.Close();
        }
    }
}