Amir Jalali
Amir Jalali

Reputation: 3212

whats is wrong with this linq query?

i have a product table which has a Title as string.

inside my view i have

    @using (Html.BeginForm("Serach","Store"))
        {

          <input type="text" name="q" class="searchbox_textbox" />
          <input  type="submit" class="searchbox_btn" />
          }

and inside my controller i have

        public ActionResult Serach(string q)
    {
        var result = storeDB.Products
                     .Where(p => p.Title.Contains(q) || string.IsNullOrEmpty(q));


        return View(result);

    }

when i run the page and type a word to search for it give me this error

The specified argument value for the function is not valid. [ Argument # = 1,Name of function(if known) = isnull ] Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlServerCe.SqlCeException: The specified argument value for the function is not valid. [ Argument # = 1,Name of function(if known) = isnull ]

whats the problem? and what should i do if i want to show users a meesage that tell them your serach didn't match any product

Upvotes: 0

Views: 344

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500505

Well, you should use some logging to find out what's actually being sent to the database - but personally I'd split the query up before it gets there:

public ActionResult Search(string q)
{
    var result = string.IsNullOrEmpty(q) ? storeDB.Products
                     : storeDB.Products.Where(p => p.Title.Contains(q));

    return View(result);
}

It's possible that the SQL dialect supported by SQL CE doesn't support the check for emptiness that you were using - and this gets round that problem.

Upvotes: 2

Related Questions