Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
I am trying to bind the value that is coming in list object to Listbox but on debug it is showing value but when form loads listbox is empty. I am trying to create a IE BHO in which i want to load all the words that are available in excelsheet file name DictionaryFile. Below is my code:

Please help me out, thanking you all

private void Form1_Load(object sender, EventArgs e)
       {
          // provider = (CodeDomProvider)Activator.CreateInstance("Microsoft.JScript", "Microsoft.JScript.JScriptCodeProvider").Unwrap();
           if (istrue == false)
           {
               LoadAllWord();
           }
       }

       private void LoadAllWord()
       {
           istrue = true;
           OleDbConnection objConn = null;
           //Passing Query String and Ole connection object to Ole DB Command
           OleDbCommand cmd = new OleDbCommand();
           //Declaring OleDbDataAdapter object which execute the OleDbCommand in next line.
           OleDbDataAdapter oleda = new OleDbDataAdapter();
           //Creating Query String
           string querystr = "";
           //Declaring new Data table with dt object
           DataTable dt = new DataTable();
           DataSet ds = new DataSet();
           try
           {

               string FileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) + "\\Dictionary\\Dictionary.xls";
               //Creating Connection String
               String connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + FileName + " ;Persist Security Info=False;Extended Properties=Excel 8.0;";
               //Passing Connection String Value into Ole Connection Object
               objConn = new OleDbConnection(connString);
               //Connection is getting open from here
               objConn.Open();
               //Creating Query String
              // querystr = @"SELECT *  from [DictionaryFile$] where CHECK_DIGIT > 0";// Record_Level < 501 and
               querystr = @"SELECT *  from [DictionaryFile$]";// Record_Level < 501 and
               //querystr
               //Passing Query String and Ole connection object to Ole DB Command
               cmd = new OleDbCommand(querystr, objConn);
               //Passing Command Value to OldDbDataAdapter object
               oleda.SelectCommand = cmd;
               // Filling OleDataAdapter value in DataTable Object dt
               oleda.Fill(dt);
               //oleda.Fill(ds);

               List<string> lst = new List<string>();
               foreach (DataRow r in dt.Rows)
               {
                   //lst.Add(r["words"].ToString());
                   lstInstalledWord.Items.Add(r["words"].ToString());
               }

              // lstInstalledWord.Items.Clear();
               //lstInstalledWord.DisplayMember = "words";
               //lstInstalledWord.ValueMember = "words";
               //lstInstalledWord.DataSource = lst;
               //lstInstalledWord.DisplayMember = "words";
               //lstInstalledWord.DataSource = ds.Tables["words"];


           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

           //lstInstalledWord.DataSource = dt;//DataSource to GrigView(Id:gvOne)
       }
Posted

1 solution

Based on few comments words (querystring, gridview, etc), I assume you are working on a web application and it's an ASP.NET app code.

If so, you need to bind the items once defined to the list.
Post fetching the values, do:
C#
oleda.SelectCommand = cmd;
oleda.Fill(dt);
lstInstalledWord.Items.Clear();
lstInstalledWord.DataSource = dt;
lstInstalledWord.DisplayMember = "words";
lstInstalledWord.ValueMember = "words";
lstInstalledWord.DataBind();

Once you define the datasource, you need to bind too. Try!
 
Share this answer
 
v2

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