SQL query with ID from a combobox - C #

1

I am building a small system where I created a Formulario where there is a Combobox that lists the names of employees stored in the database and a DataGrid that shows the services done by them (value, description and client) .

WhenselectingtheofficialintheCombobox,Iwantedtoshowtheservicesdonebyhim,soIcreated

SELECTfunc_nome,serv_desc,serv_cliente,serv_valorFROMfunc_servINNERJOINfunc_dadosONfunc_serv.func_id=func_dados.func_idWHEREfunc_serv.func_id=//codigodafuncionaria;

My question is, when I get the index of the selected combobox, how can I use it as a parameter for my sql query, replacing that commented part of the code.

I thought of creating multiple ifs, but if a new employee is created, I'll have to put in another if.

Please help me

    
asked by anonymous 25.02.2015 / 18:33

4 answers

1

Just concatenate the value of comboxBox, I used @ to escape the string for better visualization.

SqlCommand sql = new SqlCommand(@"SELECT func_nome, serv_desc, serv_cliente, serv_valor FROM func_serv
                                INNER JOIN func_dados ON func_serv.func_id = func_dados.func_id
                                WHERE func_serv.func_id = " + comboBox1.SelectedValue);
    
25.02.2015 / 18:57
0

@Vinicius Caminha, you can use

  

SelectedValue

. Where meucampo = combobox.SelectedValue;

    
25.02.2015 / 18:45
0

And if you did something like this:

public SqlDataReader lista_dados_servico(object idFuncionario)
{
    conexao = new SqlConnection(conexao_sqlserver);
    var query = @"SELECT func_nome, serv_desc, serv_cliente, serv_valor FROM func_serv
        INNER JOIN func_dados ON func_serv.func_id = func_dados.func_id
        WHERE func_serv.func_id = @idFuncionario";

    using (var comando = new SqlCommand(query, conexao))
    {
        comando.Parameters.AddWithValue("@idFuncionario", idFuncionario);

        return comando.ExecuteReader();
    }
}

and use it like this:

var idFuncionario = comboBox1.SelectedValue;

var reader = lista_dados_servico(idFuncionario);

while (reader.Read())
{
    var index = dataGridView1.Rows.Add();

    int indexColunaNome    = 0,
        indexColunaServico = 1,
        indexColunaCliente = 2,
        indexColunaValor   = 3;

    dataGridView1[index, indexColunaNome].Value    = reader["func_nome"];
    dataGridView1[index, indexColunaServico].Value = reader["serv_desc"];
    dataGridView1[index, indexColunaCliente].Value = reader["serv_cliente"];
    dataGridView1[index, indexColunaValor].Value   = reader["serv_valor"];
}

Would not this solve your problem?

    
25.02.2015 / 18:58
0

Firstly it would change the way you fill in the comboBox for:

conexao = new SqlConnection(conexao_sqlserver); 
SqlCommand sql = new SqlCommand("select func_nome,func_id from func_serv", conexao); 
SqlDataAdapter da = new SqlDataAdapter(sql); 
DataTable dt = new DataTable(); 
da.Fill(dt);

comboBox1.DataSource = (dt.Tables[0]);
comboBox1.DisplayMember = ("func_nome");
comboBox1.ValueMember = ("func_id");

Then in the IndexChanged of the Combobox or in a search button would put the code of @Laerte

SqlCommand sql = new SqlCommand(@"SELECT func_nome, serv_desc, serv_cliente,serv_valor FROM func_serv
                            INNER JOIN func_dados ON func_serv.func_id = func_dados.func_id
                            WHERE func_serv.func_id = " + comboBox1.SelectedValue);

SqlDataAdapter da = new SqlDataAdapter(sql); 
DataTable dt = new DataTable(); 
da.Fill(dt);

Datagrid1.DataSource=dt.Tables[0];

Note: If the system is not organized in layers for a better organization and System Development Practice follows some links for possible study.

link link

    
27.02.2015 / 15:44