public void getallemployee() { List<EmployeeAJAX> listemployees = new List<EmployeeAJAX>(); string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("select * from tblemployee", con); con.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { EmployeeAJAX employee = new EmployeeAJAX(); employee.ID = Convert.ToInt32(rdr["Id"]); employee.name = rdr["name"].ToString(); employee.gender = rdr["gender"].ToString(); employee.salary = Convert.ToInt32(rdr["salary"]); listemployees.Add(employee); } } // This code returns the object in XML format if we want to return a JSO then make the following changes /* we will not return from the function and hence set the return type to VOID */ JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Write(js.Serialize(listemployees)); //and delete the return employee // return employee; use this code in case u would like to send a xml object from teh webservice }
public void addemployees(EmployeeAJAX emp) { string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spinsertemployees", con); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter() { ParameterName = "@name", Value = emp.name }); cmd.Parameters.Add(new SqlParameter() { ParameterName = "@gender", Value = emp.gender }); cmd.Parameters.Add(new SqlParameter() { ParameterName = "@salary", Value = emp.salary }); con.Open(); cmd.ExecuteNonQuery(); } }
public void getemployeebyid(int employeeid) { EmployeeAJAX employee = new EmployeeAJAX(); string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spgetemployeebyid", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter parameter = new SqlParameter(); parameter.ParameterName = "@Id"; parameter.Value = employeeid; cmd.Parameters.Add(parameter); con.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { employee.ID = Convert.ToInt32(rdr["Id"]); employee.name = rdr["name"].ToString(); employee.gender = rdr["gender"].ToString(); employee.salary = Convert.ToInt32(rdr["salary"]); } } // This code returns the object in XML format if we want to return a JSO then make the following changes /* we will not return from the function and hence set the return type to VOID */ JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Write(js.Serialize(employee)); //and delete the return employee // return employee; use this code in case u would like to send a xml object from teh webservice }