Solved

Convert Generic list to data

Posted on 2013-01-08
13
451 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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