To take advantage of loose coupling, I suggest you to change your design and use MVC like this;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var view = new FormMain();
var controller = new Controller(view, new EmployeeManager(new DataService()));
Application.Run(view);
}
public partial class FormMain : Form
{
private Controller _controller;
public FormMain()
{
InitializeComponent();
}
public void SetController(Controller controller)
{
_controller = controller;
}
private void button1_Click(object sender, EventArgs e)
{
var newEmp = _controller.SearchEmployee(txtEmployeeID.Text);
if(null == newEmp)
{
}
txtEmployeeName.Text = newEmp.EmployeeName;
txtEmployeeAddress.Text = newEmp.EmployeeAddress;
}
}
public class Controller
{
private FormMain _view;
private EmployeeManager _manager;
public Controller(FormMain view, EmployeeManager manager)
{
_view = view;
_manager = manager;
view.SetController(this);
}
public Employee SearchEmployee(string employeeID)
{
return _manager.SearchEmployee(employeeID);
}
private void UpdateService(DataService newService)
{
_manager = new EmployeeManager(newService);
}
}
public class EmployeeManager
{
private DataService _ds;
public EmployeeManager(DataService DS)
{
_ds = DS;
}
public Employee SearchEmployee(string employeeID)
{
var dt = _ds.GetByEmployeeId(employeeID);
if (dt.Rows.Count > 0)
{
return new Employee(employeeID,
dt.Rows[0].ItemArray[1].ToString(),
dt.Rows[0].ItemArray[2].ToString());
}
return null;
}
}
public class Employee
{
public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string EmployeeAddress { get; set; }
public Employee(string employeeID, string employeeName, string employeeAddress)
{
EmployeeID = employeeID;
EmployeeName = employeeName;
EmployeeAddress = employeeAddress;
}
}
public class DataService
{
public DataTable GetByEmployeeId(string employeeID)
{
using (SqlConnection newCon = new SqlConnection(""))
{
SqlCommand Cmd = new SqlCommand("SELECT..WHERE emp_id=@employeeID", newCon);
Cmd.Parameters.Add("@employeeID", SqlDbType.VarChar).Value = employeeID;
newCon.Open();
SqlDataReader rdr = Cmd.ExecuteReader();
DataTable results = new DataTable();
results.Load(rdr);
return results;
}
}
}