hi!
I am currently following below link to learn WebAPI
Link: http://www.c-sharpcorner.com/blogs/consuming-json-rest-or-restful-web-services-response-using-net-c-sharp-client1
I am getting error while calling POST method to Add Record.
Error message:
POST method is :
[HttpPost]
public void AddEmployee(Employee employee)
{
//int maxId = listEmp.Max(e => e.ID);
//employee.ID = maxId + 1;
//listEmp.Add(employee);
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = @"Server=192.168.200.26;Database=DBCompany;User ID=xyz;Password=1234;";
//SqlCommand sqlCmd = new SqlCommand("INSERT INTO tblEmployee (EmployeeId,Name,ManagerId) Values (@EmployeeId,@Name,@ManagerId)", myConnection);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "INSERT INTO tblEmployee (EmployeeId,Name,ManagerId) Values (@EmployeeId,@Name,@ManagerId)";
sqlCmd.Connection = myConnection;
sqlCmd.Parameters.AddWithValue("@EmployeeId", employee.EmployeeId);
sqlCmd.Parameters.AddWithValue("@Name", employee.Name);
sqlCmd.Parameters.AddWithValue("@ManagerId", employee.ManagerId);
myConnection.Open();
int rowInserted = sqlCmd.ExecuteNonQuery();
myConnection.Close();
}
public void AddEmployee(Employee employee)
{
//int maxId = listEmp.Max(e => e.ID);
//employee.ID = maxId + 1;
//listEmp.Add(employee);
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = @"Server=192.168.200.26;Database=DBCompany;User ID=xyz;Password=1234;";
//SqlCommand sqlCmd = new SqlCommand("INSERT INTO tblEmployee (EmployeeId,Name,ManagerId) Values (@EmployeeId,@Name,@ManagerId)", myConnection);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "INSERT INTO tblEmployee (EmployeeId,Name,ManagerId) Values (@EmployeeId,@Name,@ManagerId)";
sqlCmd.Connection = myConnection;
sqlCmd.Parameters.AddWithValue("@EmployeeId", employee.EmployeeId);
sqlCmd.Parameters.AddWithValue("@Name", employee.Name);
sqlCmd.Parameters.AddWithValue("@ManagerId", employee.ManagerId);
myConnection.Open();
int rowInserted = sqlCmd.ExecuteNonQuery();
myConnection.Close();
}
please tell me what is the right way of consuming this WEBAPI.

Rajeev PunhaniRank #551 · 4.4K reputationPosted Jul 14, 2016, 3:50 AM EDT
If the above solution is helpful then,accept the answer.
shashi kant13 reputationPosted Jul 14, 2016, 8:26 PM EDT
Method I am having problem is AddEmployee
Bikesh SrivastavaRank #108 · 20.3K reputationPosted Jul 14, 2016, 11:11 PM EDT
Manas MohapatraRank #182 · 13.2K reputationPosted Jul 14, 2016, 11:37 PM EDT
shashi kant13 reputationPosted Jul 15, 2016, 12:54 AM EDT
POST method is :
[HttpPost] public void AddEmployee(Employee employee) { //int maxId = listEmp.Max(e => e.ID); //employee.ID = maxId + 1; //listEmp.Add(employee); SqlConnection myConnection = new SqlConnection(); myConnection.ConnectionString = @"Server=192.168.200.26;Database=DBCompany;User ID=abc;Password=1234;"; //SqlCommand sqlCmd = new SqlCommand("INSERT INTO tblEmployee (EmployeeId,Name,ManagerId) Values (@EmployeeId,@Name,@ManagerId)", myConnection); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "INSERT INTO tblEmployee (EmployeeId,Name,ManagerId) Values (@EmployeeId,@Name,@ManagerId)"; sqlCmd.Connection = myConnection; sqlCmd.Parameters.AddWithValue("@EmployeeId", employee.EmployeeId); sqlCmd.Parameters.AddWithValue("@Name", employee.Name); sqlCmd.Parameters.AddWithValue("@ManagerId", employee.ManagerId); myConnection.Open(); int rowInserted = sqlCmd.ExecuteNonQuery(); myConnection.Close(); }To consume :
I have created a class named Employee
Class Employee.cs
using System; using System.Collections.Generic; using System.Text; namespace TestWebAPIConsume { class Employee { public int EmployeeId { get; set; } public string Name { get; set; } public int ManagerId { get; set; } } }On Default.aspx Page:
protected void Page_Load(object sender, EventArgs e) { HttpClient client = new HttpClient(); string serviceUrl = "http://localhost/WebApiDemo/api/Employees/AddEmployee"; var Employee = new Employee() { EmployeeId = 44, Name = "AmitKumar", ManagerId = 21 }; try { HttpResponseMessage response = client.PostAsJsonAsync(serviceUrl, Employee).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("EmployeeAdded"); } else { Console.WriteLine("{0}({1})", (int)response.StatusCode, response.ReasonPhrase); } } catch (Exception ex) { lblmsg.Text= ex.Message.ToString(); } }Error Details:
{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
Cache-Control: no-cache
Date: Fri, 15 Jul 2016 06:55:07 GMT
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 172
Content-Type: application/json; charset=utf-8
Expires: -1
}}