【问题标题】:Searching data through a specific Field in C# from MS Access db using Visual Studio使用 Visual Studio 从 MS Access db 通过 C# 中的特定字段搜索数据
【发布时间】:2020-04-10 13:05:43
【问题描述】:

我正在做一个项目,我必须在该项目中执行一项任务,我必须通过特定文本或关键字从 MS Access 数据库 2016 文件中打印/查找数据我会尝试一切但无法解决我的问题所以在尝试了一切之后我决定在这里发布我的问题以获得一些帮助来解决它。 我附上了我的代码,您可以看到,但该代码没有执行任何操作,并且在尝试搜索错误时出现错误

“System.FormatException”类型的第一次机会异常发生在 mscorlib.dll 中 附加信息:输入字符串的格式不正确。 如果有这个异常的处理程序,程序可以安全地继续。

这是我在执行任务时遇到的错误。

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

}

【问题讨论】:

标签: c# database visual-studio ms-access-2016 icsharpcode


【解决方案1】:

请仔细阅读:

  1. 您删除了一个非常重要的行,因此没有数据加载到您的数据源中。

我们正在对 Verification.cs 文件进行第一次更改。完全像这样更改 Verification_Load:

    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. 您以某种方式设法删除了 searchbutton_Click 事件处理程序。

请完全按照他们的建议应用这些步骤:

  • 在解决方案资源管理器中双击 Verification.cs。这应该会以设计模式打开表单。
  • 在表单上,​​右键单击“搜索”按钮并从菜单中选择“属性”。
  • 在顶部的“属性”窗口中,有一些图标。找到带有雷电(闪电)形状的“事件”图标。点击这里。
  • 现在,在最顶部,有“点击”事件。
  • 键入时要非常小心并输入btnsearch_Click

仅此而已。

希望这会有所帮助。

如果有请标记为答案



**** 原始答案 ****

这应该可以解决您的问题。

请完全使用此代码。

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.");
    }
}

【讨论】:

  • 这段代码没有解决我的问题,现在当我点击搜索按钮时代码没有生成任何输出。
  • 你能把你的解决方案上传到哪里让我修复吗?
  • 刚刚在聊天中向您发送了一条消息,我在其中附上了一个链接,您可以从中下载solution。如果您fix,请通过聊天告诉我。
  • 我正在更新我的答案。请检查它的顶部。你犯了错误,我会告诉你如何修复它。如果解决了,请标记为答案(它现在对我有用。搜索并可以找到并显示记录)
猜你喜欢
  • 1970-01-01
  • 2017-07-09
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 2017-06-12
相关资源
最近更新 更多