id been working on this for days now, maybe someone could help me figure this one, i have this items in listbox2, my database has a field for project, data directory, etc... if my first item in listbox2 equals to the field PROJECT in my database, i want to get the data directory of that item which is in the field of data directory, but im having problem with my if statement,(comparing the value of listbox2.items[0] to MyDataTable) please help me... thank you in advance...

private void btnConnect_Click(object sender, EventArgs e)
        {
            bool MyCon1_suc = false;
            {
                if (MyCon1 != null && MyCon1.State == ConnectionState.Open)
                {
                    MyCon1.Close();
                    MyCmd1.Dispose();
                }
                try
                {
                    //connecting to database
                    MyCon1 = new OleDbConnection();
                    MyCon1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\" + txtPath.Text;
                    MyCon1.Open();
                    MyCmd1 = MyCon1.CreateCommand();
                    MessageBox.Show("Connected to " + txtPath.Text, "Dbase connection!!!");
                    MyCon1_suc = true;

                    string commandString = "Select * from Replicate";
                    MyDataAdapter1 = new OleDbDataAdapter(commandString, MyCon1);

                    DataSet ds = new DataSet();
                    MyDataAdapter1.Fill(ds, "Replicate");
                    MyDataTable = ds.Tables["Replicate"];

                    listBox1.DataSource = ds.Tables["Replicate"];
                    listBox1.DisplayMember = "Project";
                    listBox1.ValueMember = "Project";

                    if (MyDataTable.Columns["Project"].ToString () != null)
                    {
                        int i = 0;
                        do
                        {

                            if (listBox2.Items[0].ToString() == MyDataTable.Rows[i]["Projects"].ToString())
                            // im having problem here, comparing my listbox2.item to mydatatables, it jumps to catch{} after reading my if

                           {
                                string DIR = MyDataTable.Rows[i]["Data_Directory"].ToString();
                                MessageBox.Show("C:\\ " + DIR);
                                
                            }
                            i++;
                            
                        } while (ds.Tables["Replicate"] != null);
                    }
                    

                

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

Hi!

have you checked if the listBox2 really have items? because in your code i only see you set for listBox1 the DataSource, but i can't see where you fill your listBox2. Maybe it is a typo and you should use listBox1 instead?

mybe its a hint for your problem
Daniel

Hi!

have you checked if the listBox2 really have items? because in your code i only see you set for listBox1 the DataSource, but i can't see where you fill your listBox2. Maybe it is a typo and you should use listBox1 instead?

mybe its a hint for your problem
Daniel

my value for listbox2 will come from listbox1 when i click one of the items in there,

Hi!

have you checked if the listBox2 really have items? because in your code i only see you set for listBox1 the DataSource, but i can't see where you fill your listBox2. Maybe it is a typo and you should use listBox1 instead?

mybe its a hint for your problem
Daniel

sir daniel, thank you for the hint, it helped me a lot, its working now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.