The input string does not have the correct format. C #

1

First of all sorry for so many questions that for many of you must be insignificant things I imagine, (apprentice mistakes say) The subject is like this: I have properties, each property has date ranges (start date and end date) I did the following in 2 different ways to test if there was something wrong with it and I missed the following error: "The input string it does not have the correct format "

Next I put the code so they can see. In the mapper of properties I did it this way.

 public List<Propiedad> obtenerRangosPorIdPropiedad(int xIdPropiedad)
        {
            List<Propiedad> listaPropiedades= new List<Propiedad>();
            var param = new List<SqlParameter>();
            var idPropiedad = new SqlParameter();
            idPropiedad .ParameterName = "@idPropiedad ";
            idPropiedad .Value = xIdPropiedad;
            param.Add(idPropiedad );
            var conn = abrirConexion();
            var r = @select("SELECT * FROM Propiedad p  JOIN Rango r ON r.idPropiedad  = a.idPropiedad WHERE idPropiedad= @idPropiedad", CommandType.Text, param, conn, null);
            while(r.Read())
            {
                listaPropiedades.Add(CargarPropiedad(r));
            }
            cerrarConexion(conn);
            return listaPropiedades;
        }

In the services part I did it in this other way:

public List<Rango> obtenerRangosDePropiedades(int xIdPropiedad)
        {
            List<Rango> listaRangos = obtenerTodosLosRangos();
            List<Rango> rangosFiltrados = new List<Rango>();
            foreach(var objR in listaRangos)
            {
                if(objR.propiedad.idPropiedad == xIdPropiedad)
                {
                    rangosFiltrados.Add(objR);
                }
            }
            return rangosFiltrados;
        }

And here is where I get the error, in the codebehind I do the following:

In the load I call a method that does this

private void cargarRangos()
        {
            lstRangos.DataSource = new GestoraRango().obtenerRangosDePropiedades(Convert.ToInt32(lstPropiedades.SelectedValue)); //En esta línea me tira ese error.
            lstRangos.DataTextField = "datos";
            lstRangos.DataValueField = "idRango";
            lstRangos.DataBind();
        }

Thanks in advance.

    
asked by Ccccccccc 15.07.2016 в 23:03
source

2 answers

1

The problem is within the method CargarPropiedad() since it is in this count that you convert the fields of the datareader to properties of the entity.

Validate with a breakpoint what value you receive in the date fields because it is clear that one is not valid.

You can also use the DateTime.TryParse () to convert to date but without the exception if it is invalid

DateTime fecha;
if(DateTime.TryParse(r["nombrecampo"].ToString(), out fecha))
{
   entidad.PropFecha = fecha;
}

In this way, if the value you receive is not valid, you will not have an error.

    
answered by 15.07.2016 в 23:25
0

In the cargarRangos() method, the following line appears:

lstRangos.DataSource = new GestoraRango().obtenerRangosDePropiedades(Convert.ToInt32(lstPropiedades.SelectedValue)); //En esta línea me tira ese error.

Before assigning the data source to lstRangos , you must validate that lstPropiedades is NOT NULL and also has data.

It is possible that lstPropiedades.SelectedValue is NULL or a value that can not be converted to Int32 .

    
answered by 15.07.2016 в 23:30