コード例 #1
0
 public void AlbumList_InsertItem()
 {
     if (ModelState.IsValid)
     {
         Model.Album item = new Individuellt_arbete.Model.Album();
         if (TryUpdateModel(item))
         {
             // Save changes here
             try
             {
                 Service.saveAlbum(item);
                 Page.SetTempData("successmessage", "Albumet skapades.");
                 Response.RedirectToRoute("EditAlbums", new {page=DataPager.StartRowIndex / DataPager.PageSize + 1});
             }
             catch (ValidationException vx)
             {
                 var validationResult = vx.Data["validationResult"] as List<ValidationResult>;
                 validationResult.ForEach(r => ModelState.AddModelError(String.Empty, r.ErrorMessage));
                 return;
             }
             catch (Exception ex)
             {
                 ModelState.AddModelError(String.Empty, ex.Message);
                 return;
             }
         }
     }
 }
コード例 #2
0
        public Album GetAlbumById(int id)
        {
            using (var conn = CreateConnection())
            {
                Album album;
                SqlCommand cmd = new SqlCommand("getAlbumById", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@albumId", SqlDbType.Int, 4).Value = id;

                conn.Open();

                using (var reader = cmd.ExecuteReader())
                {
                    int albumIdIndex = reader.GetOrdinal("AlbumId");
                    int albumNameIndex = reader.GetOrdinal("AlbumName");
                    int releaseYearIndex = reader.GetOrdinal("ReleaseYear");

                    if (reader.Read())
                    {
                        album = new Album
                        {
                            AlbumName = reader.GetString(albumNameIndex),
                            ReleaseYear = reader.GetInt16(releaseYearIndex),
                            AlbumId = reader.GetInt32(albumIdIndex)
                        };
                        return album;
                    }
                    return null;
                }
            }
        }
コード例 #3
0
 public void AlbumList_InsertItem()
 {
     if (ModelState.IsValid)
     {
         Model.Album item = new Individuellt_arbete.Model.Album();
         if (TryUpdateModel(item))
         {
             // Save changes here
             try
             {
                 Service.saveAlbum(item);
                 Page.SetTempData("successmessage", "Albumet skapades.");
                 Response.RedirectToRoute("EditAlbums", new { page = DataPager.StartRowIndex / DataPager.PageSize + 1 });
             }
             catch (ValidationException vx)
             {
                 var validationResult = vx.Data["validationResult"] as List <ValidationResult>;
                 validationResult.ForEach(r => ModelState.AddModelError(String.Empty, r.ErrorMessage));
                 return;
             }
             catch (Exception ex)
             {
                 ModelState.AddModelError(String.Empty, ex.Message);
                 return;
             }
         }
     }
 }
コード例 #4
0
 public void saveAlbum(Album album)
 {
     ICollection<ValidationResult> validationResult;
     if (!album.Validate(out validationResult))
     {
         var vx = new ValidationException("Objektet klarade inte valideringen.");
         vx.Data.Add("validationResult", validationResult);
         throw vx;
     }
     if (album.AlbumId == 0)
     {
         Album.SaveAlbum(album);
     }
     else
     {
         Album.UpdateAlbum(album);
     }
 }
コード例 #5
0
        internal void SaveAlbum(Album album)
        {
            using (var conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("addAlbum", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@albumName", SqlDbType.VarChar, 50).Value = album.AlbumName;
                cmd.Parameters.Add("@releaseYear", SqlDbType.Int, 4).Value = album.ReleaseYear;
                cmd.Parameters.Add("@albumId", SqlDbType.Int, 4).Direction = ParameterDirection.Output;

                conn.Open();
                cmd.ExecuteNonQuery();

                album.AlbumId = (int)cmd.Parameters["@albumId"].Value;
            }
        }
コード例 #6
0
        public void UpdateAlbum(Album album)
        {
            using (var conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("updateAlbum", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@albumName", SqlDbType.VarChar, 50).Value = album.AlbumName;
                cmd.Parameters.Add("@releaseYear", SqlDbType.Int, 4).Value = album.ReleaseYear;
                cmd.Parameters.Add("@albumId", SqlDbType.Int, 4).Value = album.AlbumId;

                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }