Click here to Skip to main content
14,458,335 members
Rate this:
Please Sign up or sign in to vote.
See more:
C#, Windows Forms - I did almost 90% of the problem but Search Button is not working!
Given Access file with the data (below). I need to build a program (GUI application) that uses LINQ to get data from the database file called (students.accdb).

I need radio buttons to select and search by last name or program.
I should be able to search if full or partial of Last Name is entered as well.
The result must display in a list box that scrolls.
The result should be like this: LastName, FirstName (StudentID) ProgramName
Clear button must clear the list box.
Query Hint: Use the Contains function to compare strings in your query.

I am unable to get the Search or Clear buttons to work! I tried the Search_Click and the SelectedIndexChanged but I can't figure out what I am missing. This is the first time I do Search in C# Windows Forms. I have properly defined the names of the ListBox, TextBox, Search Button, Clear Button and Radio Buttons.

I have defined:
Name of List Box: lstResultListBox
Name of Text Box: LastNameProgramTextBox
Name of Search Button: SearchButton
Radio button 1: byLastNameRadioButton
Radio button 2: byProgramRadioButton
Clear Button: ClearButton


Can you please help?
Thanks

Here is a screenshot (sorry I couldn't find a place to upload an image)
https://i.stack.imgur.com/BHsAw.png[][^]

This is the table in the Access file:
StudentID ProgramName	ProgramCode	LastName	FirstName
200056	   Maths	2522	        Anderson	Gregory
200057	   Maths	2522	        Anderson	John
200058	   Computer	2421	        Bay	        Samuel
200059	   Maths	2522	        Brown	        James
200060	   Computer	2389	        Brown	        Alicia
200061	   English	2421	        Doe	        Sally
200062	   English	2421	        George	        Jane
200063	   Maths	2522	        Gray	        Mark


What I have tried:

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;

namespace StdudentSearch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'simpleStudentDataSet.Student' table. You can move, or remove it, as needed.
            this.studentTableAdapter.Fill(this.simpleStudentDataSet.Student);
        }

        private void Search_Click(object sender, EventArgs e)
        {
            var students =
            from s in this.simpleStudentDataSet.Student
            where s.LastName == LastNameProgramTextBox.Text
            where s.ProgramName == LastNameProgramTextBox.Text
            select s;
            foreach (var s in students)
            {
                lstResultListBox.Items.Add(s.LastName + ", " + s.FirstName);
            }
        }

        private void lstResultListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LastNameProgramTextBox.Text = lstResultListBox.SelectedItem.ToString();
        }
    }
}
Posted
Updated 30-Mar-19 23:15pm
Comments
Richard MacCutchan 31-Mar-19 3:38am
   
I do not know the full answer, but you should not use TextBox.Text items directly in your LINQ code; especially not the same one for different fields. Look at the values the user entered to see if they are full or partial names. What about searching for everyone whose name begins with "Smit" for example?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Instead of searching, use filtering of your original data. It's easy enough to do:

private BindingSource displaySource = new BindingSource();
private void frmMain_Shown(object sender, EventArgs e)
    {
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(@"Data Source=GRIFF-DESKTOP\SQLEXPRESS;Initial Catalog=MyVideos;Integrated Security=True"))
        {
        con.Open();
        using (SqlDataAdapter da =  new SqlDataAdapter("SELECT Id, Title FROM Videos", con))
            {
            da.Fill(dt);
            }
        }
    MyListBox.DisplayMember = "Title";
    MyListBox.ValueMember = "ID";
    displaySource.DataSource = dt.DefaultView;
    MyListBox.DataSource = displaySource;
    }
private void showOnlyThese_TextChanged(object sender, EventArgs e)
    {
    displaySource.Filter = $"Title LIKE '%{showOnlyThese.Text}%'";
    }
It's quicker, and a load more responsive for the user!
   
Comments
Member 14017552 31-Mar-19 10:21am
   
I can't, I have to use
private void Search_Click(object sender, EventArgs e)
OriginalGriff 31-Mar-19 10:30am
   
Then move the single line of filter code into the click handler...
Member 14017552 31-Mar-19 12:54pm
   
Come on man, this is so confusing! besides, I am already calling the table from an Access file, why do I need this SQL connection? I am almost there, I am just missing some code to search and filter!
OriginalGriff 31-Mar-19 13:09pm
   
:sigh:
Development is about thinking, and adapting code fragment to fit your specific needs. I used SQL server for testing and to show how much code was actually needed- you can use and DB system you want, it all works the same.
BillWoodruff 1-Apr-19 14:48pm
   
+5 whoever downvoted this: shame on you !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100