Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET
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)
Posted 13-Sep-10 4:32am
Edited 13-Sep-10 5:02am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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(

Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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 at 13-Sep-10 10:02am
Hey Hiren,
the first one is my error, cannot implicitly convert type 'string' to 'int?'.

Hiren Solanki at 13-Sep-10 10:04am
Check the value is null of id variable ?
cnjadhav at 16-Sep-10 9:49am
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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 at 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 at 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 at 13-Sep-10 11:21am

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

  Print Answers RSS
0 Sascha Lefévre 365
1 Sergey Alexandrovich Kryukov 325
2 Maciej Los 240
3 Abhinav S 184
4 Peter Leow 120
0 Sergey Alexandrovich Kryukov 6,773
1 OriginalGriff 6,311
2 Maciej Los 2,692
3 Peter Leow 2,654
4 Abhinav S 2,562

Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 13 Sep 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100