Fill DropBox with XML data

0

I have a file in XML to be able to fill several data of a drop down, but I do not know if it is not deserving correctly or I am wrong in my code because when I execute it, it does not show any value inside the DropDown

Here is where I create the list and the name of the file

public List<Data.Users> UserList()
        {
            string myConnectionString = @"C:\Users\gutiece\Desktop\" + "tool_track.accdb";

            List<Data.Users> UserList = new List<Data.Users>();

            OleDbConnection connection = new OleDbConnection();
            OleDbCommand command = new OleDbCommand();
            OleDbDataAdapter adapter = new OleDbDataAdapter();

            try
            {
                connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
                bool ok = System.IO.File.Exists(myConnectionString);
                String qry = "SELECT * FROM users AS Users";
                command.Connection = connection;
                command.CommandText = qry;

                adapter.SelectCommand = command;

                command.Connection.Open();
                OleDbDataReader reader = command.ExecuteReader(); // close conn after 

                while (reader.Read())
                {
                    UserList.Add(new Data.Users(
                        reader["WHO"].ToString(),
                        Convert.ToInt32(reader["PAYROLL_NUMBER"].ToString()),
                        reader["NAME"].ToString(),
                        reader["OU"].ToString(),
                        Convert.ToInt32(reader["POOL"].ToString()),
                        Convert.ToInt32(reader["TEAM"].ToString()),
                        reader["PASS"].ToString(),
                        reader["ROL"].ToString()));
                }

            //Save Lists in temp folder
            XML.ToXML XMLs = new XML.ToXML();
            XMLs.SerializeXMLUsers(UserList, "MyListOfUsers");

            List<Data.Users> AllShort = XMLs.BuilDeSerializingUsers("MyListOfUsers");

            String Model = AllShort[0].NAME;

                if (!reader.IsClosed)
                {
                    reader.Close();
                }

                return UserList;
            }
            catch (Exception)
            {
            }
            return UserList;
        }

This is the serialization and deserealization of the XML

public void SerializeXMLUsers<T>(List<T> UserList, String FileName)
        {
            String imgPath = HttpContext.Current.Request.PhysicalApplicationPath + @"\FilesXML\" + FileName + ".xml";
            String ImageFolderPath = HttpContext.Current.Request.PhysicalApplicationPath + @"\FilesXML\";

            try
            {
                Type[] elements = { typeof(List<T>) };
                XmlSerializer serializer = new XmlSerializer(typeof(List<T>), elements);
                FileStream fs = new FileStream(imgPath, FileMode.Create);
                serializer.Serialize(fs, UserList);
                fs.Close();
                UserList = null;
            }
            catch (Exception)
            {
            }
        }
public List<Data.Users> BuilDeSerializingUsers(String FileName)
        {
            String imgPath = HttpContext.Current.Request.PhysicalApplicationPath + @"\FilesXML\" + FileName + ".xml";
            String ImageFolder = HttpContext.Current.Request.PhysicalApplicationPath + @"\FilesXML";

            List<Data.Users> UserList = null;

            XmlSerializer serializer = new XmlSerializer(typeof(List<Data.Users>));

            StreamReader reader = new StreamReader(imgPath);
            UserList = (List<Data.Users>)serializer.Deserialize(reader);
            reader.Close();

            return UserList;
        }

and this is the part where I want to fill DropDown :

private void BindDpoTlXml()
        {  
            String filePath = Server.MapPath("/FilesXML/MyListOfUsers.xml");
            using (DataSet ds = new DataSet())

            if (ds.Tables.Count > 0)
            {

                {
                    ds.ReadXml(filePath);

                    Dpo_tl.DataSource = ds;
                    Dpo_tl.DataTextField = "NAME";
                    Dpo_tl.DataValueField = "Select a Team Leader";
                    Dpo_tl.DataBind();
                }
            }
        }

Files are created correctly in the assigned folder but for some reason the files are not read correctly.

I hope you can help me

    
asked by Cesar Gutierrez Davalos 29.06.2017 в 18:08
source

1 answer

0

The solution was easy, in case someone at some point is presented with this problem ... here I leave the solution:)

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDpoTlXml();
            }
        }

private void BindDpoTlXml()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("/FilesXML/MyListOfUsers.xml"));
            DataView dv = ds.Tables[0].DefaultView;

            dv.Sort = "NAME";

            if (ds.Tables.Count > 0)
            {              
                {
                    Dpo_tl.DataSource = ds;
                    Dpo_tl.DataTextField = "NAME";
                    //Dpo_tl.DataValueField = "Select a Team Leader";
                    Dpo_tl.DataBind();
                }
            }
        }
    
answered by 29.06.2017 / 20:37
source