[Solved] Consuming Web API in Code Behind in ASP.Net not working

nauna
 
on Feb 14, 2021 10:45 PM
617 Views

hello,

Following this article Call (Consume) Web API in Code Behind in ASP.Net using C#

On click of button from page behind i want to call insert, update and delete method of api in webform default.aspx page

please advice

i created this method in webapi

        [Route("api/CustomerAPI/AddUser")]
        public IHttpActionResult PostNewUser(string username, string password)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "INSERT INTO users VALUES(@username,@password)";
                    cmd.Parameters.AddWithValue("@username", username);
                    cmd.Parameters.AddWithValue("@password", password);
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }

            return Ok();
        }

        [Route("api/CustomerAPI/Updateuserbyid")]
        public IHttpActionResult Put(string id, string username, string password)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "UPDATE users SET username=@username,password=@password WHERE id=@Id";
                    cmd.Parameters.AddWithValue("@username", username);
                    cmd.Parameters.AddWithValue("@password", password);
                    cmd.Parameters.AddWithValue("@Id", id);
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }

            return Ok();
        }


        [Route("api/CustomerAPI/deleteuserbyid")]
        public IHttpActionResult Delete(string id)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "DELETE FROM users WHERE id=@Id";
                    cmd.Parameters.AddWithValue("@Id", id);
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }

            return Ok();
        }

 

<asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Update" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="Delete" OnClick="Button3_Click" />

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
nauna
 
on Feb 15, 2021 04:01 AM

i was making mistake did not define the connnection string.