public string UpdateWCFEmployee(WCFEmployeeWOEF Employee) { string cs = ConfigurationManager.ConnectionStrings["WCF"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spUpdateWCFEmployee", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter parameterName = new SqlParameter { ParameterName = "@Name", Value = Employee.Name }; cmd.Parameters.Add(parameterName); SqlParameter parameterGender = new SqlParameter { ParameterName = "@Gender", Value = Employee.Gender }; cmd.Parameters.Add(parameterGender); SqlParameter parameterDateOfBirth = new SqlParameter { ParameterName = "@DateOfBirth", Value = Employee.DateOfBirth }; cmd.Parameters.Add(parameterDateOfBirth); //Adding more attributes for complex Employee type SqlParameter parameterEmployeeType = new SqlParameter { ParameterName = "@EmployeeType", Value = Employee.Type }; cmd.Parameters.Add(parameterEmployeeType); if (Employee.GetType() == typeof(FullTimeEmployee)) // To decide type/class of data getting to save { SqlParameter parameterAnnualSalary = new SqlParameter { ParameterName = "@AnnualSalary", Value = ((FullTimeEmployee)Employee).AnnualSalary }; cmd.Parameters.Add(parameterAnnualSalary); } else { SqlParameter parameterHourlyPay = new SqlParameter { ParameterName = "@HourlyPay", Value = ((PartTimeEmployee)Employee).HourlyPay, }; cmd.Parameters.Add(parameterHourlyPay); SqlParameter parameterHoursWorked = new SqlParameter { ParameterName = "@HoursWorked", Value = ((PartTimeEmployee)Employee).HoursWorked }; cmd.Parameters.Add(parameterHoursWorked); } SqlParameter parameterIdtochange = new SqlParameter { ParameterName = "@Id", Value = Employee.Id }; cmd.Parameters.Add(parameterIdtochange); con.Open(); cmd.ExecuteNonQuery(); } string Updatemessage = Employee.Name + " profile has been updated succesfully"; return(Updatemessage); //throw new NotImplementedException(); }
public void SaveWCFEmployee(WCFEmployeeWOEF Employee) { string cs = ConfigurationManager.ConnectionStrings["WCF"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spSaveWCFEmployee", con); cmd.CommandType = CommandType.StoredProcedure; //Commenting to leave employee Id to autogenerate at Database side //SqlParameter parameterId = new SqlParameter //{ // ParameterName = "@Id", // Value = Employee.Id //}; //cmd.Parameters.Add(parameterId); SqlParameter parameterName = new SqlParameter { ParameterName = "@Name", Value = Employee.Name }; cmd.Parameters.Add(parameterName); SqlParameter parameterGender = new SqlParameter { ParameterName = "@Gender", Value = Employee.Gender }; cmd.Parameters.Add(parameterGender); SqlParameter parameterDateOfBirth = new SqlParameter { ParameterName = "@DateOfBirth", Value = Employee.DateOfBirth }; cmd.Parameters.Add(parameterDateOfBirth); //Adding more attributes for complex Employee type SqlParameter parameterEmployeeType = new SqlParameter { ParameterName = "@EmployeeType", Value = Employee.Type }; cmd.Parameters.Add(parameterEmployeeType); if (Employee.GetType() == typeof(FullTimeEmployee)) // To decide type/class of data getting to save { SqlParameter parameterAnnualSalary = new SqlParameter { ParameterName = "@AnnualSalary", Value = ((FullTimeEmployee)Employee).AnnualSalary }; cmd.Parameters.Add(parameterAnnualSalary); } else { SqlParameter parameterHourlyPay = new SqlParameter { ParameterName = "@HourlyPay", Value = ((PartTimeEmployee)Employee).HourlyPay, }; cmd.Parameters.Add(parameterHourlyPay); SqlParameter parameterHoursWorked = new SqlParameter { ParameterName = "@HoursWorked", Value = ((PartTimeEmployee)Employee).HoursWorked }; cmd.Parameters.Add(parameterHoursWorked); } con.Open(); cmd.ExecuteNonQuery(); //Reference for using SQLDATAADAPTER http://csharp.net-informations.com/dataadapter/insertcommand-sqlserver.htm //adapter.InsertCommand = new SqlCommand(sql, connection); //adapter.InsertCommand.ExecuteNonQuery(); //MessageBox.Show("Row inserted !! "); } }