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 8817691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:54:41+00:00 2026-06-14T04:54:41+00:00

I am working in asp mvc 3 app. I have a model/entity called History.

  • 0

I am working in asp mvc 3 app. I have a model/entity called History. I have a linq query that returns one value. Depending on what I do, I either get a “Object not set to an instance” error in the controller when the method is called, or I get a “cannot implicitly convert from string to type Models.History.” So I am looking for assistance in resolving, do I just need to cast it or something?

Here is the method that gives the ‘object not set’ error:

public string GetPastAbuseData(int Id)
{

  var query = (from h in _DB.History
              where h.ApplicantId.Equals(Id)
              select h.AbuseComment).FirstOrDefault();

  return query.ToString();
 }

Controller:
vm.HistoryModel.AbuseComment = repo.GetPastAbuseData(Id);

And if I change the method type from string to History I get the ‘cannot convert’ error:

public History GetPastAbuseData(int Id)
{
    return (from h in _DB.History
            where h.ApplicantId.Equals(Id)
            select h.AbuseComment).SingleOrDefault();
}

Thank you for your time.

  • 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-14T04:54:43+00:00Added an answer on June 14, 2026 at 4:54 am

    You are selecting AbuseComment property (which is string) from HistoryObject. Thus your code tries to convert string to History. Just return whole History entity:

    public History GetPastAbuseData(int Id)
    {
        return (from h in _DB.History
                where h.ApplicantId.Equals(Id)
                select h).SingleOrDefault();
    }
    

    Also in first case query will be of string type. You don’t need to call ToString on this variable. Even more, when you fall into OrDefault() case, you will have NullReferenceException.

    public string GetPastAbuseData(int Id)
    {
      return (from h in _DB.History
              where h.ApplicantId.Equals(Id)
              select h.AbuseComment).FirstOrDefault();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an ASP.NET MVC App that has buttons that have the server
I have an asp.net MVC app that i am working on and I wrote
I have an app, modelled on the one from Apress Pro ASP.NET MVC that
I am working on an existing ASP.NET MVC app that started small and has
I'm trying to get an ASP.NET MVC app working... I should have known it
I have TinyMCE working great in my ASP.NET MVC app except for the spellchecker.
I'm working on a model for an ASP.NET MVC app with DI and an
Background: I am working on an ASP.NET MVC app that has 3 partials (based
I have an ASP.NET MVC 3 app. It is working fine in both IE8
I'm working on an ASP.NET MVC app (using MVC3 RC2). Say I have 2

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.