Home > Mobile >  How to search with radio Button in c#
How to search with radio Button in c#

Time:03-03

Table Form

I'm trying to filter this table with RadioButton when DateDePublication is Checked and the value of the search text equals for example 2000 table should return all books who have DateDePublication equals to 2000

this is Search Button code :

private void RechBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = repository.GetAllLivres(rechtext.Text);

        }

and search method code to return all books :

public List<Livre> FilterDateDePublication(string date)
    {
        using (var connection = factory.CreateConnection())
        {
            var livres = new List<Livre>();
            connection.ConnectionString = connectionString;
            connection.Open();
            var command = factory.CreateCommand();
            command.Connection = connection;
            command.CommandText = "select * from livre where date_publication like '%"   date   "%'";
            using (DbDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Livre l = new Livre();
                    l.Isbn = reader["isbn"].ToString();
                    l.Titre = reader["titre"].ToString();
                    l.DatePublication = DateTime.Parse(reader["date_publication"].ToString());
                    l.NombrePage = Int32.Parse(reader["nombre_page"].ToString());
                    l.Couverture = reader["couverture"].ToString();
                    l.Prix = Double.Parse(reader["nombre_page"].ToString());
                    l.QuantiteDisponible = Int32.Parse(reader["quantite_disponible"].ToString());
                }
            }
            return livres;

        }

CodePudding user response:

your function GetAllLivres just looks for books with a specifc string in their title.

command.CommandText = "select * from livre where titre like '%"   mc   "%'";

You should change that to search on the pub date. Can says how that should look because we dont know your database scheme.

By the way , do not build SQL like that, its a huge security hole. Use parametrized queries

CodePudding user response:

I know i have 4 radio buttons when one of them is checked Search function should look up by each of radio button.

Well again not seeing the UI its hard to say but my guess is you have something like this

   |-search by: --------| <<< group box
   | ( ) Author         |  << radio buttons
   | ( ) title          |
   | (*) Year           |
   ----------------------
   Search for :_________________: <<== text box
   [Start Search]                 <<== button

I hope so.

So do this

 if(radioYear.Checked){
        FilterDateDePublication(searchText.Text);
 }
 else if(radioAuthor.Checked)
    .......

Note that the date the user enters might not match the format of the date in the database, in that case you need to do some massaging

I would however refactor your code, you have surely noticed that 90% of your FilterDateDePublication is the same as the title one.

You should do

public List<Livre> ReadBooks(string query)
{
    using (var connection = factory.CreateConnection())
    {
        var livres = new List<Livre>();
        connection.ConnectionString = connectionString;
        connection.Open();
        var command = factory.CreateCommand();
        command.Connection = connection;
        command.CommandText = query;
        using (DbDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Livre l = new Livre();
                l.Isbn = reader["isbn"].ToString();
                l.Titre = reader["titre"].ToString();
                l.DatePublication = DateTime.Parse(reader["date_publication"].ToString());
                l.NombrePage = Int32.Parse(reader["nombre_page"].ToString());
                l.Couverture = reader["couverture"].ToString();
                l.Prix = Double.Parse(reader["nombre_page"].ToString());
                l.QuantiteDisponible = Int32.Parse(reader["quantite_disponible"].ToString());
            }
        }
        return livres;

    }

and then have

   List<Livre> GetByDate(string date){
        return GetBooks("select * from livre where date_publication like '%"   date   "%'";

    }

even better would be to use sql parameters. I will update this answer later

  • Related