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

  • SEARCH
  • Home
  • 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 8454809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:07:27+00:00 2026-06-10T12:07:27+00:00

I already use linq but search id only and it worked perfectly var obj

  • 0

I already use linq but search id only and it worked perfectly

var obj = (from VAR in db.infos
           where VAR.id == 22                                                                                
           select new
           {
               title = VAR.title,
           }).SingleOrDefault();
Label2.Text = obj.title.Trim();

If I try to search by location get a error

var obj = (from VAR in db.infos
           where VAR.location.Trim() == "Lim"
           select new
           {
               title = VAR.title.Trim(),
           }).SingleOrDefault();

        SearchJob.Items.Add(obj.title.Trim());
        Label2.Text = obj.title;

Have a error in label2 line

Object reference not set to an instance of an object.

How do I fix it?

if (obj.title != null)
{
    SearchJob.Items.Add(obj.title.Trim());
    Label2.Text = obj.title;
}

Object reference not set to an instance of an object.

SOLUTION

Change SingleOrDefault() to FirstOrDefault()

  • 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. Editorial Team
    Editorial Team
    2026-06-10T12:07:28+00:00Added an answer on June 10, 2026 at 12:07 pm

    You’re doing some nasty stuff there, VERY bad habits. For instance this:

    var obj = (from VAR in db.infos
               where VAR.location.Trim() == "Lim"
               select new
               {
                    title = VAR.title.Trim(),
    
               }).SingleOrDefault();
    
    SearchJob.Items.Add(obj.title.Trim());
    Label2.Text = obj.title;
    

    Is a nonsense! Let me tell you why:

    Always check the data BEFORE you insert it into your database, not AFTER. You’re creating a lot of unnecessary overhead this way, which could be avoided altogether. Trim the data before, never after.

    Next thing – you need only a single string value, yet you create an anonymous object. WHY? Do this instead:

    string title = (from o in db.infos
                    where o.location == "Lim"
                    select o.title).SingleOrDefault(); 
    

    Use SingleOrDefault if you expect a single result or none. However, if you expect multiple results and want only the first, use FirstOrDefault.

    As you can see, I’m using o instead of VAR. It’s true it doesn’t really matter that much, BUT, it’s never a good idea to use a word that’s very similar to one of the reserved words (var).

    If you get an exception Object reference not set to an instance of an object., it means that your query returned a null and you’re trying to access a non-existing object. If your query may return null at some point, always check for null when accessing a member!

    EDIT

    if ( obj.title != null )
    

    is bad too, because you need to check for null the object itself!

    if (obj != null)
    

    if you really want to use your bad approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already know that most implementations use a single thread, but is there anything
Possible Duplicate: Use LINQ to read all nodes from XML I am trying to
I wish to know which are the Eclipse navigation keyboard shortcuts. I already use:
Is anybody using OpenGLES2.0 shaders (GLSL) successfully for audio synthesis? I already use vDSP
I am a Symfony developer and my web server is Linux. I already use
I am looking to implement a grid system. I already use jQuery, jQuery UI
I am developing a web database that is already in use for about a
Is there a GWT Date Time Component? Note: I am already making use of
Is there anyone who has already tried to use the Microsoft Bing translator web
I use a CursorLoader in a LoaderManager with a Custom CursorAdapter. I've already achieved

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.