0

I have listview that is getting data from sql database .. I want to search by text entered in textbox and show the result after clicking a button and hide the records that doesn't match

here's my approach

plz help

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Inventory_Manager_Pro
{
public partial class Modify : Form
{

    public SqlConnection cn = new SqlConnection("Data Source=10.0.0.13;Initial Catalog=INVENTDB;Persist Security Info=True;User ID=sa;Password=farespila010A@;Encrypt=False");


    public Modify()
    {
        InitializeComponent();
    }

    private void populate()
    {
        listView1.Items.Clear();

        SqlCommand cm = new SqlCommand("SELECT * FROM lapdev", cn);

        try
        {

            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {

                ListViewItem it = new ListViewItem(dr["fillingcode"].ToString());
                it.SubItems.Add(dr["username"].ToString());
                it.SubItems.Add(dr["branch"].ToString());
                it.SubItems.Add(dr["department"].ToString());
                it.SubItems.Add(dr["agency"].ToString());
                it.SubItems.Add(dr["computername"].ToString());
                it.SubItems.Add(dr["lapmodel"].ToString());
                it.SubItems.Add(dr["lapserial"].ToString());
                it.SubItems.Add(dr["assetnumber"].ToString());
                it.SubItems.Add(dr["os"].ToString());
                it.SubItems.Add(dr["winlicense"].ToString());
                it.SubItems.Add(dr["office"].ToString());
                it.SubItems.Add(dr["officelicense"].ToString());
                it.SubItems.Add(dr["hddsize"].ToString());
                it.SubItems.Add(dr["processor"].ToString());
                it.SubItems.Add(dr["ram"].ToString());
                it.SubItems.Add(dr["macadress"].ToString());
                it.SubItems.Add(dr["ipadress"].ToString());

                listView1.Items.Add(it);

            }

            dr.Close();
            dr.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

    private void Modify_Shown(object sender, EventArgs e)
    {
        try
        {
            cn.Open();
            populate();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.ExitThread();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
listView1.Items.Clear(); // clear list items before adding 
    listView1.Items.AddRange(Items.Where(i=>string.IsNullOrEmpty(textBox1.Text)||i.Name.StartsWith(textBox1.Text))
        .Select(c => new ListViewItem(c.Name)).ToArray());

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {



    }

    private void button2_Click(object sender, EventArgs e)
    {

    }

}

}

9
  • I can see exactly where your issue is.. but can you? Have you tried to solve this at all? Have you learnt any SQL further than SELECT * FROM? We can give you answers.. but they are answers that you should be able to investigate yourself. Sorry if that sounds harsh. Commented Jul 20, 2013 at 0:26
  • I have been searching for 2 days non stop .. please help if u can Commented Jul 20, 2013 at 0:40
  • "SELECT * FROM lapdev WHERE YourField='" + textBox1.Text + "'" Commented Jul 20, 2013 at 0:43
  • 1
    nice website with examples sql and other stuff check it out after - w3schools.com/sql/default.asp Commented Jul 20, 2013 at 0:48
  • terrybozzio thaks so much but can you show me where should i put that code in the code above Commented Jul 20, 2013 at 0:54

1 Answer 1

2

In your pupolate method do this:

    private void populate()
    {
        listView1.Items.Clear();
        SqlCommand cm;
        if(textBox1.Text == "")
           cm = new SqlCommand("SELECT * FROM lapdev", cn);
        else
            cm = new SqlCommand("SELECT * FROM lapdev WHERE YourField='" + textBox1.Text + "'", cn);


        try
        {

            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {

                ListViewItem it = new ListViewItem(dr["fillingcode"].ToString());
                it.SubItems.Add(dr["username"].ToString());
                it.SubItems.Add(dr["branch"].ToString());
                it.SubItems.Add(dr["department"].ToString());
                it.SubItems.Add(dr["agency"].ToString());
                it.SubItems.Add(dr["computername"].ToString());
                it.SubItems.Add(dr["lapmodel"].ToString());
                it.SubItems.Add(dr["lapserial"].ToString());
                it.SubItems.Add(dr["assetnumber"].ToString());
                it.SubItems.Add(dr["os"].ToString());
                it.SubItems.Add(dr["winlicense"].ToString());
                it.SubItems.Add(dr["office"].ToString());
                it.SubItems.Add(dr["officelicense"].ToString());
                it.SubItems.Add(dr["hddsize"].ToString());
                it.SubItems.Add(dr["processor"].ToString());
                it.SubItems.Add(dr["ram"].ToString());
                it.SubItems.Add(dr["macadress"].ToString());
                it.SubItems.Add(dr["ipadress"].ToString());

                listView1.Items.Add(it);

            }

            dr.Close();
            dr.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

and in your button click event:

    private void button1_Click(object sender, EventArgs e)
    {
         //listView1.Items.Clear(); // clear list items before adding 
         populate();
         //you type what you want in textbox then click button.
    }

you dont even need to call clear on button click because populate method already takes care of that.

Sign up to request clarification or add additional context in comments.

1 Comment

new edit i forgot no need to call clear on button,populate method takes care of that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.