0

I'm trying to recieve data from an database table and my sql query also selects a column from another table that i would like to return aswell

          while (dr.Read())
          {
              Inlagg comentsar = new Inlagg
              {
                  names = (int)dr["name"],
                  dates = (DateTime)dr["Date"]
 //get column value from the other table not the Inlagg table here pobbisble?

              };
       //This gets the column value from the other table but i dont know how to return it
              string test = (string)dr["test"];
                  comen.Add(comentsar);

          }
          return comen;

How can i return a result that includes columns both columns from different tables? Side note: I have a column that i dont need to recieve here, altough the column is an int and i'm trying to recieve an string, is there any way to cast the column to an string? Any help or input highly appreciated, thanks!

EDIT: "Solved" it by simply creating a new class with the values i wanted which included columnvalues from different tables

4
  • 1
    "Unable to return it" how? Compile error? Exception? Commented Apr 25, 2015 at 13:57
  • What do you mean by "im unable to return it" in the comments? You can access to column values using the index operator or GetXXX methods of the returned IDataRecord (as you did) regardless of the fact that which table the column is belong to. Commented Apr 25, 2015 at 13:59
  • I'm sorry i'm going to edit "unable" to: dont know how to return it Commented Apr 25, 2015 at 14:00
  • Why don't you add another property to the Inlagg class? The best way to return that value depends on your design. Commented Apr 25, 2015 at 14:03

2 Answers 2

1

Inside of your reader, your missing the following:

if(reader["Column"] != DBNull.Value)
     model.Name = reader["Column"].ToString();

Your query for your SQL should correctly join the multiple tables, then in the returned result set you would have a column name to return by. You call the reader["..."] to access the value for said column.

As you notice the model class calls a name of a property.

The model is important as it represents your data, but to access through the reader you simply add another property.

That is because the column is null, so you can't cast a ToString() that is why the error occurs. You could attempt to also do:

int? example = reader["Column"] as int?;
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this but i get the following error "Cannot implicitly convert type 'string' to 'int?'" because the column is an int. The thing is that i would really like to avoid extending the class since it destroys when trying to call the class within another method, but maybe its the only way? unless its possible to convert the the column from int to string?
0

Extend your class:

public class Inlagg{
   ...
   addMyString(String myString){
      this.myString = myString;
   }
   getMyString(){
      return this.myString;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.