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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:01:17+00:00 2026-05-26T23:01:17+00:00

I have the following sql command with an Inner join: SqlCommand cmd = new

  • 0

I have the following sql command with an Inner join:

SqlCommand cmd = new SqlCommand(@"SELECT c.comment_Id, c.date, c.comment, c.rating, a.registration_Date, a.username, a.email, a.profile_Image FROM News_comments c INNER JOIN News_accounts a ON c.account_Id=a.account_Id WHERE c.news_Id = @news_Id", conn);
cmd.Parameters.Add("news_Id", SqlDbType.Int).Value = news_Id;
conn.Open();

I want to put the values from a (News_accounts) in the object account that is in itself in the object newsComment which is located in a generic List, List newsComments.
I do that like this:

using (SqlDataReader reader = cmd.ExecuteReader()) {                
    while (reader.Read()) {
        Comments newsComment = new Comments();
        newsComment.comment_Id = int.Parse(reader["comment_Id"].ToString());
        newsComment.date = DateTime.Parse(reader["date"].ToString());
        newsComment.comment = reader["comment"].ToString();
        newsComment.rating = int.Parse(reader["rating"].ToString());
        newsComment.account.Registration_Date = DateTime.Parse(reader["registration_Date"].ToString());
        newsComment.account.Username = reader["username"].ToString();
        newsComment.account.Email = reader["email"].ToString();
        newsComment.account.Profile_Image = reader["profile_Image"].ToString();
        newsComments.Add(newsComment);
    }
    return newsComments;
}

Comments has a constructor:

public Comments(int comment_Id, DateTime date, string comment, int rating, Accounts account) {
    this.comment_Id = comment_Id;
    this.date = date;
    this.comment = comment;
    this.rating = rating;
    this.account = account;
}

And Accounts account as well:

public Accounts(int account_Id, DateTime registration_Date, string email, string username, string profile_Image, string homepage, string about, bool admin) {
    this.account_Id = account_Id;
    this.registration_Date = registration_Date;
    this.email = email;
    this.profile_Image = profile_Image;
    this.homepage = homepage;
    this.about = about;
    this.admin = admin;
}

Up until rating all goes well and the values are being put in the newsComment object, however, when it reaches the values that need to be put in the object account that is located in the object newsComment, it gives a NullReferenceException.
I know this means that it doesn’t have a value but I can’t seem to find why it has no value.

I’ve looked checked my Inner join with sql server 2008 query designer, and that works
so it’s got to be the object but I don’t see the problem.

please help me 🙂

greetings

  • 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-05-26T23:01:18+00:00Added an answer on May 26, 2026 at 11:01 pm

    newsComment.account You have to initialize that object before accessing its fields, properties, or methods. Something like this:

    newsComment.account = new Account();
    newsComment.account.Registration_Date = DateTime.Parse(reader["registration_Date"].ToString());
    newsComment.account.Username = reader["username"].ToString();
    newsComment.account.Email = reader["email"].ToString();
    newsComment.account.Profile_Image = reader["profile_Image"].ToString();
    

    … or you can do it from the constructor of the Comments class, the one which takes no arguments.

    As a side note: maybe you should consider using an ORM, like LINQ to SQL or Entity Framework. It’s what they do.

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

Sidebar

Related Questions

I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have got a SQL command like the following : SELECT * FROM temp1
I have the following scenario; Dim cmd as New SQLCommand cmd.Connection = myopenconnection cmd.CommandText
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have the following SQL Statement. I need to select the latest record for
I have the following SQL statement: Select Choose(1,Orders.Employee, Orders.Customer) as Name1, Choose(2,Orders.Employee, Orders.Customer) as
I have the following SQL table - Date StoreNo Sales 23/4 34 4323.00 23/4
I have the following sql command through code and because the parameter contains a
Say I have the following Linq 2 SQL command; ItemsRepository.All().Where(r => r.type == myType);

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.