This is my whole code of web api method which is creating team and i am consuming this method in client. I have tested this post method via /api/Team/insertTeam and it's adding new team in database but giving error while cunsuming with httpclient/
[HttpPost]
[Route("insertTeam")]
public int insertTeam(Team team)
{
int teamid = -1;
try
{
using (SqlConnection con = new SqlConnection(ConString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("dbo.Team", con))
{
cmd.CommandText = "INSERT INTO team(Score, wideball, Noball, name, wickets, overs) VALUES(@score, @wideball, @noball, @name, @wickets, @overs); SELECT SCOPE_IDENTITY()";
cmd.Parameters.AddWithValue("@score", team.Score);
cmd.Parameters.AddWithValue("@wideball", team.Wideball);
cmd.Parameters.AddWithValue("@noball", team.Noball);
cmd.Parameters.AddWithValue("@name", team.Name);
cmd.Parameters.AddWithValue("@wickets", team.Wickets);
cmd.Parameters.AddWithValue("@overs", team.Overs);
teamid = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
}
catch (SqlException e)
{
System.Diagnostics.Debug.WriteLine("\n----------Error-----------\n");
System.Diagnostics.Debug.WriteLine(e.Message);
}
return teamid;
}
Code of client project
private void AddTeam()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:44382/api/Team");
//Adding teams
Team team1 = new Team
{
Name = tbteam1.Text,
Score = 0,
Wideball = 0,
Noball = 0,
Wickets = 0,
Overs = 0,
};
Team team2 = new Team
{
Name = tbteam2.Text,
Score = 0,
Wideball = 0,
Noball = 0,
Wickets = 0,
Overs = 0,
};
MediaTypeFormatter[] formatter = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
HttpContent content = new ObjectContent<Team>(team1, formatter[0]);
HttpResponseMessage response = client.PostAsync(client.BaseAddress + "/insertTeam", content).Result;
}
Error at postAsync method in client code
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at Client.team.AddTeam() in K:\LiveScoreSystemWebApi\Client\team.aspx.cs:line 259
at Client.team.GoBtn_Click(Object sender, EventArgs e) in K:\LiveScoreSystemWebApi\Client\team.aspx.cs:line 51
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
HttpRequestException: An error occurred while sending the request.
Inner Exception 2:
WebException: The underlying connection was closed: An unexpected error occurred on a receive.
Inner Exception 3:
IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
Inner Exception 4:
SocketException: An existing connection was forcibly closed by the remote host
Try this code:
public async Task PostData
{
var team1 = new Team
{
Name = tbteam1.Text,
Score = 0,
Wideball = 0,
Noball = 0,
Wickets = 0,
Overs = 0,
};
using var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:44382/api/Team");
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
var stringData = JsonConvert.SerializeObject(team1);
contentData = new StringContent(stringData, Encoding.UTF8, "application/json");
var response = await client.PostAsync(client.BaseAddress + "/insertTeam", contentData);
.....
}
just change the baseaddress https from http..!!
User contributions licensed under CC BY-SA 3.0