-1

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

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

这是我在执行任务时面临的错误。

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

}

4

1 回答 1

0

请仔细阅读:

  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.");
    }
}
于 2020-04-10T21:28:46.807 回答