Click here to Skip to main content
14,324,872 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi there.

I have a class 'Employee', BusinessLayer method 'Blayer.GetEmployeeDetails' and a PresentationLayer in a c# desktop application.

I need to retrieve the employee details from the presentation layer in order to display it.

Employee emp = new Employee(); //Calling the class 
Object empDetails = Blayer.GetEmployeeDetails(
(string)name, (string)surname, (string)roleType); //Calling the business layer method
string empName = emp.Name; //Adding employee name into string
label1.Text = empName; //Displaying in user interface


When i run the app, the results return 'null'.
I tried debugging and saw that the correct information is in 'empDetails' but when i try to assign the object to the variable e.g
string empName = empDetails.Name;
then i get an error.

Any help would do...

Thanks
Posted
Comments
member60 4-Oct-11 5:29am
   
Instead of taking a object create an object of the class whose properties u want to access
Rate this:
Please Sign up or sign in to vote.

Solution 1

empDetails needs to be of the return type of the Blayer.GetEmployeeDetails method not Object.
   
Comments
DJAppleboy 4-Oct-11 8:39am
   
I've tried that before. Still not working
Rate this:
Please Sign up or sign in to vote.

Solution 3

hey dude, use the return type of the method in the business layer or use var:
var empDetails = Blayer.GetEmployeeDetails(
(string)name, (string)surname, (string)roleType);
if it doesn't work, try stepping into the business layer cause maybe the error is happening there
   
Comments
DJAppleboy 4-Oct-11 8:48am
   
Tried that. Getting 'null' results
Rate this:
Please Sign up or sign in to vote.

Solution 4

try this
Employee emp = new Employee(); //Calling the class 
emp  = Blayer.GetEmployeeDetails(
(string)name, (string)surname, (string)roleType); //Calling the business layer method
string empName = emp.Name; //Adding employee name into string
label1.Text = empName; //Displaying in user interface
   
Comments
DJAppleboy 4-Oct-11 8:47am
   
I've tried that approach first. That led me to this....
http://www.codeproject.com/Questions/263581/3-Tier-Layer-Business-to-Presenation-Layer-Error
Rate this:
Please Sign up or sign in to vote.

Solution 5

try this

Employee emp = new Employee(); //Calling the class 
Object empDetails = Blayer.GetEmployeeDetails(
(string)name, (string)surname, (string)roleType);
string empName = emp.Name.ToString();
label1.Text = empName.ToString();




Yogesh
   
Comments
DJAppleboy 4-Oct-11 9:13am
   
No luck
Rate this:
Please Sign up or sign in to vote.

Solution 6

BusinessLayer Code

public Teacher GetEmployeeDetails(string name, string surname, string role)
        {
            using (SqlConnection loginCon = new SqlConnection(ConString))
            {
                SqlCommand LoginCmd = new SqlCommand
                    ("procGetTeacherDetails", loginCon);
                LoginCmd.CommandType = CommandType.StoredProcedure;

                LoginCmd.Parameters.Add(new SqlParameter
                    ("@Name", SqlDbType.NChar, (30)));
                LoginCmd.Parameters.Add(new SqlParameter
                    ("@Surname", SqlDbType.NChar, (30)));
                LoginCmd.Parameters.Add(new SqlParameter
                    ("@RoleID", SqlDbType.Int ));

                LoginCmd.Parameters["@Surname"].Value = surname;
                LoginCmd.Parameters["@Name"].Value = name;
                LoginCmd.Parameters["@RoleID"].Value = role;

                Teacher teacherDetails;

                //try
                //{
                    loginCon.Open();
                    SqlDataReader reader = LoginCmd.ExecuteReader();
                    reader.Read();

                    teacherDetails = new Teacher(
                        Convert.ToString(reader["DateEmployed"]),
                        Convert.ToString(reader["ResidentialAddress"]),
                        Convert.ToString(reader["ResSuburbArea"]),
                        Convert.ToString(reader["ResCityTown"]),
                        Convert.ToString(reader["ResPostalCode"]),
                        Convert.ToString(reader["PostalAddress"]),
                        Convert.ToString(reader["PostSuburbArea"]),
                        Convert.ToString(reader["PostCityTown"]),
                        Convert.ToString(reader["PostPostalCode"]),
                        Convert.ToString(reader["TelH"]),
                        Convert.ToString(reader["TelC"]),
                        Convert.ToString(reader["Email"]));
                    reader.Close();
                //}
                //catch (SqlException)
                //{
                //    throw new ApplicationException
                //        ("Error connecting to database");
                //}
                return teacherDetails;
            }
   
Rate this:
Please Sign up or sign in to vote.

Solution 7

hai.,


use like this.,

employee obj=new employee(); //calling class
string name=txtname.Text;
string sname=txtsname.Text;
string role=txtrole.Text;
obj.GetEmployeeDetails(name,sname,role);

i hope this is helpful to you
   
Comments
DJAppleboy 4-Oct-11 17:12pm
   
Error1 - Does not contain a definition for GetEmployeeDetails (because im doing 3tier, so it's called from a different dll).
Error2 - Are you missing a using directive

Thanks for your trouble though
Rate this:
Please Sign up or sign in to vote.

Solution 8

The one thing i think might be causing the problem is if there're no results in the reader you should take a look at this code
public bool GetEmployeeDetails(Teacher teacher)
        {
            using (SqlConnection loginCon = new SqlConnection(ConString))
            {
                SqlCommand LoginCmd = new SqlCommand
                    ("procGetTeacherDetails", loginCon);
                LoginCmd.CommandType = CommandType.StoredProcedure;
 
                LoginCmd.Parameters.Add(new SqlParameter
                    ("@Name", SqlDbType.NChar, (30)));
                LoginCmd.Parameters.Add(new SqlParameter
                    ("@Surname", SqlDbType.NChar, (30)));
                LoginCmd.Parameters.Add(new SqlParameter
                    ("@RoleID", SqlDbType.Int ));
 
                LoginCmd.Parameters["@Surname"].Value = teacher.Surname;
                LoginCmd.Parameters["@Name"].Value = teacher.Name;
                LoginCmd.Parameters["@RoleID"].Value = teacher.Role;
 
                
 
                //try
                //{
                    loginCon.Open();
                    SqlDataReader reader = LoginCmd.ExecuteReader();
                    if(reader.HasRows)
                    {
                        reader.Read();
                        teacher.DateEmployed= reader["DateEmployed"]),
                        //add the rest of the properties as well
                         reader.Close();
                         return true;}
                //}
                //catch (SqlException)
                //{
                //    throw new ApplicationException
                //        ("Error connecting to database");
                //}
                return false;
            }



this way you pass your teacher object by reference and the values will be assigned if you find the teacher and it will return true showing it's found and false if nothing is found. and by returning a boolean you presentation layer will know whether teacher is found or not and can also react appropriately

Adding the reader.HasRows in the if statement make your code safer cause you won't be trying access the reader when it's empty, this might be the cause of your code returning null
   
v4
Rate this:
Please Sign up or sign in to vote.

Solution 9

I believe your Object EmpDetails has no specific type.
You need to cast it to whatever type is returned from Blayer.GetEmployeeDetails method.

eg.
if Blayer.GetEmployeeDetails returns an object of type "EmployeeDetails"
then this should work.

string empName = ((EmployeeDetails)(EmpDetails)).Name

This will convert the unknown object EmpDetails to a specific "EmployeeDetails" type object. Which means you can then access the properties.

hope this helps.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100