How to work with various parameters in c#

Asked

Viewed 177 times

-1

Good afternoon. I need a help, I have a combobox with 3 options, and I need to pass any of the three options as a parameter of a Procedure in sql for a query, but since I’m new in c# I never passed more than one parameter for a query in sql server, now I’m having this difficulty, that I need to pass the chosen parameter in the combobox, and either CHEKLIST or SUPPLIER or CNPJ. Procedure is already working on sql.

Follow my screen inserir a descrição da imagem aqui

follows my code

private void btn_consultar_Click(object sender, EventArgs e)
    {
        SqlCommand xml = new SqlCommand("usp_consultachkrecebimento", conexaoUSUARIOS(true));
        xml.Parameters.AddWithValue("@CHEKLIST", this.txt_consultar.Text);
        xml.Parameters.AddWithValue("@FORNECEDOR", this.txt_consultar.Text);
        xml.Parameters.AddWithValue("@CNPJ", this.txt_consultar.Text);
        xml.CommandType = CommandType.StoredProcedure;
        xml.ExecuteNonQuery();

        try
        {
            SqlDataAdapter dados = new SqlDataAdapter(xml);
            DataTable dtLista = new DataTable();
            dados.Fill(dtLista);

            dgw_consultaxml.DataSource = dtLista;
        }
        catch (Exception)
        {
            MessageBox.Show("Não existem dados a retornar por favor verifique como o Administrador do sistema");
            return;
        }
    }

inserir a descrição da imagem aqui

follows the trial

ALTER PROCEDURE [dbo].[usp_consultachkrecebimento]

     @CHEKLIST    AS VARCHAR(10)
    ,@FORNECEDOR  AS VARCHAR(MAX)
    ,@CNPJ        AS VARCHAR(20)

    AS
    BEGIN

    SET NOCOUNT ON;

    SELECT DISTINCT 
    R.ID_CKCLIST,
    R.CHV_NFE,
    R.N_FONEC,
    R.N_NOTA,
    R.N_CNPJ,
    R.N_IE,
    CONVERT(VARCHAR(10), CAST(R.DT_RECEBIM AS DATE), 103) AS RECEBIMENTO
    FROM tbl_chkrecebimento AS R
    WHERE (@CHEKLIST IS NULL      OR ID_CKCLIST = @CHEKLIST)
    AND   (@FORNECEDOR IS NULL    OR N_FONEC LIKE '%' + @FORNECEDOR + '%')
    AND   (@CNPJ IS NULL          OR N_CNPJ = @CNPJ)
END
  • you intend to perform a query? for example a select where cnpj is equal to the field entered, correct?

  • That’s correct. But I need to know which field I’m going through because if I pass the CNPJ the VENDOR and CHEKLIST field gets null

  • have q have the 3 parameters always ? but if it is not the option chosen, the others have to pass null ? -> xml.Parameters.AddWithValue("@CHEKLIST", combo.SelectedIndex == 0 ? this.txt_consultar.Text : null);

  • Rovann your option is passing the same value in the three parameters, for example if I pass the parameter CHEKLIST = 000003, The parameter SUPPLIER is catching 000003 too and not null.

  • Include your trial in the question.

  • Already includes the Procedure

  • @Juniorguerreiro See the edition of my reply

  • Thank you all, I changed the trial as Leandro indicated me and gave the consultation.

Show 3 more comments

1 answer

3


It actually depends on how your trial is going, but in the current scenario with the data provided, you could make a condition to determine which parameter will be sent according to your selection DropDownList, that as you do not present your id I will call you ddl_Filtro

private void btn_consultar_Click(object sender, EventArgs e)
{
    SqlCommand xml = new SqlCommand("usp_consultachkrecebimento", conexaoUSUARIOS(true));

    switch(this.ddl_Filtro.SelectedValue){
        case "CHEKLIST" :
            xml.Parameters.AddWithValue("@CHEKLIST", this.txt_consultar.Text);
            break;
        case "FORNECEDOR" :
            xml.Parameters.AddWithValue("@FORNECEDOR", this.txt_consultar.Text);
            break;
        case "CNPJ":
            xml.Parameters.AddWithValue("@CNPJ", this.txt_consultar.Text);
            break;
    }
    xml.CommandType = CommandType.StoredProcedure;
    xml.ExecuteNonQuery();

    try
    {
        SqlDataAdapter dados = new SqlDataAdapter(xml);
        DataTable dtLista = new DataTable();
        dados.Fill(dtLista);

        dgw_consultaxml.DataSource = dtLista;
    }
    catch (Exception)
    {
        MessageBox.Show("Não existem dados a retornar por favor verifique como o Administrador do sistema");
        return;
    }
}

Already in your process add default values in the parameters declaration

ALTER PROCEDURE [dbo].[usp_consultachkrecebimento]

     @CHEKLIST    AS VARCHAR(10) = NULL
    ,@FORNECEDOR  AS VARCHAR(MAX) = NULL
    ,@CNPJ        AS VARCHAR(20) = NULL

AS
BEGIN

    SET NOCOUNT ON;

    SELECT DISTINCT 
    R.ID_CKCLIST,
    R.CHV_NFE,
    R.N_FONEC,
    R.N_NOTA,
    R.N_CNPJ,
    R.N_IE,
    CONVERT(VARCHAR(10), CAST(R.DT_RECEBIM AS DATE), 103) AS RECEBIMENTO
    FROM tbl_chkrecebimento AS R
    WHERE (@CHEKLIST IS NULL      OR ID_CKCLIST = @CHEKLIST)
    AND   (@FORNECEDOR IS NULL    OR N_FONEC LIKE '%' + @FORNECEDOR + '%')
    AND   (@CNPJ IS NULL          OR N_CNPJ = @CNPJ)
END
  • Leandro takes me a doubt, the ddl_Filtro, this and hi my combobox or not. because I’m wrong in this value, my program does not find the ddl_Filtro.

  • As I explained, it was the name I gave as an example, you replace it with the combobox, because you can’t guess what name you gave him :P

  • But the error you added to the question indicates that you will still need to make more changes to get the least functionality.

  • Thank you Leandro I changed the process as you indicated and it worked. Vlw

Browser other questions tagged

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