darma teja

darma teja

  • NA
  • 496
  • 336.3k

how to write code if we don't have the values in the txt file

Oct 17 2012 4:28 AM
Hi, 

I developed an application, in which it takes the values from the database and put into the listbox. and the next one is i take values from the list box, search it in the txt file and show the concerned values in the list box.

My problem is if i dont find the one value in txt file, my application is going crash down.
i am getting error like this:
dict has null value
dict has no match values

I should skip these two conditions.


Here i wrote database connetion code

                l
                while (targetrdr.Read())
                {
                    lbandari.Items.Add(targetrdr[0]);
                }
                targetrdr.Close();

                string[] lines = System.IO.File.ReadAllLines("andari.txt");
                string[] fullnames = new string[lines.Length];
                Dictionary<string, string> dict = new Dictionary<string, string>();

                for (int i = 0; i < lines.Length; i++)
                {
                    string[] items = lines[i].Split('-');
                    if (!dict.ContainsKey(items[0]))
                    {
                        dict.Add(items[0], items[1]);
                    }
                }
                for (int i = 0; i < lbandari.Items.Count; i++)
                {
                    fullnames[i] = dict[lbandari.Items[i].ToString()];                
                }
                lbandari.DataSource = fullnames;





Thanks

Darma

Answers (1)