How to do a search in c#?

Asked

Viewed 290 times

1

I’m having some difficulty doing a search as the image illustrates

inserir a descrição da imagem aqui

Using combobox worked, here’s the code

Class

 public void pesquisar(int ID)
        {
            Artigo List = new Artigo();
            SqlConnection conexao = new SqlConnection(caminho);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conexao;
            cmd.CommandType = CommandType.Text;
            string obj = "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();
        }
  • 1

    you can enter the code where you get the text present in the textbox?

  • the method pesquisar(int ID) is within the class Artigo ? post your code please

  • I tried to subtweet here: obj.search(txt_codigo_barra.Text). Tostring(); and nothing

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");
}

One detail is that in comboBox you search by ID, in Textbox you search by barcode, notice that in barcodes you can normally have 13 digits, which would burst the maximum size of a variable int, this situation will need to be adjusted. Also note the nomenclature pattern.

Browser other questions tagged

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