Procedure does not perform

Asked

Viewed 40 times

0

I have two calls from a trial that just won’t run. The code passes, Procedure code in sql management studio works perfectly and within c# returns me no error.

It registers the protocol normally

Controller:

public JsonResult Inserir()
        {
            Pedidos pedidos = new Pedidos();
            Protocolo protocolo = new Protocolo();
            if (ModelState.IsValid)
            {
                List<string> id = new List<string>();
                List<string> itens = new List<string>();
                string nProtocolo = "";
                long contrato = long.Parse(Request.QueryString["contrato"]);
                nProtocolo = SupportFunctions.SupportFunctions.gerarNumeroProtocoolo(2, "e-mail") + geraNumeroSequencial().ToString();

                string url = Request.Url.ToString();
                string[] urlDivida = url.Split('?');
                string[] itensArray = urlDivida[1].Split('&');


                for (int i = 0; i < itensArray.Length; i++)
                {
                    if (itensArray[i].Contains("itensContrato") && itensArray[i].Contains("valorUsado"))
                    {
                        string[] valoresUsados = itensArray[i].Split('=');
                        itens.Add(valoresUsados[1]);
                    }
                    else if (itensArray[i].Contains("itensContrato") && itensArray[i].Contains("id"))
                    {
                        string[] idsUtilizados = itensArray[i].Split('=');
                        id.Add(idsUtilizados[1]);
                    }
                }

                string[,] valores = new string[itens.Count, 2];
                for (int i = 0; i < itens.Count; i++)
                {
                    valores[i, 0] = id[i].ToString();
                    valores[i, 1] = itens[i].ToString();
                }

                for (int x = 0; x < valores.GetLength(0); x += 1)
                {
                    string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {

                        using (SqlCommand cmd = new SqlCommand("SP_AtualizaItemContrato"))
                        {
                            cmd.CommandTimeout = 300;
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Connection = con;

                            con.Open();
                            cmd.Parameters.Add("@IdItemContrato", SqlDbType.Int).Value = valores[x, 0];
                            cmd.Parameters.Add("@SaldoVigente", SqlDbType.Decimal).Value = valores[x, 1];
                            cmd.Parameters.Add("@ContratoID", SqlDbType.VarChar).Value = contrato.ToString();
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                    }
                    //itemContrato.Id = int.Parse(valores[x, 0]);
                    //itemContrato.SaldoVigente = decimal.Parse(valores[x, 1]);
                    //itemContrato.ContratoId = contrato.ToString();
                    //db.Entry(itemContrato).State = EntityState.Modified;
                }

                string data = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString();
                string codidgoParceiro = Request.QueryString["codigoParceiro"];
                string nomeParceiro = Request.QueryString["nomeParceiro"];
                string tipoContrato = Request.QueryString["tipoContrato"];
                string areaReclamacao = Request.QueryString["areaReclamacao"];
                string codigoPatrimonial = Request.QueryString["codigoPatrimonial"];
                string operadora = Request.QueryString["operadora"];
                string statusObra = Request.QueryString["statusObra"];
                string nFRN = Request.QueryString["nFRN"];
                string segmento = Request.QueryString["segmento"];
                string endereco = Request.QueryString["endereco"];
                //string estado = Request.QueryString["estado"];
                int cidade = int.Parse(Request.QueryString["cidade"]);
                string descItem = Request.QueryString["descItem"];
                string icms = Request.QueryString["icms"];
                string ipi = Request.QueryString["ipi"];
                string contaContabil = Request.QueryString["contaContabil"];
                string ordemInterna = Request.QueryString["ordemInterna"];
                int quantidade = int.Parse(Request.QueryString["quantidade"]);
                Decimal valorUnitario = Decimal.Parse(Request.QueryString["valorUnitario"]);
                Decimal valorTotal = Decimal.Parse(Request.QueryString["valorTotal"]);
                string numeroPedido = Request.QueryString["numeroPedido"];
                string itemPedido = Request.QueryString["itemPedido"];
                string observacao = Request.QueryString["observacao"];

                protocolo.Dtprotocolo = DateTime.Now;
                protocolo.Sla = 5;
                protocolo.Prot_DtSla = DateTime.Now.AddDays(5.0);
                protocolo.AcaoSolicitaId = 1;
                protocolo.StatusSolicitacaoID = 1;
                protocolo.OrigemId = 1;
                protocolo.ClassificacaoId = 2;
                protocolo.SolicitanteId = 1;

                protocolo.Descricao = descItem;
                protocolo.Prot_numero = nProtocolo;
                protocolo.GeraProtocolo = DateTime.Now;
                protocolo.DtCompet = DateTime.Now;

                //protocolo.Endereco = endereco;
                protocolo.CidadeID = cidade;
                protocolo.OrdemServico = ordemInterna;
                protocolo.Quantidade = quantidade;
                protocolo.Valor = valorUnitario;
                protocolo.ValorTotal = valorTotal;
                protocolo.NPedido = numeroPedido;
                protocolo.Observacao = observacao;

                db.Protocoloes.Add(protocolo);

                for (int x = 0; x < valores.GetLength(0); x += 1)
                {
                    string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {

                        using (SqlCommand cmd = new SqlCommand("SP_InsertPedido"))
                        {
                            cmd.CommandTimeout = 300;
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Connection = con;

                            con.Open();
                            cmd.Parameters.Add("@ReservaSaldo", SqlDbType.Decimal).Value = valorTotal; //pergunta para a flávia
                            cmd.Parameters.Add("@DtProtocolo", SqlDbType.DateTime).Value = DateTime.Now;
                            cmd.Parameters.Add("@Valor", SqlDbType.Decimal).Value = valorUnitario;
                            cmd.Parameters.Add("@CustoFixoVariado", SqlDbType.Decimal).Value = valorUnitario;
                            cmd.Parameters.Add("@ContratoID", SqlDbType.NVarChar).Value = contrato.ToString();
                            cmd.Parameters.Add("@ItemContrato", SqlDbType.Int).Value = valores[x, 0];
                            cmd.Parameters.Add("@DtCompet", SqlDbType.DateTime).Value = DateTime.Now.AddDays(5.0); //perguntar para a flávia
                            cmd.Parameters.Add("@ProtocoloID", SqlDbType.NVarChar).Value = protocolo.Prot_numero;
                            cmd.Parameters.Add("@Quantidade", SqlDbType.Int).Value = quantidade;
                            cmd.Parameters.Add("@ValorTotal", SqlDbType.Decimal).Value = valorTotal;
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                    }
                }
                db.SaveChanges();

                return Json(new { resultado = true, mensagem = "Protocolo gerado com sucesso" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                IEnumerable<ModelError> erros = ModelState.Values.SelectMany(item => item.Errors);

                return Json(new { resultado = false, mensagem = erros });
            }
        }

I have no idea what is causing this error.

  • The parameters are correct ? can put your proc

  • 1

    Yeah, I saw there was a parameter missing

  • 1

    I found the answer

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.