I have a access database and tables fields like ClassCode(PK, autonumber) and class_standard
In the c# windows form m having one combobox , and when i select the class standard in the class combobox its classcode field value should be select.
how to do this
Loading
Satyapriya NayakPosted Jul 15, 2012, 1:38 PM
Try this...
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 Form2 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
string str;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Choose class_standard");
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from class";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["class_standard"]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Text = "";
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from class where class_standard='" + comboBox1.Text.Trim() + "'";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox2.Text = reader["ClassCode"].ToString();
}
con.Close();
reader.Close();
}
}
}
Thanks
If this post helps you mark it as answer