Find Items inside the ListBox Based on TextBox Using C# and MySQL Database
Submitted by janobe on Wednesday, July 31, 2019 - 18:18.
In this tutorial, I will teach you how to find items in the ListBox using C# and MySQL Database. This method has the ability to find the data from the database and display it inside the ListBox. This procedure is very easy and you can be done in no time. Just follow the step by step guide to know how it works
The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Database
1. Install the XAMPP on your machine. 2. Open the XAMPP and start MySQL and Apache 3. Open the browser and type this url (localhost/phpmyadmin) to access PHPMyAdmin. 4. Create a new database named “dbsubjects” 5. Do the following query to add a table and data in the database that you have created.- --
- -- Dumping data for table `subject`
- --
- (8, 'English Plus', 'English Plus', 3, 'Nones'),
- (14, 'NSTP1', 'National Service Training Program 1', 3, 'None'),
- (15, 'PE1', 'Physical Education 1', 2, 'None'),
- (16, 'HUMAN', 'Humanities', 3, 'None'),
- (17, 'COMART2', 'Communication Arts 2', 3, 'COMART1'),
- (18, 'COPRO-2', 'Computer Programming 2', 4, 'COPRO1'),
- (19, 'DATSTRUC', 'Data Structures', 4, 'COPRO1'),
- (20, 'DISTRUC', 'Discrete Structure', 3, 'ALGEBRA'),
- (21, 'NSTP2', 'National Service Training Program 2', 3, 'None'),
- (22, 'INTROART', 'Introduction to Arts', 3, 'None'),
- (23, 'PE2', 'Physical Education 2', 2, 'PE1'),
- (24, 'TRIGO', 'Trigonometry', 3, 'ALGEBRA'),
- (25, 'COMART3', 'Communication Arts 3', 3, 'COMART2'),
- (26, 'COPRO-3', 'Computer Programming 3', 4, 'COPRO2'),
- (27, 'LOGIC', 'Logic Design and Switching', 3, 'DISTRUC'),
- (28, 'PHILGOV', 'Philippine Government', 3, 'None'),
- (29, 'PHYSICS1', 'Physics 1', 4, 'None'),
- (30, 'STAT', 'Statistics', 3, 'ALGEBRA'),
- (31, 'SOCSTUD', 'Social Studies', 3, 'None'),
- (32, 'PE3', 'Physical Education 3', 2, 'PE1'),
- (33, 'COMART4', 'Communication Arts 4', 3, 'COMART3'),
- (34, 'ASSEM', 'Assembly Language', 4, 'LOGIC'),
- (35, 'PHILO', 'Philosophy', 3, 'None'),
- (36, 'SADSIGN', 'System Analysis and Design', 3, 'COPRO1'),
- (37, 'PHYSICS2', 'Physics 2', 4, 'Physics 1'),
- (38, 'DBSYS', 'Theory Database Systems', 3, 'DATSTRUC'),
- (39, 'PE4', 'Physical Education 4', 2, 'PE1'),
- (40, 'Eng 111', 'CommunicationArts 1', 3, 'none'),
- (41, 'Fil 111', 'Kom sa Akad. Fil', 3, 'NONE'),
- (42, 'Math 1', 'Basic math ', 3, 'NONE'),
- (43, 'SCE 111', 'Earth Science', 3, 'NONE '),
- (44, 'Lit 111', 'Philippines Literature ', 3, 'NONE'),
- (45, 'Rdg 1', 'Dev. Reading 1', 3, 'NONE'),
- (46, 'Psych 116', 'General Psychology', 3, 'NONE'),
- (47, 'PE 111', ' Physical Fitness', 2, 'NONE'),
- (48, 'NSTP 111', 'National Service training program 1 ', 3, 'NONE'),
- (49, 'Eng 211', 'speech & oral communication', 3, 'Eng 121'),
- (50, 'Fil 211', 'Masining na pagpapahayag', 3, 'Fil 121'),
- (51, 'Lit 121 ', 'World literature', 3, 'Lit 111'),
- (52, 'SocSci 132', 'Rizal & other heroes ', 3, 'NONE'),
- (53, 'Ed 211', 'Child & adolescent Psychology', 3, 'Psyh 116'),
- (54, 'Ed 212', 'Education Technology 1', 3, 'NONE'),
- (55, 'FS 1', 'Observation of learners Dev\'t & the school enviroment ', 3, 'NONE'),
- (56, 'Eng 212', 'The Teaching Of speaking ', 3, 'NONE '),
- (57, 'Eng 213', 'The teaching of lsting & Rdg.', 2, 'NONE'),
- (58, 'PE 211', 'Team sports', 2, 'PE 121 '),
- (59, 'Stat 115', 'Intro. to Statistics ', 3, 'NONE'),
- (60, 'Ed 311', 'The teaching profession ', 3, 'NONE'),
- (61, 'Ed 312', 'Assessment of Teaching 2 ', 3, 'Ed 221'),
- (62, 'Ed 313', 'Principles of teaching', 3, 'Ed 224'),
- (63, 'FS 3', 'Micro-Teaching &the use of technology', 1, 'FS 2'),
- (64, 'Eng 311', 'Arfo- Asian Literature ', 3, 'NONE '),
- (65, 'Eng 312', 'Introduction to Stylistics ', 3, 'NONE'),
- (66, 'Eng 313', 'Translating & Editing of text ', 3, 'NONE'),
- (67, 'Eng 314', 'Teaching Literature', 3, 'NONE'),
- (68, 'Ed 314', ' Teaching Multigrade Class', 1, 'NONE'),
- (69, 'Ed 412', 'Use of popular Media ', 1, 'NONE'),
- (70, 'FS 5', 'Assestment of student Learning ', 1, 'FS 4'),
- (71, 'FS 6 ', 'Becoming a Teacher', 1, 'FS 5'),
- (72, 'Hum116', 'Art , Man & Society', 3, 'Hum 111'),
- (73, 'Eng 411', 'language & literature assestment', 3, 'NONE'),
- (74, 'Eng 412', 'lang. Curr. for sec. sch.', 3, 'NONE'),
- (75, 'Eng 413', 'Literary Criticism ', 3, 'NONE'),
- (76, 'Eng 414', 'Language Research ', 3, 'NONE'),
- (77, 'Eng 415', 'Dramatic & Stagecrafts', 3, 'NONE '),
- (78, 'CS 113', 'Basic Software Package w/ Internet Application', 3, 'None'),
- (79, 'Eng 111', 'Communication Arts', 3, 'None'),
- (80, 'Fil 111', 'Kom. sa Akad. Fil', 3, 'None'),
- (81, 'Math 1', 'Basic Math', 3, 'None'),
- (82, 'SCE 111', 'Earth Science', 3, 'None'),
- (83, 'Read 1', 'Dev. Reading 1', 3, 'None'),
- (84, 'Psych 116', 'General Psycology', 3, 'None'),
- (85, 'PE 111', 'Physical Fitness', 2, 'None'),
- (86, 'NSTP 111', 'Nat\'l Service Trng. Prog. 1', 3, 'None'),
- (87, 'Eng 211', 'Speech and Oral Communication', 3, 'Eng 121'),
- (88, 'Fil 211', 'Masining na Pagpapahayag', 3, 'Fil 121'),
- (89, 'Lit 121', 'World Literature', 3, 'Lit 111'),
- (90, 'SocSci 132', 'Rizal and Other Heroes', 3, 'None'),
- (91, 'Ed 211', 'Child Psycology', 3, 'Psych 116'),
- (92, 'Ed 212', 'Educational Technology 1', 3, 'None'),
- (93, 'FS 1', 'Observation of Learning Dev\'t and the School Environment', 1, 'None'),
- (94, 'Eng 212', 'The Teaching of Speaking', 3, 'Eng 121'),
- (95, 'Math 3', 'Elem. Theory of Numbers', 3, 'Math 2'),
- (96, 'PE 211', 'Team Sports', 2, 'PE 121'),
- (97, 'JEEP Start 1', ' ', 3, 'None'),
- (98, 'Stat 115', 'Intro. To Statistics', 3, 'None');
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
Step 2
Add a ListBox,Label, and a TextBox inside the Form. Then do the Form just like shown below.
Step 3
Press F7 to open the code editor. In the code editor, add a namespace to accessMySQL
libararies.
- using MySql.Data.MySqlClient;
Step 4
Create a connection between C# and MySQL database. After that, declare and initialize all the classes and variables that are needed.- MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=dbsubjects;sslMode=none");
- MySqlCommand cmd;
- MySqlDataAdapter da;
- DataTable dt;
- string sql;
Step 5
Create a method for retrieving data in the database and display it inside the ListBox.- private void retrieve_item(string sql,ListBox lst)
- {
- try
- {
- con.Open();
- cmd.Connection = con;
- cmd.CommandText = sql;
- da.SelectCommand = cmd;
- da.Fill(dt);
- lst.DataSource = dt;
- lst.DisplayMember = dt.Columns[1].ColumnName;
- lst.ValueMember = dt.Columns[0].ColumnName;
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- con.Close();
- da.Dispose();
- }
- }
Step 6
Go back to the design view, double click the Form to open theload event
handler of it. After that, add this code inside the “Form1_Load” event to retrieve all the data in the database.
- sql = "SELECT * FROM `subject`";
- retrieve_item(sql, listBox1);
Step 7
Go back to the design view, double click the TextBox to open theTextChanged event
handler of it. After that, add this code inside the “textBox1_TextChanged” event to search the specific data in the database.
- sql = "SELECT * FROM `subject` WHERE `SUBJ_CODE` LIKE '%" + textBox1.Text + "%'";
- retrieve_item(sql, listBox1);
Add new comment
- 330 views