Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert Generic list to data

Posted on 2013-01-08
13
Medium Priority
?
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 45

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 45

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 45

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

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Screencast - Getting to Know the Pipeline

610 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