public HttpResponseMessage UpdateStudentSastanak([FromBody] StudentSastanak studentSastanak) { using (var connection = new SqlConnection(ConnectionString)) { try { using (var sqlCmd = new SqlCommand("UPDATE Tim.StudentSastanak SET StudentID=@StudentID, SastanakID=@SastanakID WHERE StudentSastanakID=@id")) { connection.Open(); sqlCmd.Connection = connection; sqlCmd.Parameters.AddWithValue("StudentID", studentSastanak.StudentID); sqlCmd.Parameters.AddWithValue("SastanakID", studentSastanak.SastanakID); sqlCmd.Parameters.AddWithValue("id", studentSastanak.StudentSastanakID); int rowAffected = sqlCmd.ExecuteNonQuery(); if (rowAffected == 0) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Error 404 Not Found")); } } } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error 400 Bad Request")); } } return(Request.CreateResponse(HttpStatusCode.OK, "Updated Successfully")); }
public HttpResponseMessage GetStudentSastanak() { List <StudentSastanak> result = new List <StudentSastanak>(); using (var connection = new SqlConnection(ConnectionString)) { SqlCommand sqlCmd = new SqlCommand("select * from Tim.StudentSastanak", connection) { CommandType = CommandType.Text }; try { connection.Open(); using (SqlDataReader dataReader = sqlCmd.ExecuteReader()) { while (dataReader.Read()) { var studentSastanak = new StudentSastanak { StudentSastanakID = Convert.ToInt32(dataReader["StudentSastanakID"]), SastanakID = Convert.ToInt32(dataReader["SastanakID"]), StudentID = Convert.ToInt32(dataReader["StudentID"]) }; result.Add(studentSastanak); } } } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error retrieving data")); } } return(Request.CreateResponse(HttpStatusCode.OK, result)); }
public HttpResponseMessage CreateStudentSastanak([FromBody] StudentSastanak studentSastanak) { using (var connection = new SqlConnection(ConnectionString)) { SqlCommand sqlCmd = new SqlCommand("INSERT INTO Tim.StudentSastanak VALUES(@SastanakID, @StudentID)", connection); sqlCmd.Parameters.AddWithValue("SastanakID", studentSastanak.SastanakID); sqlCmd.Parameters.AddWithValue("StudentID", studentSastanak.StudentID); try { connection.Open(); int i = sqlCmd.ExecuteNonQuery(); SqlCommand sqlUniverzitet = new SqlCommand("SELECT TOP 1 * FROM Tim.StudentSastanak ORDER BY StudentSastanakID DESC", connection); StudentSastanak last = new StudentSastanak(); using (SqlDataReader studentSastanakRead = sqlUniverzitet.ExecuteReader()) { while (studentSastanakRead.Read()) { studentSastanak = new StudentSastanak { StudentSastanakID = Convert.ToInt32(studentSastanakRead["StudentSastanakID"]), SastanakID = Convert.ToInt32(studentSastanakRead["SastanakID"]), StudentID = Convert.ToInt32(studentSastanakRead["StudentID"]) }; } } var response = Request.CreateResponse(HttpStatusCode.Created, last); response.Headers.Location = new Uri(Request.RequestUri + "/" + last.StudentSastanakID); return(response); } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error 404 Bad request")); } } }