Hi I am creating a webapi in asp.net core when I call the controller it is throwing the error and this is have shows how i create the models and the controller. Model :
public class tblProject { [Key] public Guid prj_id { get; set; } public string prj_name { get; set; } public string prj_endcustomer { get; set; } public string prj_customer_name { get; set; } public string prj_client { get; set; } public string prj_scope_of_supply { get; set; } public int prj_revision { get; set; } public bool isactive { get; set; } public DateTime created_date { get; set; } public string created_by { get; set; } } Controller :
[Route("api/[controller]")] [ApiController] public class CDProjectController : ControllerBase { private readonly CDDbcontext _context; public CDProjectController(CDDbcontext context) { _context = context; } // GET: api/DCandidate [HttpGet] public async Task<ActionResult<IEnumerable<tblProject>>> GetDCandidates() { return await _context.tblProject.ToListAsync(); } // GET: api/DCandidate/5 [HttpGet("{id}")] public async Task<ActionResult<tblProject>> GetDCandidate(Guid prj_id) { var dCandidate = await _context.tblProject.FindAsync(prj_id); if (dCandidate == null) { return NotFound(); } return dCandidate; } 65 Answers
var dCandidate = await _context.tblProject.where(p => p.prj_id == prj_id).singleOrdefault();
To check another example
CREATE TABLE MathStudents1 ( Id UNIQUEIDENTIFIER PRIMARY KEY default NEWID(), StudentName VARCHAR (50)
) GO
INSERT INTO MathStudents1 VALUES (default,'Sally') INSERT INTO MathStudents1 VALUES (default,'Edward')
First you need to check sql table in column datatype varchar or nvarchar. After in c# method call passing guid parameter to set string type.
Example
string prj_id = System.Guid.NewGuid().ToString(); var dCandidate = await _context.tblProject.Where(p => p.prj_id == prj_id).SingleOrDefault(); 1in my case some rows in database was Null check or delete Null Rows
Found the error when I use [FromRoute] it is working thank to all for your answers
1