-1

I am working in c #, wpf with mvvm and a stored procedure in sql server 2008.

I have a problem when sending the values ​​to the function where the stored procedure is executed, I get then from a form, where it can leave some fields empty. But these are objects of a class, then to access them should not be null. I leave the code.

        public IEnumerable<Model.AsuntoModel> GetBusqueda(Model.PrioridadModel prioridad, Model.StatusAsuntoModel statusasunto, Model.DestinatarioModel destinatario, Model.SignatarioModel signatario, DateTime rangofecha, DateTime referenciadocumento)
    {
        ObservableCollection<Model.AsuntoModel> Busqueda = new ObservableCollection<Model.AsuntoModel>();
        using (var entity = new GestorDocumentEntities())
        {

            try 
             {
                 entity.GetAsuntos(prioridad.IdPrioridad, statusasunto.IdStatusAsunto, destinatario.IdDestinatario, signatario.IdSignatario, referenciadocumento).ToList().ForEach(p =>
                 {
                     Busqueda.Add(new Model.AsuntoModel()
                     {
                         IdAsunto = p.IdAsunto,
                         FechaCreacion = (DateTime)p.FechaCreacion,
                         FechaRecibido = (DateTime)p.FechaRecibido,
                         FechaDocumento = (DateTime)p.FechaDocumento,
                         Titulo = p.Titulo,
                         Descripcion = p.Descripcion,
                         Alcance = p.Alcance,
                         IdUbicacion = (long)p.IdUbicacion,
                         Ubicacion = new Model.UbicacionModel()
                         {
                             UbicacionName = p.CAT_UBICACION.UbicacionName
                         },
                         IdInstruccion = (long)p.IdInstruccion,
                         Instruccion = new Model.InstruccionModel()
                         {
                             InstruccionName = p.CAT_INSTRUCCION.InstruccionName
                         },
                         IdPrioridad = (long)p.IdPrioridad,
                         Prioridad = new Model.PrioridadModel()
                         {
                             PrioridadName = p.CAT_PRIORIDAD.PrioridadName
                         },
                         IdStatusAsunto = p.IdStatusAsunto,
                         StatusAsunto = new Model.StatusAsuntoModel()
                         {
                             StatusName = p.CAT_STATUS_ASUNTO.StatusName
                         },
                         FechaVencimiento = p.FechaVencimiento,
                         Folio = p.Folio
                     });
                 }); 

             }
            catch (Exception)
            {
                ;
            }
        }
        return Busqueda;
    }

2 Answers 2

2

Here's one of many solutions.

You simply make you SQL parameters nullable by giving them a default value.

E.g :

@ParamA = null
@ParamB = null
etc..

Then you manage this on the SQL side to see how many you've received.
This way, your query will have a valu (in this case null) for you parameters if you don't supply any. You can then test it like :

IF @ParamA IS NULL
BEGIN
-- You didn't give that param.
END
ELSE
-- You gave a param
0

Your SP can be coded to treat NULL parms as ANY.

select *
from table
where (table.col = @COL or @COL IS NULL)
and (table.col2 = @COL2 or @COL2 IS NULL)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.