public Employee GetEmployee(int?id) { Employee employee = new Employee(); employee.EmployeeID = (int)id; DataTable dataTable = new DataTable(); try { using (MySqlCommand command = new MySqlCommand("instest.Proc_GetEmployeeDetails")) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@EmpID", employee.EmployeeID); using (DataObjectClass dataObjectClass = new DataObjectClass()) { dataTable = dataObjectClass.GetDataTablefromMySqlCmd(command); } } if (dataTable != null) { if (dataTable.Rows.Count > 0) { employee.EmployeeName = Convert.IsDBNull(dataTable.Rows[0].ItemArray[0].ToString()) ? "" : dataTable.Rows[0].ItemArray[0].ToString(); employee.Gender = Convert.IsDBNull(dataTable.Rows[0].ItemArray[1].ToString()) ? "" : dataTable.Rows[0].ItemArray[1].ToString(); employee.City = Convert.IsDBNull(dataTable.Rows[0].ItemArray[2].ToString()) ? "" : dataTable.Rows[0].ItemArray[2].ToString(); } else { employee.ErrorText = "No Data Found"; } } else { employee.ErrorText = "No Data Found"; } } catch (Exception ex) { employee.ErrorText = ex.Message.ToString(); throw; } finally { if (dataTable != null) { dataTable.Dispose(); dataTable = null; } } return(employee); }
//private bool connection_open = false; //private MySqlConnection connection; public Employee(int arg_id) { //Get_Connection(); EmployeeID = arg_id; DataTable dataTable = new DataTable(); DataSet dataSet = new DataSet(); DataRow dataRow = null; try { int id = 1; MySqlCommand command = new MySqlCommand { CommandText = "select name, gender, city from instest.tblemployee where id = @id" }; command.CommandType = CommandType.Text; command.Parameters.AddWithValue("@id", id); using (DataObjectClass dataObjectClass = new DataObjectClass()) { dataTable = dataObjectClass.GetDataTablefromMySqlCmd(command); dataRow = dataObjectClass.GetDataRowfromMySqlCmd(command); dataSet = dataObjectClass.GetDataSetfromMySqlCmd(command); } if (dataTable != null) { if (dataTable.Rows.Count > 0) { EmployeeName = Convert.IsDBNull(dataTable.Rows[0].ItemArray[0].ToString()) ? "" : dataTable.Rows[0].ItemArray[0].ToString(); Gender = Convert.IsDBNull(dataTable.Rows[0].ItemArray[1].ToString()) ? "" : dataTable.Rows[0].ItemArray[1].ToString(); City = Convert.IsDBNull(dataTable.Rows[0].ItemArray[2].ToString()) ? "" : dataTable.Rows[0].ItemArray[2].ToString(); } else { } } else { } //MySqlCommand cmd = new MySqlCommand //{ // Connection = connection, // CommandText = string.Format("select name, gender, city from instest.tblemployee where id = '{0}'", ID) //}; //MySqlDataReader reader = cmd.ExecuteReader(); //try //{ // reader.Read(); // if (reader.IsDBNull(0) == false) // { // Name = reader.GetString(0); // } // else // { // Name = null; // } // if (reader.IsDBNull(1) == false) // { // Gender = reader.GetString(1); // } // else // { // Gender = null; // } // if (reader.IsDBNull(2) == false) // { // City = reader.GetString(2); // } // else // { // City = null; // } // //if (reader.IsDBNull(3) == false) // //{ // // Photo = new byte[reader.GetInt32(4)]; // // reader.GetBytes(3, 0, Photo, 0, reader.GetInt32(4)); // //} // //else // //{ // // Photo = null; // //} // reader.Close(); //} //catch (MySqlException e) //{ // string MessageString = "Read error occurred / entry not found loading the Column details: " // + e.ErrorCode + " - " + e.Message + "; \n\nPlease Continue"; // //MessageBox.Show(MessageString, "SQL Read Error"); // reader.Close(); // Name = MessageString; // Gender = City = null; //} } catch (MySqlException e) { string MessageString = "The following error occurred loading the Column details: " + e.ErrorCode + " - " + e.Message; EmployeeName = MessageString; Gender = City = null; } //connection.Close(); }