1

First off, I am pretty new to C# and windows forms. Usually I am able to find an answer to my questions online but this one has eluded me.

I think what I want to do is something pretty simple but errors keep getting in my way. I have a text box and a listbox on a form. I want the user to be able to find a name in a datatable quickly by starting to type the name in the textbox and all names that fit what he typed so far would appear in the listbox.

So, I have an Access database that I am putting into Tableadapters. I am then putting the data from a table that lists employees into a DataTable. My textbox is called textbox1. Ideally I would be able to use the '*' character as a wildcard on the search like this:

private void textBox1_TextChanged(object sender, EventArgs e)
{    
    //Put data into the datatable and seach
    DataTable EmployeeTable = this.tblEmployeesTableAdapter.GetData();
    var EmployeeNames = from Employees in EmployeeTable.AsEnumerable()
                        where Employees.Field<string>("Full Name") == * + textbox1.Text + *
                        select Employees.Field<string>("Full Name");
    this.listBox1.DataSource = EmployeeNames.ToList();
}

Since there doesn't seem to be any wildcard characters I use the .Contains method like this:

DataTable EmployeeTable = this.tblEmployeesTableAdapter.GetData();
var EmployeeNames = from Employees in EmployeeTable.AsEnumerable()
                    where Employees.Field<string>("Full Name").Contains(textBox1.Text) 
                    select Employees.Field<string>("Full Name");
this.listBox1.DataSource = EmployeeNames.ToList()

The problem is when I run this code I get the error "object reference not set to an instance of an object". I believe that this is caused because the 'Full Name' datacolumn in my datatable contains names with whitespace between the first and last name in the form "Tom Riddle". There is another datacolumn in my table just containing the first name with no white space and if I instead say:

where Employees.Field<string>("First Name").Contains(textBox1.Text)

This works fine with no errors. The 'First Name' column has the same properties as the 'Full Name' column except it does not contain spaces. I would use this but I want the form user to be able to start typing in the textbox either the first or last name and the listbox to display any matches.

To make matters even more confusing for me, if I just select all full names in the datatable and then go through each one like this I still get the error:

var EmployeeNames = from Employees in EmployeeTable.AsEnumerable()
                    select Employees.Field<string>("Full Name");

foreach (string EmployeeName in EmployeeNames)
    if (EmployeeName.Contains(textbox1.Text))
         this.listBox1.Items.Add(EmployeeName);

Yet if I define a string myself:

string name = "Tom Riddle";
bool does_contain = name.Contains("To");

This works fine.

I guess I am a bit confused by this. Also, I know there are other ways I could be doing this such as splitting the string and seaching each seperate string or having a seperate text box on the main form for first and last names and then seaching the datatable based on the first and last name columns seperately. This is the cleanest way to do what I want so I am just trying to understand what I am doing wrong. I appreciate everyones help.

jkcaldwe
  • 13
  • 3
  • Well, obviously one of the strings being returned is a `null`-reference. The names with spaces are not the cause here. Most likely there are rows with NULL values in that column, and some rows with empty strings. Depending on the tool you use to look at the data, they may look the same, but are read and processed differently. – Lasse V. Karlsen Mar 07 '14 at 15:43
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ken2k Mar 07 '14 at 15:45

1 Answers1

2

cant you change the code to check for nulls?

!String.IsNullOrEmpty(string)

var EmployeeNames = from Employees in EmployeeTable.AsEnumerable()
                    select Employees.Field<string>("Full Name");

foreach (string EmployeeName in EmployeeNames)
    if (!String.IsNullOrEmpty(EmployeeName) && EmployeeName.Contains(textbox1.Text))
         this.listBox1.Items.Add(EmployeeName);

As suggested by aevitas, consider using IsNullOrWhitespace.

string.IsNullOrEmpty(string) vs. string.IsNullOrWhiteSpace(string)

Community
  • 1
  • 1
WraithNath
  • 16,543
  • 8
  • 50
  • 79
  • 1
    You'll probably want to use `!string.IsNullOrWhiteSpace` instead, as a whitespace isn't a valid entry in this case either. – aevitas Mar 07 '14 at 15:47
  • Added a link comparing the two :) – WraithNath Mar 07 '14 at 15:51
  • Shoot. I had a null reference burried in my database that I hadn't fixed before. This explains it. I'll take you advice and change the code for future issues though. Thanks a lot! – jkcaldwe Mar 07 '14 at 16:06