SQL query with ID from a combobox - C#

Asked

Viewed 2,129 times

1

I’m putting together a little system where I created a Formulario where there is a Combobox listing the names of the employees stored at the bank and a DataGrid that shows the services done by them (value, description and client).

Imagem do Sistema

When selecting the employee in Combobox,I wanted to show the services done by him,

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 = //codigo da funcionaria;

SQL

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

I thought about creating several ifs, but if a new employee is created, I will have to put another if.

Help me out, please

  • How do you fill the Combobox? If you take the data directly from the database, you can use Combobox’s Displaymember and Valuemember properties and take the combo Selectedvalue with the selected ID.

  • conexao = new SqlConnection(conexao_sqlserver);&#xA; SqlCommand sql = new SqlCommand("select func_nome from func_dados", conexao);&#xA; SqlDataAdapter da = new SqlDataAdapter(sql);&#xA;&#xA; DataTable dt = new DataTable();&#xA;&#xA; da. Fill(dt); Try { for (int i = 0; i < dt.Rows.Count; i++) { cb_func_passbook.Items.Add(dt.Rows[i]["func_name"]); } } catch (Exception error) { throw error; }

4 answers

1


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

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);
  • 1

    Thank you, I think this will solve my problem, I tested it in a more general way and I saw that it goes well. is now looking for a way to take the value of the combobox and pass it to DAL ,which is another class

  • You can pass the DAL pro value by parameter, no? If the answer has solved your problem, I ask you to check as the answer.

  • I would have to create a getCombo method to get the value of the combobox?

  • Something like this: http://pastebin.com/8isc9B1B

0

@Vinicius Caminha, you can use the

Selectedvalue

. Where meucampo = combobox.SelectedValue;

  • I can call the Selectedvalue function inside the sql query ?

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 wear 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"];
}

That wouldn’t solve your problem?

0

First I would change the way you fill the combobox to:

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 on Combobox Indexchanged or on a search button would put the @Laerte code

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 follow some links for possible study.

http://www.devmedia.com.br/desenvolvimento-em-3-camadas-conceitos/22277 https://rafaelnicolettdeveloper.wordpress.com/2012/07/04/projeto-em-camadas-c-crud/

Browser other questions tagged

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