-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.
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;
}
}
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?
– Brewerton Santos
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
– Junior Guerreiro
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 Linhalis
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.
– Junior Guerreiro
Include your trial in the question.
– Leandro Angelo
Already includes the Procedure
– Junior Guerreiro
@Juniorguerreiro See the edition of my reply
– Leandro Angelo
Thank you all, I changed the trial as Leandro indicated me and gave the consultation.
– Junior Guerreiro