0

I have following code:

In a Form:

public void filterType_TextChanged(object sender, EventArgs e)
    {
        var dSearch = new D_Search(this);
        dSearch.filterD(sender);
     }

So I have a Textbox event where I call a filterD function in another class dSearch. In the class dSearch I have:

    public D_Search(Form1 frm1)
    {
        form1 = frm1;
    }

      public String filterD(object sender)
    {
        string val = String.Empty;

        if (sender == form1.filterType())
        {
            val = (sender as TextBox).Text;
           //havent written the whole SQL Command here
            sqlCmd = new SqlCommand("SELECT * FROM, connection); 
        }

        datTable = new DataTable();
        sqlDatAdapter = new SqlDataAdapter(sqlCmd.CommandText,
        connection); //causes NullReferenceException
        sqlDatAdapter.Fill(datTable);

        form1.setDataGrid = datTable;

        return val;
    }

So I have multiple functions in my Form like filterType which are Textbox events. Now I want to pass them to my Class which should notice with if-statement what event was called (what Textbox was changed) but I get a NullReference Exception at sqlDatAdapter. What should I do?

EDIT: It's the cmd that is null. Additionally:

Here is the filterType function:

   public String filterType()
    {
        return filterTypeNumber.Text;
    }

EDIT2: The if statement is not used, so he does not recognize the sender, because he compares if the sender is the Textbox entry. What should I do?

uzi42tmp
  • 271
  • 2
  • 9
  • 22
  • [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) – Soner Gönül Jan 21 '15 at 06:45
  • Yes I know what that is but I don't know how to fix it in my case – uzi42tmp Jan 21 '15 at 06:47
  • What is value of "form1.filterType()" ? are you sure that "sqlCmd" is not null ? – H. Mahida Jan 21 '15 at 06:47
  • filterType() is the function I created in my Form to pass the Textbox entries to my Class. That works. Yes I guess too that sqlCmd is null but why? When I use it in my Form like that it works. How can I pass that? – uzi42tmp Jan 21 '15 at 06:52
  • What is type of value, as "sender" is object which you are trying to compare..!! show your code for filterType() function if possible – H. Mahida Jan 21 '15 at 06:59

3 Answers3

1

So change your IF statement to;

if ( (sender as TextBox).Text== form1.filterType())
    {

       //havent written the whole SQL Command here
        sqlCmd = new SqlCommand("SELECT * FROM, connection); 
    }

Also make sure that connection is open.

Hope it helps..!!!

H. Mahida
  • 2,356
  • 1
  • 12
  • 23
0

You doesn't Open the Connection

 Con.OPen();    
 //logic here
 cmd.ExecuteNonQuery();
 Con.Close();

//Data resides in sqlCmd Object

sqlCmd = new SqlCommand("SELECT * FROM, connection);

Try by passing sqlCmd as argument

sqlDatAdapter = new SqlDataAdapter(sqlCmd,connection); 
MMM
  • 3,132
  • 3
  • 20
  • 32
  • I don't think it's the connection. It worked without it. He loads the database but I can't search in it. I tried your way and it didn't work. I have seen its the cmd that's null – uzi42tmp Jan 21 '15 at 07:05
0

Make sure you've instantiated connection, so it is not null. Assuming that is not your problem, it seems to me like you might be getting the NullRefEx from sqlCmd, which might not be instantiated when (sender == form1.filterType()) is false. If so, you should be able to solve it by moving things around a little, for example:

// Make the table available outside the if-else, even if it is empty:
datTable = new DataTable(); 

if (sender == form1.filterType())
{
    val = (sender as TextBox).Text;
    //havent written the whole SQL Command here
    sqlCmd = new SqlCommand("SELECT * FROM, connection); 

    // Only do this if it is relevant, i.e. here within the if-structure,
    // that way, you should be able to avoid the nullRef:
    sqlDatAdapter = new SqlDataAdapter(sqlCmd.CommandText, connection); 
    sqlDatAdapter.Fill(datTable);
}

// You might have to check something extra here, depending on
// usage, since the table might be empty:
form1.setDataGrid = datTable;

Update in response to comment:

I'm not sure I follow you, but if you want to move the sqlCmd out again, that is fine - just make sure it is not null, even when sender == form1.filterType() is false. If you have something else to populate the sqlCmd.CommandText with before you instantiate the SqlDataAdapter, then make sure you do that first. Otherwise, you could probably do something like the following instead, and place all of this outside the other if-structure:

if(sqlCmd != null && !String.IsNullOrEmpty(sqlCmd.CommandText)){
    sqlDatAdapter = new SqlDataAdapter(...)
    ...
}

Either way, you have to avoid the null reference in one of two ways: Either populate sqlCmd, so it is not null, or avoid calling it.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
  • Ok when I move that then I don't get an Exception anymore but the program does nothing when I type something in the Textbox. The Problem is that I wanted to move the sqlAdapter lines out of the if statement, thats the reason I've written the Class. So that depending on the Textbox he chooses the SELECT statement and then moves on – uzi42tmp Jan 21 '15 at 06:58
  • @uzi42tmp I'm not sure I understand what you want to do, but see my update, maybe that will clarify things a little? – Kjartan Jan 21 '15 at 07:15
  • I have seen now what the error is. He doesn't jump into the if statement! So he doesn't even know that this textbox is called – uzi42tmp Jan 21 '15 at 07:16