Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need a sample of code that will allow me to type a name in a text box and on change of the text box if record exist populate information from my database into text boxes.

sample code please, please, please



thanks ahead of time.
Posted

Every time you enter correct employee name textbox1 and textbox2 shows his additional information.
DataSet ds;        
        private void Form1_Load(object sender, EventArgs e)
        {
            ds = new DataSet();
            DataAdapter da = new SqlDataAdapter(@"SELECT * FROM Personel", @"Data Source=HEINZZ-PC\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True");
            da.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];          
        }        

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            string text = ((ComboBox)sender).Text;
            IEnumerable<DataRow> query =
                from employee in ds.Tables[0].AsEnumerable()
                where (employee["Name"]).ToString().Trim() == text
                select employee;
            if (query.Count() != 0)
            {
                this.textBox1.Text = query.First()["ID"].ToString();
                this.textBox2.Text = query.First()["Age"].ToString();
            }
        }





This is one of the ways to make it. If you use a combobox with datasource it will prompt user to the right name.
private void Form1_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataAdapter da = new SqlDataAdapter(@"SELECT * FROM Personel", @"Data Source=HEINZZ-PC\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True");
            da.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];
            
            this.comboBox1.DataSource = ds.Tables[0];
            this.comboBox1.DisplayMember = "Name";
            
            this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.textBox1.Text = ((DataRowView)(this.comboBox1.SelectedItem))["ID"].ToString();
            this.textBox2.Text = ((DataRowView)(this.comboBox1.SelectedItem))["Age"].ToString();
        }
 
Share this answer
 
v2
You need to research on c# filtering dataset or c# filtering datatable to find out how to get the right records.

You should also look up c# parameterized queries for the correct/safe way to do it.
 
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