Pregunta

I have a Class (Entity we call them) Named Person which is mapped to a database Table (Person table). The records of this table may contain null values in all fields but one, the PK.

This is in brief how we define our Person entity:

...
private System.DateTime _BirthDate;

[DisplayName("Birth Date")]
[Category("Column")]
public System.DateTime BirthDate
{
    get
    {
        try { return _BirthDate; }
        catch (System.Exception err) 
        { throw new Exception("Error getting BirthDate", err); }
    }
    set
    {
        try { _BirthDate = value; }
        catch (System.Exception err)
        { throw new Exception("Error setting BirthDate", err); }
    }
}
...

the problem is that when I get the list of persons

var dsPersons = DataHelper.personTabelAdapter.GetFilteredPersons(nationalNo, name);

those fields which were null in db, are having exceptions so I can't compare them to null or even DBNull.Value, look at the following image of my code in debug

enter image description here

I want to check their being null, so that I can decide to fill my new objects fields or not to fill them.

here's the details of the exception I get

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.StrongTypingException: The value for column 'BirthDateFa' in table 'Person' is DBNull. ---> System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.

   at EcomService.Data.EcomDS.PersonRow.get_BirthDateFa() in c:\Projects\Project\EcomForms\EcomService\EcomService\Data\EcomDS.Designer.cs:line 30607

   --- End of inner exception stack trace ---

   at EcomService.Data.EcomDS.PersonRow.get_BirthDateFa() in c:\Projects\Project\EcomForms\EcomService\EcomService\Data\EcomDS.Designer.cs:line 30610

   at EcomService.Classes.Entities.Person.GetFilteredPersons(String nationalNo, String name) in c:\Projects\Project\EcomForms\EcomService\EcomService\Classes\Entities\Person.cs:line 898

   at EcomService.EService.GetFilteredPersons(String nationalNo, String name) in c:\Projects\Project\EcomForms\EcomService\EcomService\EService.asmx.cs:line 172

   --- End of inner exception stack trace ---

this is my web method

[WebMethod]
public List<Person> GetFilteredPersons(string nationalNo, string name)
{
    return Person.GetFilteredPersons(nationalNo, name);
}

that calls this method

static public List<Person> GetFilteredPersons(string nationalNo, string name)
{
    List<Person> persons = new List<Person>();
    var dsPersons = DataHelper.personTabelAdapter.GetFilteredPersons(nationalNo, name);
    foreach (var dsPerson in dsPersons)
    {
        Person person = new Person();
        person.BirthDateFa = dsPerson.BirthDateFa;
        //I don't know how to deal with the exceptions here
        .... //other fields
        persons.Add(person);
    }
    return persons;
}

I am really stuck in this piece. Your answers will be kindly welcome.

¿Fue útil?

Solución

The exception comes because you are using a strongly typed DataSet. StrongTypingException documentation says it:

The exception that is thrown by a strongly typed DataSet when the user accesses a DBNull value.

Please read this article, http://msdn.microsoft.com/en-us/magazine/cc163877.aspx

Example :

object.Is<ColumnName>Null() //which returns true or false

Otros consejos

You can check if the object in a typed dataset is null by calling the Is(columnname)Null() function, which returns a boolean. Those strong typing exceptions are due to the column being null. e.g. dsPerson.IsBirthDateNull() .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top