简体   繁体   English

在数据库表中搜索条目时出现NullReferenceException错误

[英]NullReferenceException error when searching database table for entries

I'm working on a relatively straight forward C# project that connects an Access Database via OleDb with various functions such as adding, deleting and editing records. 我正在开发一个相对直接的C#项目,它通过OleDb连接Access数据库,具有添加,删除和编辑记录等各种功能。 Now, everything has worked fine up until trying to implement a database search. 现在,在尝试实现数据库搜索之前,一切都运行良好。

The following line (in the searchbox code) throws up the exception during debug, but I am unsure what is actually a null value that is causing it to break. 以下行(在搜索框代码中)在调试期间抛出异常,但我不确定实际上是什么是导致它中断的空值。

ReturnedResults = DBDataSet.Tables["Movies"].Select("Title like '%" + Search + "%'");

So the goal is to search through the 'Movies' table in the database and find movies based on the user's input (stored in the 'Search' string). 因此,我们的目标是搜索数据库中的“电影”表,并根据用户的输入(存储在“搜索”字符串中)查找电影。

The search box's code can be found below, and the database initialisation and connection is provided at the bottom. 搜索框的代码可以在下面找到,数据库初始化和连接在底部提供。

private void SearchTextBox_Changed(object sender, EventArgs e)
{
  string SearchString = SearchTextBox.Text.ToString();
  int Results = 0;
  DataRow[] ReturnedResults;

  ReturnedResults = DataSet.Tables["Movies"].Select("Title like '%" + SearchString + "%'");
  Results = ReturnedResults.Length;

  if (Results > 0)
 {
    SearchResultsBox.Items.Clear();
    for (int i = 0; i <= Results; i++)
    {
      DataRow Row;
      Row = ReturnedResults[i];
      SearchResultsBox.Items.Add(Row["Title"].ToString());
    }
 }
  else
 {
   SearchResultsBox.Items.Clear();
   MessageBox.Show("Error! No items found.");
 }
}

For some context here is the main database initialisation/connection and the form load events: 对于某些上下文,这里是主数据库初始化/连接和表单加载事件:

public partial class BaseForm : Form
{
  System.Data.OleDb.OleDbConnection Connection;

  DataSet DataSet;

  System.Data.OleDb.OleDbDataAdapter DataAdapter;

  int MaxRows = 0;
  int CurrentRow = 0;

public BaseForm()
{
  InitializeComponent();
}

private void BaseForm_Load(object sender, EventArgs e)
{
  Connection = new System.Data.OleDb.OleDbConnection();
  Connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=Movies.accdb";
  Connection.Open();

  DataSet = new DataSet();

  string sq1 = "SELECT * From Movies";
  DataAdapter = new System.Data.OleDb.OleDbDataAdapter(sq1, DBConnection);
  DataAdapter.Fill(DBDataSet);

  MaxRows = DataSet.Tables[0].Rows.Count;
  DisplayRecord();
}

Multiple things can be null in your statement. 语句中的多个内容可以为null。 DBDataSet can be null, or the table Movies doesn't exits. DBDataSet可以为null,或者表Movies不退出。 You may add following check before the line. 您可以在该行之前添加以下检查。

if(DBDataSet != null && 
   DBDataSet.Tables.Count > 0 && 
   DBDataSet.Tables["Movies"] != null)
{
    ReturnedResults = DBDataSet.Tables["Movies"].Select("Title like '%" + Search + "%'");
    //....  rest of your code

You may also try accessing the record against Tables[0] instead of Table Movies 您也可以尝试访问Tables[0]而不是表格Movies

EDIT: 编辑:

For your next problem, mentioned in the comment, your for loop is executing till the length. 对于您在评论中提到的下一个问题,您的for循环正在执行,直到长度。 It should be like: 应该是这样的:

for (int i = 0; i < Results; i++) // Less than (<) not less than equal to 
    {
      DataRow Row;
      Row = ReturnedResults[i];
      SearchResultsBox.Items.Add(Row["Title"].ToString());
    }

You need to modify the condition and remember the index starts from 0 and you will get rows till Result - 1 您需要修改条件并记住索引从0开始,您将获得直到Result - 1

In your case multiple null reference is occur in your code as per your comment you got error in following line ReturnedResults = DBDataSet.Tables["Movies"].Select("Title like '%" + Search + "%'"); 在您的情况下,您的代码中会出现多个空引用,根据您的注释,您在以下行中出现错误.RingnedResults = DBDataSet.Tables [“Movies”]。选择(“Title like'%”+ Search +“%'”);

In above DBDataSet may be null or its table (Movies) has no row etc.. so better way t check before filter 在上面DBDataSet可能为null或其表(Movies)没有行等。所以更好的方法是在过滤之前检查

if(DBDataSet != null && 
   DBDataSet.Tables.Count > 0 && 
   DBDataSet.Tables["Movies"] != null && DBDataset.Tables["Movies"].Rows.Count > 0)
{

ReturnedResults = DBDataSet.Tables["Movies"].Select("Title like '%" + Search + "%'");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NullReferenceException 错误数据库数据到表 html - NullReferenceException Error Database data to table html 对数据库上下文执行SaveChanges时发生NullReferenceException错误 - NullReferenceException error when executing SaveChanges to database context 在列表内的值上搜索时,C#Linq NULLReferenceException - C# Linq NULLReferenceException when searching on value inside list 模拟我的处理程序数据库时出现 NullReferenceException - NullReferenceException when mock my handler database NullReferenceException仅在从数据库打开图像时 - NullReferenceException only when opening images from database 从datagridview更新数据库时发生NullReferenceException - NullReferenceException when update database from datagridview NullReferenceException在for循环中创建标签数组时出错 - NullReferenceException Error when creating array of labels in for loop Datagridview NullReferenceException 在计时器中更新时出错 - Datagridview NullReferenceException Error when renewing in timer NullReferenceException错误 - NullReferenceException error 带有来自数据库的信息的表未显示:System.NullReferenceException - Table with information from database not showing up : System.NullReferenceException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM