Exemplo n.º 1
0
        public JsonResult InsertNewArtist()
        {
            var artist = new ArtistViewModel();

            try
            {
                var resolveRequest = HttpContext.Request;
                resolveRequest.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
                string jsonString = new System.IO.StreamReader(resolveRequest.InputStream).ReadToEnd();
                //deserialse
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string artistname = serializer.Deserialize <string>(jsonString);
                using (ArtistViewModel presentaion = new ArtistViewModel())
                {
                    artist.ArtistName = artistname;
                    var retVal = presentaion.Insert(artist);;
                    if (retVal >= 1)
                    {
                        //get the newly artist and show

                        artist = presentaion.GetObject(retVal);

                        return(new JsonResult
                        {
                            Data = new { Data = artist, Success = true, ErrorMessage = "Artist was created" },
                            ContentEncoding = System.Text.Encoding.UTF8,
                            JsonRequestBehavior = JsonRequestBehavior.AllowGet
                        });
                    }
                    else
                    {
                        return(new JsonResult
                        {
                            Data = new { Data = artist, Success = false, ErrorMessage = "Error in creating Artist" },
                            ContentEncoding = System.Text.Encoding.UTF8,
                            JsonRequestBehavior = JsonRequestBehavior.AllowGet
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                return(new JsonResult
                {
                    Data = new { Data = artist, Success = false, ErrorMessage = ex.Message },
                    ContentEncoding = System.Text.Encoding.UTF8,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Exemplo n.º 2
0
        public JsonResult InsertNewRecordNO_EF()
        {
            var artist = new ArtistViewModel();

            try
            {
                var resolveRequest = HttpContext.Request;
                resolveRequest.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
                string jsonString = new System.IO.StreamReader(resolveRequest.InputStream).ReadToEnd();
                //deserialse
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string artistname = serializer.Deserialize <string>(jsonString);
                using (ArtistViewModel presentaion = new ArtistViewModel())
                {
                    artist.ArtistName = artistname;
                    var    retVal           = 0;;
                    string connectionString = ConfigurationManager.ConnectionStrings["ArtWorkDBConnectionString"].ToString();
                    using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        con.Open();
                        using (SqlCommand command = new SqlCommand("INSERT INTO [dbo].[Artist] ([Artist]) output INSERTED.ArtistID  VALUES(@param)", con))
                        {
                            command.Parameters.Add("@param", System.Data.SqlDbType.VarChar, 50).Value = artist.ArtistName;
                            // retVal=   command.ExecuteNonQuery();

                            retVal = (int)command.ExecuteScalar();
                        }
                    }
                    if (retVal >= 1)
                    {
                        //get the newly artist and show

                        artist = presentaion.GetObject(retVal);

                        return(new JsonResult
                        {
                            Data = new { Data = artist, Success = true, ErrorMessage = "Artist was created" },
                            ContentEncoding = System.Text.Encoding.UTF8,
                            JsonRequestBehavior = JsonRequestBehavior.AllowGet
                        });
                    }
                    else
                    {
                        return(new JsonResult
                        {
                            Data = new { Data = artist, Success = false, ErrorMessage = "Error in creating Artist" },
                            ContentEncoding = System.Text.Encoding.UTF8,
                            JsonRequestBehavior = JsonRequestBehavior.AllowGet
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                return(new JsonResult
                {
                    Data = new { Data = artist, Success = false, ErrorMessage = ex.Message },
                    ContentEncoding = System.Text.Encoding.UTF8,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }