I have an ASP.NET Core app project, SQL Server in the backend and React as the frontend.
When I pass the parameters (must be 1 or 0) to SetGameResults
method through Swagger UI (browser) the database accepts the parameters and the table is filled.
But when I pass the parameters through the React client-side to ASP.NET, I get this error:
Inner Exception: Procedure or function 'Set_Game_Result' expects parameter '@user1_res', which was not supplied.
Controller.cs
:
[HttpPost]
[Route("SetGameResults")]
public string SetGameResults(byte? user1, byte? user2)
{
if (!(user1 >= 1 && user2 >= 1)) // it passes this condition -
{
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("tictaktoedb1");
SqlDataReader myReader;
try
{
using (SqlConnection myCon = new SqlConnection(sqlDataSource))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand("Set_Game_Result", myCon))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@user1_res", user1);
myCommand.Parameters.AddWithValue("@user2_res", user2);
myReader = myCommand.ExecuteReader();
table.Load(myReader);
myReader.Close();
if (table.Rows.Count > 0)
{
var JsonResoltString = JsonConvert.SerializeObject(table);
return JsonResoltString;
}
return "No data sent and table returned empty";
}
}
}
catch (SqlException ex)
{
return "Inner Exception: " + ex.Message;
}
catch (Exception ex)
{
return $"Outer Exception: " + ex.Message;
}
}
return "No data sent";
}
React Native
let data= {
us1: gameResult.us1Win,
us2: gameResult.us2Win,
};
const fetchResponse = await fetch(URL, {
body: JSON.stringify(data),
method: 'POST',
headers: {
Accept: 'application/json',
'Content-type': 'application/json; charset=UTF-8',
},
});
const data = await fetchResponse.json();
Be glad for some explanations about that, thanks.