Click here to Skip to main content
15,887,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
<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 31-Jul-20 22:29pm
Comments
[no name] 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

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.

C#
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900