SqlDataReader Returned Values RRS feed

  • Question

  • I have a class that defines the following items. 

    public int Tid { get; set; }
    public string TDesc { get; set; }
    public string TNotes { get; set; }
    public DateTime TDate { get; set; }
    public int TPercent { get; set; }
    while (reader.Read())
           var tasklist = new Task();
    //tasklist.Tid = reader.GetInt32(0);
    tasklist.TDesc  = reader.GetString(1);
    tasklist.TNotes = reader.GetString(2);
    tasklist.TDate = reader.GetDateTime(3);
    tasklist.TPercent = reader.GetInt32(4);

    Whenever the sqldatareader is ran and pulls back the data to be read and added to the 'var tasklist', is there a way to take tasklist.TDesc and have it be added as tasklist.Description so that the header of the data indicates Description instead of TDesc.

    I know this I a little strangely worded.

    • Edited by rlawrimore Tuesday, June 19, 2018 1:57 PM
    Tuesday, June 19, 2018 1:56 PM

All replies

  • Hello,

    you could allow only a set to TDESC and make a new property with only a get that returns the value of TDESC.

    Tuesday, June 19, 2018 3:05 PM
  • >>I know this I a little strangely worded<<

    Yes, it is   ;)  I'm not sure what you're asking, to be honest.  Does your Task class have a Description property? If so (and, if I understand the question), then you could do this:

    tasklist.TDesc  = reader.GetString(1);
    tasklist.Description = tasklist.TDesc;
    If your Task class doesn't have a Description property, then add one.

    ~~Bonnie DeWitt [C# MVP]

    Tuesday, June 19, 2018 3:28 PM
  • Maybe you need this:

       [DisplayName( "Description" )]

       public string TDesc { get; set; }


    Tuesday, June 19, 2018 9:34 PM
  • It seems like you're trying to put some sort of association between the property name and the column name. There isn't any. You can use any property name in combination with any column name/ordinal. Since your reader code is doing the translation it would be appropriate to "rename" the column at this point.

    public string Description { get; set; }
    tasklist.Description = reader.GetString(1);
    Note that if you already have TDesc in use in your code then use Find All References to see where else it is used. If none of those places rely on the name being TDesc then right click the property definition and rename it and the IDE will auto-rename it everywhere.

    Michael Taylor

    Wednesday, June 20, 2018 1:55 PM