How to do a search in C #? [closed]

1

I'm having some difficulty doing a search as the picture illustrates.

UsingcomboBoxworked,hereisthecode

Class

publicvoidpesquisar(intID){ArtigoList=newArtigo();SqlConnectionconexao=newSqlConnection(caminho);SqlCommandcmd=newSqlCommand();cmd.Connection=conexao;cmd.CommandType=CommandType.Text;stringobj="Select * From artigo Where id_artigo=@id_artigo";
            cmd.Parameters.AddWithValue("@id_artigo", ID);
            cmd.CommandText = obj;
            SqlDataReader dr = null;
            conexao.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                id_artigo = Convert.ToInt32(dr["id_artigo"]);
                item = dr["item"].ToString();
                tipo_item = dr["tipo_item "].ToString();
                preco_compra = Decimal.Parse(dr["preco_compra"].ToString());
                preco_venda = Decimal.Parse(dr["preco_venda"].ToString());

            }
        }

Form

public void pesquisa_Detalhada()
        {
            Artigo obj = new Artigo();
            obj.pesquisar(Convert.ToInt32(cb_Artigo.SelectedValue));
            txt_Item.Text = obj.item.ToString();
            txt_Tipo_Item.Text = obj.tipo_item.ToString();
            txt_preco_vend.Text = Convert.ToDecimal(obj.preco_venda).ToString();
        }
    
asked by anonymous 19.06.2017 / 11:36

1 answer

0

Considering the Search method within the Article class, it should look like this:

 public static Artigo Pesquisar(int _id)
 {
        Artigo obj = new Artigo();
        SqlConnection conexao = new SqlConnection(caminho);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conexao;
        cmd.CommandType = CommandType.Text;
        string sql = "Select * From artigo Where id_artigo=@id_artigo";
        cmd.Parameters.AddWithValue("@id_artigo", _id);
        cmd.CommandText = sql;
        conexao.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            obj.Id = Convert.ToInt32(dr["id_artigo"]);
            obj.Item = dr["item"].ToString();
            obj.TipoItem = dr["tipo_item "].ToString();
            obj.PrecoCompra = Decimal.Parse(dr["preco_compra"].ToString());
            obj.PrecoVenda = Decimal.Parse(dr["preco_venda"].ToString());
        }

       conexao.Close();

       return obj;
 }

Now to run the search, run like this:

public void PesquisaDetalhada()
{
     //Usando um TextBox para informar o ID
     Artigo obj = Artigo.Pesquisar(Convert.ToInt32(txbId.Text))
     txt_Item.Text = obj.Item;
     txt_Tipo_Item.Text = obj.TipoItem;
     txt_preco_vend.Text = obj.PrecoVenda.ToString("C2");
}

A detail is that in comboBox you search by ID, in the TextBox you search by barcode, notice that in bar codes you can usually have 13 digits, which would populate the maximum size of a variable int , it will be necessary to adjust this situation. Note also the naming pattern.

    
19.06.2017 / 14:16