Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 173547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:21:47+00:00 2026-05-11T13:21:47+00:00

I am trying write a SQL query that filters a gridview by the fields

  • 0

I am trying write a SQL query that filters a gridview by the fields that are entered. There are four fields, title, firstname, surname and Company.Name.

The first three are fine as they are never null but the fourth can be null. The following LINQ Query works just fine:

var listofclients = from client in allcients                     where client.Title.ToLower().Contains(titletxtbox.Text.Trim().ToLower())                     where client.Firstname.ToLower().Contains(firstnametxtbox.Text.Trim().ToLower())                     where client.Surname.ToLower().Contains(surnametxtbox.Text.Trim().ToLower())                     orderby client.Name 

But when I try and put a filter into it for the company I will get an error at runtime when the company is null

var listofclients = from client in allcients                     where client.Title.ToLower().Contains(titletxtbox.Text.Trim().ToLower())                     where client.Firstname.ToLower().Contains(firstnametxtbox.Text.Trim().ToLower())                     where client.Surname.ToLower().Contains(surnametxtbox.Text.Trim().ToLower())                     where client.Company.Name.ToLower().Contains(companynametxtbox.Text.Trim().ToLower())                     orderby client.Name 

What I would like to know, is there a way to build the query so that it will only filter when the client.Company field is not null.

Also am I vulnerable to SQL injection or the like when I pull directly from the textbox fields like this. I know in this case it is not connected to the DB but if it was could they do a drop. Or even if it is not connected to the db could they fiddle with the objects in the list?

Thanks

Jon Hawkins

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:21:48+00:00Added an answer on May 11, 2026 at 1:21 pm

    I’m assuming you want all matching records where Company is null but filtered by name when the Company exists. The following should do that. Also, you needn’t worry about SQL injection as LINQToSQL uses parameterized queries. You will have to worry about cleaning up any HTML that may be in the client controls if you intend to do inserts from them and display any of the values on the web to avoid XSS attacks.

    var listofclients = from client in allcients                     where client.Title.ToLower().Contains(titletxtbox.Text.Trim().ToLower())                     where client.Firstname.ToLower().Contains(firstnametxtbox.Text.Trim().ToLower())                     where client.Surname.ToLower().Contains(surnametxtbox.Text.Trim().ToLower())                     where client.Company == null || client.Company.Name.ToLower().Contains(companynametxtbox.Text.Trim().ToLower())                     orderby client.Name 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You cannot use namespacing to great effect with frameworks that… May 12, 2026 at 12:32 pm
  • Editorial Team
    Editorial Team added an answer I faced the same question before, and "Process management" answered… May 12, 2026 at 12:32 pm
  • Editorial Team
    Editorial Team added an answer In C++ nothing prevent an inline function to have a… May 12, 2026 at 12:32 pm

Related Questions

I am implementing a small queue to handle which process gets to run first.
I am trying to write a query for SQL Server 2005 but I can't
The schema: (psuedocode) I have a bean, called BaseEntity... @Entity class BaseEntity { @OneToMany
In this situation I am trying to perform a data import from an XML

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.