Insert data to Database using Restful Web Service in ASP.Net

Last Reply on Jun 15, 2017 08:43 AM By AnandM

Posted on Jun 15, 2017 07:36 AM



I am making restful api through which i want to insert data in sql db i have this code but its not working please advice what i am doing wrong

 [WebInvoke(Method="Post", RequestFormat=WebMessageFormat.Json, UriTemplate= "AddCountry/{countryname}", ResponseFormat= WebMessageFormat.Json,BodyStyle =WebMessageBodyStyle .Wrapped)]
        public void addcountry(string countryname)

            string constring = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constring))
                using (SqlCommand cmd = new SqlCommand("INSERT INTO Country (CountryName) VALUES (@CountryName)", con))
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@CountryName", countryname);
                    int rowsAffected = cmd.ExecuteNonQuery();