Hello,
I have a WebService that gets a LessonID and returns a list<string> that has all the links of the lesson from access database:

[WebMethod]
        public List<string> Lessons(int lessonID)
        {
            string lessonsource = "";
            string cs = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source= " + GetDBLocation();
            OleDbConnection cl = new OleDbConnection(cs);
            string ssql = "SELECT Lesson.Lesson_ID, Lesson.Lesson_Source FROM Lesson WHERE (((Lesson.Lesson_ID)=" + lessonID + "));";
            OleDbCommand myCmd = new OleDbCommand(ssql, cl);
            OleDbDataReader dr = null;
            try
            {
                cl.Open();
                dr = myCmd.ExecuteReader();
                while (dr.Read())
                {
                    lessonsource = dr["Lesson_Source"].ToString();
//Lesson_Source has the path to the .txt file
                }
            }
            catch (Exception err)
            {

            }
            finally
            {
                dr.Close();
                cl.Close();
            }

            StreamReader reader = new StreamReader(lessonsource);
            string line = "";
            List<string> list = new List<string>();
            while ((line = reader.ReadLine()) != null)
                list.Add(line);
            reader.Close();
            return list;
        }

I use it this way:

List<string> list = new List<string>();
            Web_Services.Service1 ws = new TeoryWebSite.Web_Services.Service1();
            list = ws.Lessons(lessonID);

I get an error on "ws.Lessons(lessonID)":
Cannot implicitly convert type 'string[]' to 'System.Collections.Generic.List<string>'

Why is that happening?
Thanks.

Recommended Answers

All 2 Replies

List<string> as implemented is a .NET 2.0+ construct, and your web service could potentially be consumed by any number of clients. Being that the generic list is a wrapper around a backing array, the service will expose the method or property as returning an array of strings (string[]) rather than a list. But it is trivial to work with it within your client code as a list, if that is what you prefer.

// using the List<T> constuctor that takes an IEnumerable<T> argument
List<string> list = new List<string>(ws.Lessons(lessonID));

// or calling ToList() on an array (using System.Linq;)
List<string> list = ws.Lessons(lessonID).ToList();

See: http://msdn.microsoft.com/en-us/library/aa480010.aspx#toolsupport_topic3

commented: This solved my problem, Thanks. +1

Thanks for the reply,
That solved the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.