Link to home
Start Free TrialLog in
Avatar of Jeff Heilman
Jeff HeilmanFlag for United States of America

asked on

C# List conversion error Cannot convert from System.Collections.Generic.List to class

I'm hoping someone can help me understand what's going on here and what I have to do to correct it.  I'm trying to upload a list of scanned data to a web service and keep getting the error: Argument 1: cannot convert from 'System.Collections.Generic.List<ProfilesScan.WSTori.CountDetails>' to 'ProfilesScan.WSTori.CountDetails[]  I've tried converting the list to an Array to no avail.  Here is the code:
if (NetworkCheck.IsInternet())
                    {
                        string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "LocalData.db3");
                        var tablelistout = new SQLiteConnection(path);
                        var list = tablelistout.Table<EOMCountsheet>();
                        var data = new List<string>();

                        List<CountDetails> invCountList = new  List<CountDetails>();
                        foreach (var listing in list)
                        {
                            invCountList.Add(new CountDetails
                            {
                                InventoryCountSpecified = true,
                                InventoryCount = listing.InventoryCount,  
                                Location = listing.Location,
                                Part = listing.Part,
                                UniqueStamp = listing.UniqueStamp,
                                UserID = listing.UserID,
                            });
                        }

                       List<CountDetails> countInfo = invCountList;
                        WSConnect.InsertCountDetails(countInfo);
                    }

Open in new window

EOMCountsheet class:
 public class EOMCountsheet
    {        
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string Location { get; set; }
        public int InventoryCount { get; set; }
        public string Part { get; set; }
        public string UserID { get; set; }
        public string UniqueStamp { get; set; }
    }  

Open in new window

Code from my web service:
[OperationContract]
         string InsertCountDetails(List<CountDetails> countInfo);

[DataContract]
    public class CountDetails
    {
        string location = "";
        int inventorycount = 0;
        string part = "";
        string userid = "";
        string uniquestamp = "";
        [DataMember]
        public string Location
        {
            get { return location; }
            set { location = value; }
        }
        [DataMember]
        public int InventoryCount
        {
            get { return inventorycount; }
            set { inventorycount = value; }
        }
        [DataMember]
        public string Part
        {
            get { return part; }
            set { part = value; }
        }
        [DataMember]
        public string UserID
        {
            get { return userid; }
            set { userid = value; }
        }
        [DataMember] public int? ID { get; set; }
        [DataMember]
        public string UniqueStamp
        {
            get { return uniquestamp; }
            set { uniquestamp = value; }
        }
    }

public string InsertCountDetails(List<CountDetails> countInfo)
        {
            var connString = ConfigurationManager.ConnectionStrings["ToriConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(connString);
            con.Open();

            var cmd = new SqlCommand(
                    "INSERT INTO tblCountSheet(Location, InventoryCount, Part, UserID, UniqueStamp) " +
                    "VALUES(@Location, @InventoryCount, @Part, @UserID, @UniqueStamp)", con);
                foreach (CountDetails det in countInfo)
                {
                    cmd.Parameters.AddWithValue("@Location", det.Location);
                    cmd.Parameters.AddWithValue("@InventoryCount", det.InventoryCount);
                    cmd.Parameters.AddWithValue("@Part", det.Part);
                    cmd.Parameters.AddWithValue("@UserID", det.UserID);
                    cmd.Parameters.AddWithValue("@UniqueStamp", det.UniqueStamp);
                    con.Open();
                    int result = cmd.ExecuteNonQuery();

                    //if (result == 1)
                    //{
                    //    Message = "Success";
                    //}
                    //else
                    //{
                    //    Message = "Error";
                    //}
                }
            con.Close();
            
            return countInfo.ToString();
        }

Open in new window

Avatar of it_saige
it_saige
Flag of United States of America image

You need to convert your list to an array:
if (NetworkCheck.IsInternet())
{
    string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "LocalData.db3");
    var tablelistout = new SQLiteConnection(path);
    var list = tablelistout.Table<EOMCountsheet>();
    var data = new List<string>();

    List<CountDetails> invCountList = new  List<CountDetails>();
    foreach (var listing in list)
    {
        invCountList.Add(new CountDetails
        {
            InventoryCountSpecified = true,
            InventoryCount = listing.InventoryCount,  
            Location = listing.Location,
            Part = listing.Part,
            UniqueStamp = listing.UniqueStamp,
            UserID = listing.UserID,
            });
    }

    WSConnect.InsertCountDetails(invCountList.ToArray());
}

Open in new window

-saige-
Avatar of Jeff Heilman

ASKER

Hi it_saige, thank you for your comment.  I tried that again just for good measure and I still get the same error, Error      CS1503      Argument 1: cannot convert from 'System.Collections.Generic.List<ProfilesScan.WSTori.CountDetails>' to 'ProfilesScan.WSTori.CountDetails[]'.  This has me pretty baffled.
Where do you get that error message? What line?

-saige-
The error is here
public static string InsertCountDetails(List<CountDetails> countInfo)
        {
            return _wsTori.InsertCountDetails(countInfo); <<<<<countInfo is underlined in red.
        }

Open in new window

Is that referencing the one in your web service code block above?

-saige-
Yes it is.
public static class WSConnect
    {
        private static WSTori.WSToriService _wsTori = new WSTori.WSToriService();

        //#region Web service Calls

        public static string InsertCountDetails(List<CountDetails> countInfo)
        {
            return _wsTori.InsertCountDetails(countInfo);
        }
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you it_saige!  It went through.  Now to tackle the next problem, Unhandled Exception:  System.Web.Services.Protocols.SoapException: The connection was not closed. The connection's current state is open.

I appreciate your help, have a great weekend!
JeffH