Solved

Convert Generic list to data

Posted on 2013-01-08
13
445 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

23 Experts available now in Live!

Get 1:1 Help Now