locked
How to resolve the error 'System.Int32' type to the 'System.String' type is not valid in mvc api ? RRS feed

  • Question

  • User-1640542475 posted

    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

    It is my code

    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; }
    
        }
    }
    
    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();
        }
    }
    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
    
    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
    
    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
    
    
    

    Tuesday, November 19, 2019 10:22 AM

Answers

  • User61956409 posted

    Hi maideen5,

    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)

    }

    Please go definition of your tbl_Editorial class and check if the Id property look like this.

    [StringLength(20)]
    public string Id { get; set; }

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 20, 2019 2:47 AM

All replies

  • User-1780421697 posted

    Can you please share actual event that raised the error, like when you passing model to controller or performing some CRUD operation, where does it happened? It seem that some where in your code there is some type casting issue, where you have input as string and try to store it without casting to int. can you please debug it and mention where error raised.

    Tuesday, November 19, 2019 11:33 AM
  • User753101303 posted

    Hi,

    Always show the fulll message rather than an excerpt. Could it be that EF complains about having an int for the id on the model side but a nvarchar on the db side? Be consistent on both sides and see what happens.

    Tuesday, November 19, 2019 11:41 AM
  • User-1640542475 posted

    Hi Khuram

    Thank you

    But I have changed int to integer, the error in modul "Operator '==' cannot be applied to operands of type 'integer' and 'string'"

    in MORepository module

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

    }

    Please advice me

    Thank you

    Maideen

    Tuesday, November 19, 2019 11:49 AM
  • User-1640542475 posted

    Hi PatriceSc

    thank you. In table id is int and primary key. In Model I declare int. The error is

    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)

    }

    I have changed int to Intiger, also same error message

    Pls advice me

    Thank you

    Tuesday, November 19, 2019 11:55 AM
  • User-1780421697 posted

    You can convert string id to Int, like Int32.Parse(id) and then you can compare int with int. OR you can convert your existing int to .Tostring() and compare by using .Equals(id)

    Just for example: 

    int Id=1;
    int count = 2;
    
    if(Id == count)
    {
    
    }
    else{ //work fine   }



    Tuesday, November 19, 2019 11:56 AM
  • User753101303 posted

    Be consistent everywhere. If id is an integer you should also have private bool MOExists(int id) and so on...

    Edit: or change the id property to a string as basically this is the only place where it is NOT a string ? See if it works and then take the time to see if you really want a string or if it should be changed everywhere to an int ???

    Tuesday, November 19, 2019 12:01 PM
  • User61956409 posted

    Hi maideen5,

    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)

    }

    Please go definition of your tbl_Editorial class and check if the Id property look like this.

    [StringLength(20)]
    public string Id { get; set; }

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 20, 2019 2:47 AM
  • User-1640542475 posted

    Mr.Fei

    Thank you, based one your advice , I found the issue in below line.

    Below line is string id, so I have changed, It isworking fine

    Prevoixe code

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

    }

    Now changed to

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

    }

    Thank you

    Friday, November 22, 2019 9:32 AM