Solved

Convert Generic list to data

Posted on 2013-01-08
13
455 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 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

726 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