Binding search results to data grid

Posted by Abid on Stack Overflow See other posts from Stack Overflow or by Abid
Published on 2010-05-29T14:06:37Z Indexed on 2010/05/29 15:32 UTC
Read the original article Hit count: 289

Filed under:
|
|

I want to add search functionality to my program. There's a class which has this function:

public DataTable Search()
        {
            string SQL = "Select * from Customer where " + mField + " like '%" + mValue + "%'";
            DataTable dt = new DataTable();
            dt = dm.GetData(SQL);
            return (dt);

        }

There are setter and getter properties for mField and mValue. DM is the object of class DataManagement, which has a method GetData:

public DataTable GetData(string SQL)
    {
        SqlCommand command = new SqlCommand();
        SqlDataAdapter dbAdapter = new SqlDataAdapter();
        DataTable DataTable = new DataTable();

        command.Connection = clsConnection.GetConnection();
        command.CommandText = SQL;
        dbAdapter.SelectCommand = command;
        dbAdapter.Fill(DataTable);
        return (DataTable);
    }

The search functionality is currently implemented like this:

private void btnfind_Click(object sender, EventArgs e)
    {
       //cust is the object of class customer//
        if (tbCustName.Text != "")
        {
            cust.Field="CustName";
            cust.Value = tbCustName.Text;
        }
        else if (tbAddress.Text != "")
        {
            cust.Value = tbAddress.Text;
            cust.Field="Address";
        }
        else if (tbEmail.Text != "")
        {
            cust.Value = tbEmail.Text;
            cust.Field="Email";
        }
        else if (tbCell.Text != "")
        {
            cust.Value = tbCell.Text;
            cust.Field = "Cell";
        }

        DataTable dt = new DataTable();
        dt = cust.Search();
        dgCustomer.DataSource = dt;
        RefreshGrid();
    }

    private void RefreshGrid()
    {
        DataTable dt = new DataTable();
        dt = cust.GetCustomers();
        dgCustomer.DataSource = dt;
    }

This is not working. I don't know why. Please help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET