如何从组合框中的选择基础上从Access填充文本框 [英] How Text Boxes to be fill from Access on the basis from the selecion from combo box

查看:110
本文介绍了如何从组合框中的选择基础上从Access填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好





我正在使用VB 2008和MS Access 2007进行项目。我有一个组合框和五个文本框。我需要一个代码,当用户从组合框中选择一个项目时,五个文本框会自动填充来自访问的对应数据。



请由任何人引导体验人。



谢谢

Hello


I am using VB 2008 and MS Access 2007 for a project.I have one combo box and five text box.I required a code that when a user select an item from the combo box ,the five text boxes fill automatically with the correspondence data from access .

Please guide by any experience person.

Thanks

推荐答案

一些关于ComboBox的链接 [ ^ ]。 为初学者使用ADO.NET [ ^ ]。
Some links about ComboBoxes[^]. Using ADO.NET for beginners[^].


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 Combobox
{
    public partial class Form1 : Form
    {
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        OleDbCommand com;
        string str;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Text = "Select any id";
            comboBox1.Items.Add("Select any name");
            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from student";
            com = new OleDbCommand(str, con);
            OleDbDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                comboBox1.Items.Add(reader["sid"].ToString());
            }
            reader.Close();
            con.Close(); 
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from student where sid='" + comboBox1.Text + "'";
            com = new OleDbCommand(str, con);
            OleDbDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                textBox1.Text = reader["sname"].ToString();
                textBox2.Text = reader["smarks"].ToString();
            }
            reader.Close();
            con.Close();
        }
    }
}


这篇关于如何从组合框中的选择基础上从Access填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆