Note that there are some explanatory texts on larger screens.

plurals
  1. POput values from Inner join in object that is in an object
    primarykey
    data
    text
    <p>I have the following sql command with an Inner join:</p> <pre><code>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(); </code></pre> <p>I want to put the values from <em>a</em> (News_accounts) in the object <em>account</em> that is in itself in the object <em>newsComment</em> which is located in a generic List, <em>List newsComments</em>. I do that like this:</p> <pre><code>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; } </code></pre> <p>Comments has a constructor:</p> <pre><code>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; } </code></pre> <p>And Accounts account as well:</p> <pre><code>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; } </code></pre> <p>Up until rating all goes well and the values are being put in the <em>newsComment</em> object, however, when it reaches the values that need to be put in the object <em>account</em> that is located in the object <em>newsComment</em>, 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. </p> <p>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. </p> <p>please help me :)</p> <p>greetings</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload