Solved

Convert Generic list to data

Posted on 2013-01-08
13
447 Views
Last Modified: 2013-01-08
I am getting an error when I try to use a data class to retrieve data that I store in a list

it says Cannot implicitly convert type 'System.Collections.Generic.List<MessageBoard.Data.MessageData>' to 'MessageBoard.Data.MessageData

Data.Message m = new Data.Message();
                Data.MessageData md = m.GetEditThread(ID);
                //List<Data.MessageData> md = new List<Data.MessageData>();
                //List<Data.MessageData> md = m.GetEditThread(ID);
                lblPostID.Text = md.id;
                radtxtBody = md.Body;
                radtxtSubject = md.Subject;

Open in new window

I have my sql statement and my reader to add each item to the stubs in Message and my stubs in message data

This is message data
    public class MessageData
    {
         public MessageData()
        {
 
        }
        public int id { get; set; }
        public string Subject { get; set; }
        public string Body { get; set;}
        public int File_Id {get; set;}
        public bool ForPublic {get;set;}
        public bool ForPrivate {get;set;}
        public bool ForKKOnly {get;set;}
        public int UserID {get;set;}
        public int PriorityID {get;set;}
    }

Open in new window

This is Message
    class Message
    {
        public List<MessageData> GetEditThread(int ID)
        {
            List<MessageData> list = new List<MessageData>();

            
            string sql;
            sql = "Select id,Subject, Body,File_Id,ForPublic,ForKKOnly, ForPrivate,UserID,PriorityID FROM MsgPost Where id = '" + ID + "'";


            using (SqlConnection conn = new SqlConnection(DBConnectionString.Get(Database.Task)))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = System.Data.CommandType.Text;

                conn.Open();

                SqlDataReaderHelper reader = new SqlDataReaderHelper(cmd.ExecuteReader());
                if (reader.IsValid)
                {
                    while (reader.Read())
                    {
                        list.Add(new MessageData()
                        {
                            ID = reader.GetInt("ID", 0),
                            Subject = reader.GetString("Subject", ""),
                            Body = reader.GetString("Body", ""),
                            File_Id = reader.GetInt("File_Id", 0),
                            ForPublic = reader.GetBool("ForPublic", false),
                            ForPrivate = reader.GetBool("ForPrivate", false),
                            ForKKOnly = reader.GetBool("ForKKOnly", false),
                            UserID = reader.GetInt("UserID", 0),
                            PriorityID = reader.GetInt("PriorityID", 0),
                        });
                    }
                }
                conn.Close();
            }
            return list;
        }

    
    }

Open in new window

0
Comment
Question by:r3nder
  • 5
  • 4
  • 4
13 Comments
 
LVL 8

Expert Comment

by:Crashman
ID: 38755684
try

List<Data.MessageData> md = new List<Data.MessageData>();
md.AddRange = m.GetEditThread(ID);

List<Data.MessageData> md = new List<Data.MessageData>(m.GetEditThread(ID));

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38755692
A list contains none, one or many individual items.
It is like a shelf with numbers of books on it - you don't read the shelf, you take one book away and read that.

You take an item out of the list and process that item.
0
 
LVL 8

Expert Comment

by:Crashman
ID: 38755711
my mistake;

md.AddRange(m.GetEditThread(ID));
//or 
List<Data.MessageData> md = new List<Data.MessageData>(m.GetEditThread(ID));

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 38755824
does not contain a definition for subject
                Data.Message m = new Data.Message();
                List<Data.MessageData> md = new List<Data.MessageData>(m.GetEditThread(ID)); 
                lblPostID.Text = Convert.ToString(md.id);
                radtxtBody.Text = md.Body;
                radtxtSubject.Text = md.Subject;

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38755840
?
What has that got to do with your original error when you attempted to cast a list of items to an individual item
0
 
LVL 8

Expert Comment

by:Crashman
ID: 38755850
you are using a List, you must use items, not the LIST in self

Data.Message m = new Data.Message();
List<Data.MessageData> md = new List<Data.MessageData>(m.GetEditThread(ID)); 
if (md.Count > 0)
{
lblPostID.Text = Convert.ToString(md[0].id);
radtxtBody.Text = md[0].Body;
radtxtSubject.Text = md[0].Subject;
}

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 6

Author Comment

by:r3nder
ID: 38755873
that means the suggested solution didnt work - I was doing that all day long - and to be honest Andy I dont need analogies or a life lesson in coding or someone with the mentality that they are a guru - II come here for actual help - Crashman was kiind enough to offer - you are not
IMHO
R
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38755887
Before you criticise you should read my comment.
I told you EXACTLY why your original code failed AND what to do to correct it.
0
 
LVL 8

Accepted Solution

by:
Crashman earned 500 total points
ID: 38755905
Your first problem was this...
it says Cannot implicitly convert type 'System.Collections.Generic.List<MessageBoard.Data.MessageData>' to 'MessageBoard.Data.MessageData
The second problem is different, because, one thing is how fill, and another is problem using the object in self. in this case, the items.

So, for fill list you can use:
List.AddRange(otherList) 
//or in the instance
List<x> listX = new List<x>(otherList);

Open in new window

then you have a List of Items, you must specify the item you need, for that, you must refer index, or in another words, the position in the list

listX[0] 
listX[1]

Open in new window

etc.
0
 
LVL 6

Author Comment

by:r3nder
ID: 38755922
@Andy
no you told me about a bookshelf
@Crashman
Worked perfect - now I realize its alot like a dataset - thank you
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 38755934
Thank you Crashman - with a little practice I should be able to get this down :)
0
 
LVL 8

Expert Comment

by:Crashman
ID: 38755942
Great,
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38755979
A list contains none, one or many individual items.
...
You take an item out of the list and process that item.


see the comment you selected as an answer as you did not understand that.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Host asp.net pages 5 26
Replace &lt; with < 14 57
C# HTTP GET method sample code 3 42
Wav problem 4 19
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now