-1

I have a method (Win App C#) to fill DataGridView as below and used that in my TxB_ProitirySearch_TextChanged event:

void PrioFillGrid(bool IsSearching= false)
{
    if (IsSearching)
    {
        var ddd = from p in db.PDP_Priorities
                  where p.PriorityTitle.Contains(aski.Change(TxB_ProitirySearch.Text))
                  orderby p.ID descending
                  select new { p.ID, Title = p.PriorityTitle };

        if (ddd.Count() > 0)     // Solution1
        { 
            DG_Priority.DataSource = ddd; 
        }

        if (ddd != null)        // Solution2
        { 
            DG_Priority.DataSource = ddd; 
        }
        else
        {
            DG_Priority.DataSource = from p in db.PDP_Priorities
                                     orderby p.ID descending
                                     select new { p.ID, Title = p.PriorityTitle };
        }
    }
    else
    {
        DG_Priority.DataSource = from p in db.PDP_Priorities
                                 orderby p.ID descending
                                 select new { p.ID, Title = p.PriorityTitle };
    }
}

When I type a character, it's searching very well and updating data in gridview, but pressing backspace to clear the textbox and start a new search raises this exception:

An unhandled exception of type 'System.ArgumentNullException' occurred in System.Data.Linq.dll"

Value cannot be null. Parameter name: text

(Comment: aski.Change(TxB_ProitirySearch.Text) is a class to prevent unwanted characters from being saved in the database)

I'm wondering why both solution1 and 2 won't help.

Could anybody please help me?

Thanks in advance.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Meisam
  • 9
  • 4
  • What happens if you change `PriorityTitle.Contains` to `PriorityTitle?.Contains`? – mjwills Jul 22 '18 at 22:00
  • In your aski.Change control the input with string.IsNullOrEmpty. – Cetin Basoz Jul 22 '18 at 22:02
  • 2
    You have to look at the exception's StackTrace to localize the problem. But it is probably a wise idea to skip all of this code when TxB_ProitirySearch.Text.Length == 0, what will happen when you press the backspace key, nothing good is likely to come out of the query. – Hans Passant Jul 22 '18 at 23:20

3 Answers3

0

Great thanks to all of you, masters. changing "PriorityTitle.Contains to PriorityTitle?.Contains?" didn't work for me, but using others guide, I changed the class, aski.change() as below:

public string Change(string k)
    {
        if (k==string.Empty)
        {
            return null;
        }
        else
        {
            //some codes...
            return str;
        }
    }

and then changed my "PrioFillGrid()" method as below:

void PrioFillGrid(bool IsSearching= false)
    {
        if (aski.Change(TxB_ProitirySearch.Text) == null)
        {
            DG_Priority.DataSource = from p in db.PDP_Priorities
                                     orderby p.ID descending
                                     select new { p.ID, Title = p.PriorityTitle };
        }
        else if (IsSearching && aski.Change(TxB_ProitirySearch.Text) != null)
        {
            var ddd= from p in db.PDP_Priorities
                                     where p.PriorityTitle.Contains(aski.Change(TxB_ProitirySearch.Text))
                                     orderby p.ID descending
                                     select new { p.ID, Title = p.PriorityTitle };
            DG_Priority.DataSource = ddd;
        }
        else
        {
            DG_Priority.DataSource = from p in db.PDP_Priorities
                                     orderby p.ID descending
                                     select new { p.ID, Title = p.PriorityTitle };
        }
    }

and now is working well. Thanks again every body.

Meisam
  • 9
  • 4
0

For me the issue was that the DI mechanism created the UrlHelper instance, that caused that instance to have null for all of it's properties including routeCollection and requestContext, solved by passing the UrlHelper instance straight to the method that needed it.

Avi Maymon
  • 391
  • 2
  • 5
-2

Solution 1 and 2 do not help because the exception is thrown prior.

I am guessing in the aski.Change() method.

Try

if (IsSearching && !string.IsNullOrEmpty(TxB_ProitirySearch.Text))

Shane Ray
  • 1,259
  • 1
  • 10
  • 18
  • What should be tried (checking for an empty string) is correct, the first statement of the answer isn't. Given LINQ's deferred execution, the exception is thrown when the query is evaluated ( in this case, on the line `if (ddd.Count() >0)`). The second "solution" does nothing at all so... – Camilo Terevinto Jul 22 '18 at 23:12
  • I guess it’s a matter of semantics. Yes, it is deferred but the error is happening while evaluation of the linq query not when determining if the result of that query is > 0 – Shane Ray Jul 22 '18 at 23:16
  • The query *is* evaluated when determining its result. The assignment of the query does not evaluate it – Camilo Terevinto Jul 22 '18 at 23:17
  • I agree, that’s what my comment just said. The error happens when evaluating the result. Prior to checking that result against 0 or null. – Shane Ray Jul 22 '18 at 23:28