Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi
I have an error 'System.Int32' type to the 'System.String' type is not valid in mvc api Pls advice me
Thank you
Maideen

Below my Code

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace advEditorials.Models
{
    public class MoDetails
    {
        public int Id { get; set; }
        public string DocNo { get; set; }
        public DateTime PubDate { get; set; }
        public string MainSection { get; set; }
        public string SubSection { get; set; }
        public string Position { get; set; }
        public string Color { get; set; }
        public decimal SizeH { get; set; }
        public decimal SizeW { get; set; }
        public decimal Volume { get; set; }
        public decimal PageNumber { get; set; }
        public string Materials { get; set; }
        public string Remarks { get; set; }
        public string Status { get; set; }

    }
}


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace advEditorials.Models
{
    public interface IMORepository
    {
        Task Add(MoDetails mo);
        Task Update(MoDetails mo);
        Task Delete(string id);
        Task<MoDetails> Getmo(string id);
        Task<IEnumerable<MoDetails>> GetmoALL();
    }
}


C#
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace advEditorials.Models
{
    public class MORepository :IMORepository
    {
        private readonly SqlDbContext db = new SqlDbContext();

// Retrive All
        public async Task<IEnumerable<MoDetails>> GetmoALL()
        {
            try
            {
                var mo = await db.tbl_Editorial.ToListAsync();
                return mo.AsQueryable();
            }
            catch
            {
                throw;
            }
        }

 // by ID
        public async Task<MoDetails> Getmo(string id)
        {
            try
            {
                MoDetails mo = await db.tbl_Editorial.FindAsync(id);
                if (mo == null)
                {
                    return null;
                }
                return mo;
            }
            catch
            {
                throw;
            }
        }
 //Add
        public async Task Add(MoDetails mo)
        {
            mo.Id = Guid.NewGuid().ToString();
            
            db.tbl_Editorial.Add(mo);
            try
            {
                await db.SaveChangesAsync();
            }
            catch
            {
                throw;
            }
        }

        //Update
        public async Task Update(MoDetails mo)
        {
            try
            {
                db.Entry(mo).State = EntityState.Modified;
                await db.SaveChangesAsync();
            }
            catch
            {
                throw;
            }
        }
//Delete
        public async Task Delete(string id)
        {
            try
            {
                MoDetails mo = await db.tbl_Editorial.FindAsync(id);
                db.tbl_Editorial.Remove(mo);
                await db.SaveChangesAsync();
            }
            catch
            {
                throw;
            }
        }

        private bool MOExists(string id)
        {
            return db.tbl_Editorial.Count(e => e.Id == id) > 0;
        }
    }
} 


Controller

C#
using advEditorials.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace advEditorials.Controllers
{
    public class EditorialController : ApiController
    {
        private readonly IMORepository _iMORepository = new MORepository();

        [HttpGet]
        //[Route("api/Editorial/Get")]
        public async Task<IEnumerable<MoDetails>> Get()
        {
            return await _iMORepository.GetmoALL();
        }

        [HttpPost]
        [Route("api/Editorial/Create")]
        public async Task CreateAsync([FromBody]MoDetails mo)
        {
            if (ModelState.IsValid)
            {
                await _iMORepository.Add(mo);
            }
        }

        [HttpGet]
        [Route("api/Editorial/Details/{id}")]
        public async Task<MoDetails> Details(string id)
        {
            var result = await _iMORepository.Getmo(id);
            return result;
        }

        [HttpPut]
        [Route("api/Editorial/Edit")]
        public async Task EditAsync([FromBody]MoDetails mo)
        {
            if (ModelState.IsValid)
            {
                await _iMORepository.Update(mo);
            }
        }

        [HttpDelete]
        [Route("api/Editorial/Delete/{id}")]
        public async Task DeleteConfirmedAsync(string id)
        {
            await _iMORepository.Delete(id);
        }


    }
}

DB Table


SQL
CREATE TABLE [dbo].[tbl_Editorial](
	[id] [nvarchar](20) NOT NULL,
	[DocNo] [varchar](20) NULL,
	[pubdate] [date] NULL,
	[MainSection] [varchar](50) NULL,
	[SubSection] [varchar](50) NULL,
	[Position] [varchar](50) NULL,
	[Color] [varchar](20) NULL,
	[SizeH] [float] NULL,
	[SizeW] [float] NULL,
	[Volume] [float] NULL,
	[PageNumber] [float] NULL,
	[Materials] [varchar](100) NULL,
	[Remarks] [varchar](100) NULL,
	[Status] [varchar](20) NULL,
	[Category] [varchar](50) NULL
) ON [PRIMARY]
GO
Posted
Updated 18-Nov-19 23:58pm
Comments
F-ES Sitecore 19-Nov-19 5:34am    
It would probably help if you said exactly which line in the 100 lines of code you pasted raises the error.
Richard Deeming 19-Nov-19 13:29pm    
If you want someone to help you fix an exception, you need to post the full and exact error message, and indicate which line of code it's thrown from.

Don't try to paraphrase or summarise the error message, because you end up losing important information from it.

'System.Int32' type to the 'System.String' type is not valid is not the full error message.
Maideen Abdul Kader 19-Nov-19 18:28pm    
Hi
Thank you for prompt reply

Error in MORepository module

Operator '==' cannot be applied to operands of type 'int' and 'string'

private bool MOExists(string id)
{
return db.tbl_Editorial.Count(e => e.Id == id) > 0; (this line)

}

This is happened when debugging.

Pls advice me
Thank you
Maideen

1 solution

Property Id in your MoDetails class is type of integer, while your database column [id] is type of varchar, which is string.
You need to keep both types the same.
 
Share this answer
 
v3
Comments
Afzaal Ahmad Zeeshan 19-Nov-19 9:44am    
5ed.
jimmson 19-Nov-19 9:56am    
Thanks, pal!
Maideen Abdul Kader 22-Nov-19 4:57am    
Thank you all

Below I mentioned string, But in table it is int. So i have changed to int, working fine

Previous code

private bool MOExists(string id)
{
return db.tbl_Editorial.Count(e => e.Id == id) > 0; (this line)

}

Current code

private bool MOExists(int id)
{
return db.tbl_Editorial.Count(e => e.Id == id) > 0; (this line)

}

Thank you all gave me advice

Maideen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900