Show data related to combobox in label control

Satyapriyanayak
Posted by Satyapriyanayak under C# category on | Points: 40 | Views : 831
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_related_label
{
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())
{
label2.Text = reader["sname"].ToString();
label3.Text = reader["smarks"].ToString();
}
reader.Close();
con.Close();
}
}
}

Comments or Responses

Login to post response