C# And MySQL Search Data Between 2 Dates

How To Search Data In MySQL Database Between Two Dates Using C#

C# And MySQL Search Records Between Two Date

In This C# Tutorial  We Will See How To Get Records Between Two Date From MySQL Database Table Using Two DateTimePicker And Display The Selected Records Into A DataGridView Rows Using Csharp Programming Language And Visual Studio Editor.


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Csharp_Tutorials
{
    public partial class Search_Data_In_MySQL_Between_2_Date : Form
    {
        public Search_Data_In_MySQL_Between_2_Date()
        {
            InitializeComponent();
        }

        // mysql connection
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='mydb';username=root;password=");
        
        private void Search_Data_In_MySQL_Between_2_Date_Load(object sender, EventArgs e)
        {
            // populate datagridview from database using mysql data adapter
            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM student", connection);
            DataTable table = new DataTable();

            adapter.Fill(table);

            dataGridView1.DataSource = table;
        }

        // button search
        private void BTN_Search_Click(object sender, EventArgs e)
        {

            // create a command with 2 parameters [ d1, d2 ]
            // mean the first date and second one 
            MySqlCommand command = new MySqlCommand("SELECT `id`, `first_name`, `last_name`, `birthdate`, `address` FROM `student` WHERE `birthdate` BETWEEN @d1 AND @d2", connection);

            // add values to the parameters form dateTimePickers
            command.Parameters.Add("@d1", MySqlDbType.Date).Value = dateTimePicker1.Value;
            command.Parameters.Add("@d2", MySqlDbType.Date).Value = dateTimePicker2.Value;

            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM student", connection);
            DataTable table = new DataTable();

            adapter.Fill(table);

            dataGridView1.DataSource = table;

        }
    }
}

      
///////////////OUTPUT:

Search Records Between Two Dates Using Database And C#



Java Populate JTable From LinkedList

How To Fill A JTable With Data From LinkedList Using Java NetBeans

Populate JTable From  LinkedList Using Java


In this Java Tutorial we will see How To Populate A JTable With A Linked List Of Object By Creating A User Class Using For Loop And DefaultTableModel In Java NetBeans .



Project Source Code:


// create a class to use with the linkedList
    class User{
        
        private String firstName;
        private String lastName;
        private int age;
        
        public User(String fn, String ln, int ag){
            this.firstName = fn;
            this.lastName = ln;
            this.age = ag;
        }
        
    }
    
    
    
    public void populateTableWithLinkedList(){
        
        // create a user linkedList
        LinkedList<User> list = new LinkedList<>();
        
        // create users
        User u1 = new User("AA","BB",10);
        User u2 = new User("BB","CC",20);
        User u3 = new User("CC","DD",30);
        User u4 = new User("DD","EE",40);
        User u5 = new User("EE","FF",50);
        User u6 = new User("LL","MM",80);
        User u7 = new User("NN","TT",100);
        
        // add the users to the list
        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);
        list.add(u6);
        list.add(u7);
        
        // get jtable default model
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        
        // populate the jtable with the list
        Object[] row;
        for(int i = 0; i < list.size(); i++){
            row = new Object[3];
            row[0] = list.get(i).firstName;
            row[1] = list.get(i).lastName;
            row[2] = list.get(i).age;
            
            model.addRow(row);
        }
    }

OutPut:

Fill JTable From LinkedList In Java