Click here to Skip to main content
14,877,713 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,
I am trying yo build the following class for which i get an error on the line = as cannot implicitly convert type 'string' to 'int?'.

many many Thanks,

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Runtime.Serialization;
using HsInternet.Data.DAL;

namespace HsInternet.Data.BLL
    public class Baby
        public int id { get; set; }
        public string name { get; set; }
        public string parents { get; set; }
        public int gender { get; set; }
        public string weight { get; set; }
        public string length { get; set; }
        public DateTime date { get; set; }
        public DateTime time { get; set; }
        public string image { get; set; }
        public string ipa { get; set; }
        public string ipb { get; set; }

        public Baby()
   = -1;
   = string.Empty;
            this.parents = string.Empty;
            this.gender = -1;
            this.weight = string.Empty;
            this.length = string.Empty;
   = DateTime.Now;
            this.time = DateTime.Now;
            this.image = string.Empty;
            this.ipa = string.Empty;
            this.ipb = string.Empty;

        public Baby(Data.DAL.Baby e)
   =;     //getting error on this line
            this.parents = e.parents;
            this.gender = e.gender;
            this.weight = e.weight;
            this.length = e.length;
            this.time = e.time;
            this.image = e.image;
            this.ipa = e.ipa;
            this.ipb = e.ipb;

        public static void Parse(DAL.Baby e, Baby t)
Updated 13-Sep-10 4:02am


Make sure that the "" is an integer variable.
This error occurs because, you are trying to assign a string value "" to integer value "".

If your "" contains integer value, then try, = Convert.ToInt32(

You have mentioned, below line is throwing error.

public Baby(Data.DAL.Baby e)
{ =;     //getting error on this line

this.Id, represent the current object, which is int. Now what is the type of Data.DAL.Baby.Id ? Is it string ? and what value currenly hold by ?

Put a brekpoint and check. Let us know,if it does not solve your issue.
Please Clarify Which Error you are Getting.

cannot implicitly convert type 'string' to 'int?'.
cannot implicitly convert type 'string' to 'int'.

Because the First Error relates to Nullable Int.

and Second Error Seems Like Data Conversion problem.

cnjadhav 13-Sep-10 10:02am
Hey Hiren,
the first one is my error, cannot implicitly convert type 'string' to 'int?'.

Hiren solanki 13-Sep-10 10:04am
Check the value is null of id variable ?
cnjadhav 16-Sep-10 9:49am
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Well, you can ensure that the id is really an int. However, if you just want a quick and dirty way to remove the error. Try this out! = int.Parse(;

If it's already a String, the ToString() call should not matter.
Abhijit Jana 13-Sep-10 10:12am
int.Parse() will throw an exception, if the input is not valid, like int.Parse("a12") will throw an exception. So, it always better to use TryParse(), which will throw bool based on seccess and failure.
Ruselo Riva Asentista 13-Sep-10 11:09am
I think I should throw an exception because otherwise, you could get an object without an id but with a name, etc. This could be a security threat. It's always better to terminate gracefully than to continue executing out of control. That said, I also use TryParse but only when I have default values like quantities. I never use it on ids or any other navigation / identification properties.
Abhijit Jana 13-Sep-10 11:21am
Member 14482462 21-Jun-19 10:04am
Cannot implicitly convert type `string' to `int'
CapitalTooll Tools = new CapitalTooll();
var lastentry = db.CapitalToolls.OrderByDescending(c => c.ItemMasterNo).FirstOrDefault();
if (id != 0)
Tools = db.CapitalToolls.Where(x => x.ItemMasterNo == id).FirstOrDefault<capitaltooll>();
else if (lastentry == null)
Tools.ItemMasterNo = "CAP 0001";
Tools.ItemMasterNo = "CAP" + (Convert.ToInt32(lastentry.ItemMasterNo.ToString(9, lastentry.ItemMasterNo.Length - 9)) + 1).ToString(D3);
return View(Tools);

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

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