0

I have a DataGridView which is bound to a datasource. I have also a TextBox to filter the records. In TextChanged event of the textbox I have a this line of code: i bind the gridview through designview of the form drag the gridview and choose datasource ... and then the member table.

(gvSideMember.DataSource as DataTable).DefaultView.RowFilter = 
    string.Format("F_NAME LIKE '%{0}%'", textSearch.Text);

But when I try to filter records it shows me that object reference is not set to an instance. The datasource has records. I don't know what's going on, kindly guide me, help will be appreciated.

5
  • 1
    Is textSearch null? Commented Dec 6, 2013 at 10:09
  • no that is not nul during debug it shows value Commented Dec 6, 2013 at 10:09
  • 3
    If DataSource is not a DataTable, then (DataSource as DataTable) returns null. Commented Dec 6, 2013 at 10:11
  • yes it is the reason how to create the datasource as table i simply bind it through design not programingly Commented Dec 6, 2013 at 10:13
  • Never use the as keyword if you are not prepared to accept that the result might be null! If you say (source as Table).View you are not accepting that the as operator might give null. In that case you should use cast syntax, ((Table)source).View, instead. When the former fails, you get an non-informative exception message Object reference not set to an instance. But with the latter, you get something like Unable to cast object of type 'INTERESTING_INFO' to type 'Table', and that helps you understand what you do. So in short: Avoid as in such cases. Commented Dec 6, 2013 at 10:41

3 Answers 3

1

You are using the as keyword. Could it be that gvSideMember.DataSource, although having records, is not of the type DataTable, so that (gvSideMember.DataSource as DataTable) is null?

Sign up to request clarification or add additional context in comments.

1 Comment

i think datasource is not a table it is through the dataset
1

If DataSource is not a DataTable, then (DataSource as DataTable) returns null.

What you can do, for instance, is this:

var src = gvSideMember.DataSource;

Put a breakpoint AFTER that line, then when you hit it, QuickWatch src in Visual Studio to see its type and contents.

Then you can update your source code to use the proper type of DataSource.

EDIT If the DataSource is a DataSet, it will contain one or more tables. If it contains a single table, that is easy to retrieve:

var src = (DataSet) gvSideMember.DataSource;
var table = src.Tables[0];

But if it contains more tables, you can retrieve it with the right number (0, 1, ...) or the name:

var table = src.Tables["MyTable"];

1 Comment

Avoid as when you require the cast to succeed. Then you will see the actual type already in the exception text, and you won't have to debug to find that type.
0

You can test nullity before the statement - in the example below I'm just logging that they are null, so you'll still get the error, but you can use these checks to either decide not to perform the operation or supply defaults etc.

if (gvSideMember == null) {
    Debug.WriteLine("gvSideMember is null");
}

if (textSearch == null) {
    Debug.WriteLine("textSearch is null");
}

(gvSideMember.DataSource as DataTable).DefaultView.RowFilter = 
    string.Format("F_NAME LIKE '%{0}%'", textSearch.Text);

You can test anything that could possibly be null - even (gvSideMember.DataSource as DataTable).DefaultView if you like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.