Israel

Israel

  • NA
  • 1.3k
  • 204.5k

populate on my combobox...

Oct 12 2014 5:13 PM

Hi!

On my form I have a combobox1 and label1.

With these codes I would like to populate data from on my combobox1 from the name column to show others datas columns on the textboxes. Curiously its cant populate in my combobox1

Anyone can help?


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;


namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\INAC\Documents\Windows_Application_Samples\ComboboxItems_show_in_label\App_Data\office.mdb;Persist Security Info=False";

    OleDbConnection con = new OleDbConnection(str);

    string query = "select * from company where name = '"+comboBox1.Text+"' ";

    OleDbCommand cmd = new OleDbCommand( query,con);

    OleDbDataReader dbr;

    try

    {

    con.Open();

    dbr = cmd.ExecuteReader();

    while(dbr.Read())

    {

    string sID = (string) dbr["ID"].ToString();

    string sname = (string) dbr["name"].ToString(); // name is string value

    string ssurname = (string) dbr["surname"].ToString();

    string sage = (string) dbr["age"].ToString();

 

    label1.Text = ssurname;

    }

    }

    catch(Exception es)

    {

    MessageBox.Show(es.Message);

    }  

    }


Answers (5)