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

Last Reply one year ago By AnandM

Posted one year ago



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();



You are viewing reply posted by: AnandM one year ago.