Click here to Skip to main content
14,602,124 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Team

I am trying to get my list of countries using dropdownlistFor, each time when my application loads it throws this exception and need some help to resolve it, yet its not saving to my database.

What I have tried:

<pre>private List<RegistrationTrainingForm> LoadData()
        {
            List<RegistrationTrainingForm> lst = new List<RegistrationTrainingForm>();

            try
            {
                string line = string.Empty;
                string srcFilePath = "Content/files/country_list.txt";
                var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                var fullPath = Path.Combine(rootPath, srcFilePath);
                string filePath = new Uri(fullPath).LocalPath;
                StreamReader src = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read));

                // while to read the file
                while ((line = src.ReadLine()) != null)
                {
                    RegistrationTrainingForm infoLst = new RegistrationTrainingForm();
                    string[] info = line.Split(',');

                    //Setting
                    infoLst.Country_Id = Convert.ToInt32(info[0].ToString());
                    infoLst.Country_Name = info[1].ToString();

                    lst.Add(infoLst);
                }
                src.Dispose();
                src.Close();
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            return lst;
        }


        // List for countries.
        private IEnumerable<SelectListItem> GetCountryList()
        {
            SelectList listcn = null;
            try
            {
                var list = this.LoadData().Select(p => new SelectListItem
                {
                    Value = p.Country_Id.ToString(),
                    Text = p.Country_Name
                });
                listcn = new SelectList(list, "Value", "Text");

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return listcn;
        }


@using (Html.BeginForm("Index", "Home", FormMethod.Post))
                               {
                                   <div class="col-sm-3">
                                       @Html.DropDownListFor(model=>model.ListCountries.Country, ViewBag.CountryList as SelectList)
                                   </div>
                               }
Posted
Updated 5 days ago
Comments
Gerry Schmitz 31-Jul-20 13:19pm
   
Time to make some permanent POCO's instead of making up names on the fly like "Value" and "Text".

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

Seems, nothing wrong at first glance. However, I saw something that might be causing the exception.

You can change your code starting with the stream using the using-statement.

See the code sample below.

using (StreamReader src = new StreamReader (new FileStream (filePath, FileMode.Open, FileAccess.Read)) {

            // while to read the file
            while ((line = src.ReadLine ()) != null) {
                RegistrationTrainingForm infoLst = new RegistrationTrainingForm ();
                string[] info = line.Split (',');

                //Setting
                infoLst.Country_Id = Convert.ToInt32 (info[0].ToString ());
                infoLst.Country_Name = info[1].ToString ();

                lst.Add (infoLst);
            }

            src.Close ();
            src.Dispose ();
        }


Hopefully, you can start with that code above and hope this helps.

Cheers,
Jin
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100