Conceptual problem with webservice, how to make a webMethod

Asked

Viewed 32 times

0

Good afternoon guys, I’m having a little problem here at the time of creating a webMethod. The thing is, I need to work with three levels, in the three cases I need to send to the database, follow the batch addition routine...

[WebMethod(Description = "Adicione um lote")]
    public string Adicionar_Lote(string ID_Cliente, string NR_Nota_Fiscal, string DT_Emissao, string OP_Tipo_Lote, string NR_Cnpj_Faccionista, string ID_Status, string COD_Verificacao)
    {
        object DT_Status = DateTime.Now;
        int i = 0;
        string t = verifica_Lote(NR_Nota_Fiscal, OP_Tipo_Lote, NR_Cnpj_Faccionista); //se repetir esses, nao adiciona o lote novo.
        if (t == null)
        {
            string leitura = "INSERT INTO Romaneio_Lote (ID_Cliente, NR_Nota_Fiscal, DT_Emissao, OP_Tipo_Lote, NR_Cnpj_Faccionista, ID_Status, DT_Status, COD_Verificacao) VALUES(@ID_Cliente, @NR_Nota_Fiscal, @DT_Emissao, @OP_Tipo_Lote, @NR_Cnpj_Faccionista, @ID_Status, @DT_Status, @COD_Verificacao)";
            try
            {
                using (var connection = new MySqlConnection("Server=seiren_dev.mysql.dbaas.com.br;Port=3306;Database=seiren_dev;Uid=seiren_dev;Pwd=S3iR3n@1973dev;"))
                {
                    connection.Open(); using (var cmd = new MySqlCommand())
                    {
                        cmd.Parameters.AddWithValue("@ID_Cliente", ID_Cliente);
                        cmd.Parameters.AddWithValue("@NR_Nota_Fiscal", NR_Nota_Fiscal);
                        cmd.Parameters.AddWithValue("@DT_Emissao", DT_Emissao);
                        cmd.Parameters.AddWithValue("@OP_Tipo_Lote", OP_Tipo_Lote);
                        cmd.Parameters.AddWithValue("@NR_Cnpj_Faccionista", NR_Cnpj_Faccionista);
                        cmd.Parameters.AddWithValue("@ID_Status", ID_Status);
                        cmd.Parameters.AddWithValue("@DT_Status", DT_Status);
                        cmd.Parameters.AddWithValue("@COD_Verificacao", COD_Verificacao);
                        cmd.Connection = connection;
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = leitura; //aqui e executado a interacao com o banco
                        t = "Inserido com sucesso";
                        using (var dr = cmd.ExecuteReader())
                        {
                            dr.Close();
                            dr.Dispose();
                        }
                        string seleciona = "SELECT LAST_INSERT_ID()";
                        cmd.CommandText = seleciona;
                        int ultimoid = Convert.ToInt32(cmd.ExecuteScalar());
                        ids.lote.Add(ultimoid);


                        return t + " ";//+ ultimoid;
                    }
                }
            }
            catch (System.Exception)
            {
                return t;
                throw;
            }
        }
        else
        {
            return t;

        }
    }

lvl1: Creation of a lot.

lvl2: of romantic 'x' (which goes within a lot)

lvl3: of 'y' plays (which goes within a romanticism, within the lot). PS:I have the connection to the database, so I have access to the ID’s to control each of the levels. However it is necessary that it be done within the same webMethod.

Is it possible to call one webMethod inside another? because if so, it would make my problem much easier. I will receive an XML format:

<IDENTIFICAÇÃO DO USUARIO>
   <ADICIONAR LOTE>
      <ADICIONAR ROMANEIO 1>
         <ADICIONAR PEÇA 1>
         <ADICIONAR PEÇA 2>
         <ADICIONAR PEÇA 3>
      </ADICIONAR ROMANEIO 1>
      <ADICIONAR ROMANEIO 2>
         <ADICIONAR PEÇA 1>
         <ADICIONAR PEÇA 2>
         <ADICIONAR PEÇA 3>
      </ADICIONAR ROMANEIO 2>
      <ADICIONAR ROMANEIO 3>
         <ADICIONAR PEÇA 1>
         <ADICIONAR PEÇA 2>
         <ADICIONAR PEÇA 3>
      </ADICIONAR ROMANEIO 3>

A summary: How will I know how many romance and pieces the user wants to register and all will be within a lot?

  • Can’t send Json? It would be simpler to work with complex objects

1 answer

1


You are receiving static variables by parameter. You must make your Webmethod available with an object and decorate it as XML.

[XmlSerializerFormat]
public class Lote(){
    public List<Romaneio> Romaneio{get; set;}
}
[XmlSerializerFormat]
public class Romaneio(){
    public List<Peca> Peca{get; set;}
}

In your Webmethod you can pass the object by parameter. That way you have more control over what you are receiving and can better isolate your code.

[WebMethod(Description = "Adicione um lote")]
public string Adicionar_Lote(Lote lotes)
{

}

Within your code you scan your XML that you can now control the amount of items received as per your XML parameter object.

Browser other questions tagged

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