Search code examples
c#databasevisual-studioms-access-2016icsharpcode

Searching data through a specific Field in C# from MS Access db using Visual Studio


I am working on a project and i have to perform a task in that project that i have to print/find data from a MS Access Database 2016 File through a specific text or keyword i'll try everything but can't solve my problem so after trying everything i decided to post my problem here to get some help to solve it. I attached my code you can see that but that code doesn't performing any thing and i got a error while try to search anything the error is

A first chance exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format. If there is a handler for this exception, the program may be safely continued.

This is the error I am facing while perfroming the task.

namespace Vechile_Registration_System
{
public partial class Verification : Form
{
    public Verification()
    {
        InitializeComponent();
    }

    private void Verification_Load(object sender, EventArgs e)
    {

    }

    private void btnsearch_Click(object sender, EventArgs e)
    {
        searchDataBase();
    }
    private void searchDataBase()
    {
        string strsearch = txtsearch.Text.Trim().ToString();

        StringBuilder sb = new StringBuilder();
        vehicleBindingSource.Filter = string.Format("[Registration No] LIKE '%{0}%'", strsearch);

        string strFilter = sb.ToString();
        vehicleBindingSource.Filter = strFilter;

        if (vehicleBindingSource.Count != 0)
        {
            dataGridView1.DataSource = vehicleBindingSource;
        }
        else
        {
            MessageBox.Show("No Records Found. \n The Vehcile may not register or you have enter wrong Registration Number.");
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form1 MMenu = new Form1();
        MMenu.ShowDialog();
    }
}

}


Solution

  • PLEASE READ CAREFULLY:

    1. You've deleted a very important line and because of this, no data is being loaded into your data source.

    We are making the first change in Verification.cs file. Change the Verification_Load exactly like this:

        private void Verification_Load(object sender, EventArgs e)
        {
            vehicleTableAdapter.Fill(vehicleDataSet.Vehicle);
            // If you want the grid view to show no data at the beginning
            // Uncomment the following line
            // vehicleBindingSource.Filter = "1 = 0";
        }
    
    1. You've somehow managed to remove the searchbutton_Click event handler.

    Please apply these steps exactly as they are suggesting:

    • In the solution explorer double click the Verification.cs. This should open the form in design mode.
    • On the form, right click the SEARCH button and select Properties from the menu.
    • In the Properties window at the top, there are some icons. Find the "Events" icon with a thunderbolt (lightning) shape. Click this.
    • Now, at the very top, there is the "Click" event.
    • Be very careful with typing and enter btnsearch_Click

    And that's all.

    Hope this helps.

    Please mark as answer if it does



    **** ORIGINAL ANSWER ****

    This should solve your problem.

    Please use exactly this code.

    private void searchDataBase()
    {
        string strsearch = txtsearch.Text.Trim().ToString();
    
        StringBuilder sb = new StringBuilder();
        vehicleBindingSource.Filter = string.Format("[Registration No] LIKE '%{0}%'", strsearch);
    
        if (vehicleBindingSource.Count != 0)
        {
            dataGridView1.DataSource = vehicleBindingSource;
        }
        else
        {
            MessageBox.Show("No Records Found. \n The Vehcile may not register or you have enter wrong Registration Number.");
        }
    }