字符串值为空时发生Web api错误

时间:2016-06-16 20:02:59

标签: c# oracle rest stored-procedures asp.net-web-api

这是我尝试传递其中一个值“in blank”/ null时得到的错误。奇怪的是,只有一个参数会导致这个问题...只是“nomePai” 在数据库上,storedprocedure就可以了......

如果我通过NomeEnvolvido = Teste,nomeMae = Teste,nomePai = Teste,Dtnasc = 01/01 / 2000 - > OKKKKK

如果我通过NomeEnvolvido = Teste,nomeMae =,nomePai = Teste,Dtnasc = 01/01 / 2000 - > OKKKKK

但是如果我通过NomeEnvolvido = Teste,nomeMae = Teste,nomePai =,Dtnasc = 01/01 / 2000,我收到错误:

  

{“Message”:“发生错误。”,“ExceptionMessage”:“'ObjectContent'1'类型无法序列化内容类型'application / json; charset = utf-8'的响应正文。” ,“ExceptionType”:“System.InvalidOperationException”,“StackTrace”:null,“InnerException”:{“消息”:“发生了错误。”,“ExceptionMessage”:“Objetonãodepode ser convertido de DBNull em outros tipos。 “,”ExceptionType“:”System.InvalidCastException“,”StackTrace“:”em System.DBNull.System.IConvertible.ToDateTime(IFormatProvider provider)\ r \ n em em System.Convert.ToDateTime(Object value)\ r \ n em WebApiApp.Controllers.NomesController。<> c.b__1_0(DataRow行)na C:\ Users \ 50216740 \ Documents \ Visual Studio 2015 \ Projects \ AppMobilePCRJ \ AppMobilePCRJ \ WebApiApp \ Controllers \ NomesController.cs:linha 69 \ r \ n em System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\ r \ n em Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IEnumerable values,JsonArrayContract contrac t,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n em Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n em Newtonsoft .Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value,Type objectType)\ r \ n em Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value,Type objectType)\ r \ n em System.Net.Http .Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型类型,对象值,流writeStream,编码effectiveEncoding)\ r \ n \ n系统System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(类型类型,对象值,流writeStream,编码effectiveEncoding)\ r \ n \ n em System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,Object value,Stream writeStream,Ht tpContent content)\ r \ n em System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type,Object value,Stream writeStream,HttpContent content,TransportContext transportContext,CancellationToken cancellationToken)\ r \ n --- Fim do rastreamento de pilha System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n em,System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n em System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()“}}

[控制器代码]

 [HttpGet]
        [Route("Envolvidos")]
        public IEnumerable<Envolvido> GetEnvolv(string nomePesquisa, string nomeMae, string nomePai,  Nullable<DateTime> dtnasc
            , string nRG, string nCPF)
        {

            DataSet lretorno = new DataSet();

            string connectionString = GetConnectionString();
            using (OracleConnection connection = new OracleConnection())
            {
                connection.ConnectionString = connectionString;

                OracleDataReader reader = null;
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = connection;
                cmd = new OracleCommand("MOBILE.XAPIMANDADOMOBILE.BUSCAPOSSIVEISCANDIDATOSPF", connection);
                cmd.CommandType = CommandType.StoredProcedure;

                //variáveis entrada            
                cmd.Parameters.Add(new OracleParameter("ivnome",nomePesquisa));
                cmd.Parameters.Add(new OracleParameter("ivmae", nomeMae));
                cmd.Parameters.Add(new OracleParameter("ivpai", nomePai));
                cmd.Parameters.Add(new OracleParameter("ivdatanasc", dtnasc));
                cmd.Parameters.Add(new OracleParameter("ivrg", nRG));
                cmd.Parameters.Add(new OracleParameter("icpf", nCPF));
                //variáveis de saida          
                cmd.Parameters.Add(new OracleParameter("oretorno", OracleDbType.RefCursor)).Direction = ParameterDirection.Output;
                cmd.Parameters.Add(new OracleParameter("qretorno", OracleDbType.RefCursor)).Direction = ParameterDirection.Output;

                connection.Open();
                cmd.ExecuteNonQuery();

                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                //CRIO A LISTA
                lretorno.Load(reader, LoadOption.OverwriteChanges, "BUSCAPOSSIVEISCANDIDATOSPF");
                connection.Close();
                connection.Dispose();

                //CARREGO O DATASET E TRANSFORMO PARA IENUMERABLE E RETORNO SEUS VALORES PRO JSON
                return lretorno.Tables[0].AsEnumerable().Select(row => new Envolvido
                {
                    ID = Convert.ToInt32(row["SUSPID"]),
                    IVNOME = Convert.ToString(row["SUSPNOME"]),
                    IVMAE = Convert.ToString(row["SUSPMAE"]),
                    IVPAI = Convert.ToString(row["SUSPPAI"]),
                    IVDATANASC = Convert.ToDateTime(row["SUSPDATANASC"]).ToString("dd/MM/yyyy"),
                    IVRG = Convert.ToString(row["RG"]),
                    ICPF = Convert.ToString(row["CPF"]),
                    MANDADO = Convert.ToInt16(row["TEMMANDADO"]),
                    OCORRENCIA = Convert.ToInt16(row["TEMOCORRENCIA"]),

                });

            }
        }

[Envolvido课程]

namespace WebApiApp.Models
{
    public class Envolvido
    {
        [Key]
        public Int32 ID { get; set; }
        public string IVNOME { get; set; }
        public string IVMAE { get; set; }
        public string IVPAI { get; set; }
        public string IVDATANASC { get; set; }
        public string IVRG { get; set; }
        public string ICPF { get; set; }
        public Int16 MANDADO { get; set; }
        public Int16 OCORRENCIA { get; set; }
    }
}

...问候

0 个答案:

没有答案