Unable to convert a DBNull object to other types

1

I am creating an application in which you can make reservations, in the mapper of the properties I have passed a method called "LoadProperties" in which I pass the data such as type, etc, the problem is when you I want to pass the user who has registered that property, I did it in the following way:

 private Propiedad CargarPropiedad (SqlDataReader r)
        {
var prop= new Propiedad();
prop.idPropiedad = Convert.ToInt32(r["idPropiedad"]);
//prop.usuario = new Usuario() { idUsuario = Convert.ToInt32(r["idUsuario"])};
...
...
}

In the form when creating a property, what I do is make a session variable

Usuario user = (Usuario)Session["Usuario"];
Propiedad prop = new Propiedad()
{
usuario = user,
nombre = txtNombre.Text,
...
...
...
}

If anyone could help me, I would be very grateful, greetings.

    
asked by Ccccccccc 15.07.2016 в 19:23
source

1 answer

2

I do not know what will be the property that causes the problem with the DBNull but basically you should validate which columns of the table allow null and apply the validation

private Propiedad CargarPropiedad (SqlDataReader r)
{
    var prop= new Propiedad();

    if(r["idPropiedad"] != DBNull.Value)
        prop.idPropiedad = Convert.ToInt32(r["idPropiedad"]);

    //resto codigo
}

The idea is to validate if the columns you take with the datareader is DBNull.Value

    
answered by 15.07.2016 / 19:59
source