Click here to Skip to main content
Rate this: bad
good
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 this.id = e.id as cannot implicitly convert type 'string' to 'int?'.
 
many many Thanks,
Chetan.
 
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()
        {
            this.id = -1;
            this.name = string.Empty;
            this.parents = string.Empty;
            this.gender = -1;
            this.weight = string.Empty;
            this.length = string.Empty;
            this.date = DateTime.Now;
            this.time = DateTime.Now;
            this.image = string.Empty;
            this.ipa = string.Empty;
            this.ipb = string.Empty;
        }
 
        public Baby(Data.DAL.Baby e)
        {
            this.id = e.id;     //getting error on this line
            this.name = e.name;
            this.parents = e.parents;
            this.gender = e.gender;
            this.weight = e.weight;
            this.length = e.length;
            this.date = e.date;
            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)
        {
            e.id = t.id;
Posted 13-Sep-10 4:32am
Edited 13-Sep-10 5:02am
Dalek Dave432.9K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Make sure that the "e.id" is an integer variable.
This error occurs because, you are trying to assign a string value "e.id" to integer value "this.id".
 
If your "e.id" contains integer value, then try,
 
this.id = Convert.ToInt32(e.id)
 
Regards,
Suresh
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You have mentioned, below line is throwing error.
 
public Baby(Data.DAL.Baby e)
{    this.id = e.id;     //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 e.id ?
 
Put a brekpoint and check. Let us know,if it does not solve your issue.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Please Clarify Which Error you are Getting.
 
cannot implicitly convert type 'string' to 'int?'.
or
cannot implicitly convert type 'string' to 'int'.
 
Because the First Error relates to Nullable Int.
 
and Second Error Seems Like Data Conversion problem.
 
Thanks,
  Permalink  
Comments
cnjadhav at 13-Sep-10 10:02am
   
Hey Hiren,
the first one is my error, cannot implicitly convert type 'string' to 'int?'.
 
thanks
Chetan.
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
good
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!
 
this.id = int.Parse( e.id.ToString());
 
If it's already a String, the ToString() call should not matter.
  Permalink  
Comments
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
   
Yeah.

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 519
2 Maciej Los 305
3 BillWoodruff 250
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 13 Sep 2010
Copyright © CodeProject, 1999-2014
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