Home > Software engineering >  Postgres .NET core 3.1 web api, npgsql, cannot use POST method
Postgres .NET core 3.1 web api, npgsql, cannot use POST method

Time:02-23

I'm running into a problem. I have .net core 3.1 web api that can successfully use GET method to fetch participant table data from a postgres localhost, but I cannot use POST method. Postman throws error ""Parameter '@FirstName' must have its value set"", despite giving it non null input. I'm not sure if the problem is I don't use postman properly or the C# code is wrong. I'm a newbie in .NET core 3.1 so any help is more than welcome.

Postman Post error screenshot

Participant table:

-- Table: public.Participant

-- DROP TABLE IF EXISTS public."Participant";

CREATE TABLE IF NOT EXISTS public."Participant"
(
    "Id" integer NOT NULL,
    "First Name" character varying COLLATE pg_catalog."default" NOT NULL,
    "Last Name" character varying COLLATE pg_catalog."default" NOT NULL,
    "Date Of Birth" date,
    "Sex" character varying COLLATE pg_catalog."default",
    CONSTRAINT "Participant_pkey" PRIMARY KEY ("First Name", "Last Name"),
    CONSTRAINT "Id" UNIQUE ("Id")
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public."Participant"
    OWNER to postgres;

Post Method:


using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Npgsql;
using Assignment.Models;
using System;
using System.Data;

namespace Assignment.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ParticipantController : ControllerBase
    {
        private readonly IConfiguration _configuration;
        private readonly IWebHostEnvironment _env;
        public ParticipantController(IConfiguration configuration, IWebHostEnvironment env)
        {
            _configuration = configuration;
            _env = env;
        }

        [HttpGet]
        public JsonResult Get()
        {
            string query = @"
                select * from ""Participant""
            ";

            try
            {
                //create datatable and configuration stirng
                DataTable table = new DataTable();
                string sqlDataSource = _configuration.GetConnectionString("ParticipantConnectionString");

                //initialize npgsql reader
                NpgsqlDataReader myReader;
                using (NpgsqlConnection myCon = new NpgsqlConnection(sqlDataSource))
                {
                    myCon.Open();

                    //create command from given query and connection
                    using (NpgsqlCommand myCommand = new NpgsqlCommand(query, myCon))
                    {
                        myReader = myCommand.ExecuteReader();
                        table.Load(myReader);

                        myReader.Close();
                        myCon.Close();

                    }
                }

                return new JsonResult(table);
            }
            catch (Exception e)
            {
                return new JsonResult(e);
            }
            
        }


        [HttpPost]
        public JsonResult Post(Participant participant)
        {
            string query = @"
                insert into ""Participant"" 
                (""Id"", ""First Name"", ""Last Name"", ""Date Of Birth"", ""Sex"")
                values (@Id,@FirstName,@LastName,@DateOfBirth,@Sex) 
            ";

            //create datatable and configuration stirng
            DataTable table = new DataTable();
            string sqlDataSource = _configuration.GetConnectionString("ParticipantConnectionString");

            //initialize npgsql reader
            NpgsqlDataReader myReader;

            try
            {
                using (NpgsqlConnection myCon = new NpgsqlConnection(sqlDataSource))
                {
                    myCon.Open();
                    using (NpgsqlCommand myCommand = new NpgsqlCommand(query, myCon))
                    {

                        myCommand.Parameters.AddWithValue("@Id", participant.Id);
                        myCommand.Parameters.AddWithValue("@FirstName", participant.FirstName);
                        myCommand.Parameters.AddWithValue("@LastName", participant.LastName);
                        myCommand.Parameters.AddWithValue("@DateOfBirth", Convert.ToDateTime(participant.DateOfBirth));
                        myCommand.Parameters.AddWithValue("@Sex", participant.Sex);

                        //myCommand.Parameters.AddWithValue("@PhotoFileName", participant.PhotoFileName);

                        myReader = myCommand.ExecuteReader();
                        table.Load(myReader);

                        myReader.Close();
                        myCon.Close();

                    }
                }

                return new JsonResult("Added Successfully");
            }
            catch (Exception e)
            {

                return new JsonResult(e);
            }
            
        }
    }
}

Participant class:


public class Participant
{
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string DateOfBirth { get; set; }

    public string Sex { get; set; }

}

CodePudding user response:

You are sending "First Name" (with a space) but your model contains "FirstName" (with no space)

You can either modify what you post (remove the space), or instruct your model to accept bindings to a parameter containing a space (see the doc)

[FromQuery(Name = "First Name")]
public string FirstName { get; set; }
  • Related