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)
{
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();
}
}
}