Wednesday, October 5, 2011

UPDATE DROP DOWN

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace UpdateDropDown
{
class Employee
{

SqlConnection sqlcon = null;
SqlDataAdapter Adptr = null;

public Employee() {

}

private void connectDB() {
string constr = "Data Source=VINDANA-PC;Initial Catalog=W3School;Persist Security Info=True;User ID=sa;Password=saranga";
sqlcon = new SqlConnection(constr);
sqlcon.Open();
}

public DataTable updateDropDown() {

connectDB();
Adptr = new SqlDataAdapter("SELECT Emp_Id,Emp_Name FROM Employee", sqlcon);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
Adptr.Fill(dt);
sqlcon.Close();

return dt;

}

public DataTable display(int id) {
connectDB();

Adptr = new SqlDataAdapter("SELECT * FROM Employee where Emp_Id= '" + id + " ' ", sqlcon);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
Adptr.Fill(dt);
sqlcon.Close();

return dt;
}

}
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace UpdateDropDown
{
class Department
{

SqlConnection sqlcon = null;
SqlDataAdapter Adptr = null;

public Department() {

}


private void connectDB()
{
string constr = "Data Source=.;Initial Catalog=W3School;Persist Security Info=True;User ID=sa;Password=saranga";
sqlcon = new SqlConnection(constr);
sqlcon.Open();
}

public DataTable depData(int id) {
connectDB();
Adptr = new SqlDataAdapter("SELECT * FROM Department where Dep_Id= '" + id + " ' ", sqlcon);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
Adptr.Fill(dt);
sqlcon.Close();

return dt;
}

}
}



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.SqlClient;

namespace UpdateDropDown
{
public partial class Form1 : Form
{

int ID;
string str;
public Form1()
{
InitializeComponent();

loadComboBox();


}

public void loadComboBox()
{
Employee emp = new Employee();
DataTable dt = emp.updateDropDown();
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Emp_Id";
comboBox1.ValueMember = "Emp_Id";


}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void btnsubmit_Click(object sender, EventArgs e)
{
Employee em = new Employee();
ID = Int16.Parse(comboBox1.SelectedValue.ToString());

//MessageBox.Show(ID.ToString());
DataTable dt = em.display(ID);

txtName.Text = dt.Rows[0].ItemArray.GetValue(1).ToString();
txtAge.Text = dt.Rows[0].ItemArray.GetValue(2).ToString();
txtDepId.Text = dt.Rows[0].ItemArray.GetValue(3).ToString();
int depid = Int16.Parse(txtDepId.Text);
//MessageBox.Show(depid.ToString());
Department dep = new Department();
DataTable dt2 = dep.depData(depid);
txtDepName.Text = dt2.Rows[0].ItemArray.GetValue(1).ToString();

}

private void Form1_Load(object sender, EventArgs e)
{

}

}
}

No comments:

Post a Comment