Implement a search box that works c# sql database

26 views Asked by At

I tried several times to implement a search box for my project but I failed.The code before it works fine but the code for the search box doesn't work?this is c#

private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            using(IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
            {
                if (db.State == ConnectionState.Closed)
                    db.Open();
                studentBindingSource.DataSource = db.Query<Student>("select *from Students", commandType: CommandType.Text);
                pContainer.Enabled = false;
                Student obj = studentBindingSource.Current as Student;
                if (obj != null)
                {
                    if (!string.IsNullOrEmpty(obj.ImageUrl)) ;
                    pictureBox.Image = Image.FromFile(obj.ImageUrl);
                }
            }

        }
        catch(Exception ex)
        {
            MetroFramework.MetroMessageBox.Show(this, ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
    {
        if(e.KeyChar == (char)13) 
        { 
        DataView dv = studentBindingSource.DataSource as DataView;
        dv.RowFilter = string.Format("FullName Like '%{0}%'", txtSearch.Text);
        }
    }
0

There are 0 answers