Exemplo n.º 1
0
        /*public static int ExecuteInsertBook(BusinessLayer.Book book, int authorid, int typeid)
         * {
         *  SqlConnection conn;
         *  SqlCommand cmd;
         *
         *  using (conn = new SqlConnection(GetConnectionString()))
         *  {
         *      try
         *      {
         *          string sql = "INSERT INTO Book (Title, PageCount, Price, AuthorId, TypeId) VALUES (@Title, @PageCount, @Price, @AuthorId, @TypeId)";
         *          //string sql = "sp_InsertBookInfo";
         *
         *          conn.Open();
         *
         *          using (cmd = new SqlCommand(sql, conn))
         *          {
         *
         *              SqlParameter[] param = new SqlParameter[5];
         *
         *              param[0] = new SqlParameter("@Title", System.Data.SqlDbType.VarChar, 20);
         *              param[1] = new SqlParameter("@PageCount", System.Data.SqlDbType.Int);
         *              param[2] = new SqlParameter("@Price", System.Data.SqlDbType.Decimal, 2);
         *              param[3] = new SqlParameter("@AuthorId", System.Data.SqlDbType.Int);
         *              param[4] = new SqlParameter("@TypeId", System.Data.SqlDbType.Int);
         *
         *
         *              param[0].Value = book.Title;
         *              param[1].Value = book.PageCount;
         *              param[2].Value = book.Price;
         *              param[3].Value = authorid;
         *              param[4].Value = typeid;
         *
         *
         *              foreach (SqlParameter p in param)
         *              {
         *                  cmd.Parameters.Add(p);
         *              }
         *
         *              cmd.CommandType = System.Data.CommandType.Text;
         *              return (int)cmd.ExecuteNonQuery();
         *          }
         *      }
         *      catch (SqlException ex)
         *      {
         *          Console.WriteLine(ex.Message);
         *          return ex.ErrorCode;
         *      }
         *  }
         * }*/

        public static int ExecuteInsertAuthor(BusinessLayer.Author author)
        {
            int    authorid = 0;
            string sql      = "INSERT INTO Author (FirstName, LastName) VALUES (@FirstName, @LastName)" +
                              "SELECT COUNT(*) FROM Author";

            using (SqlConnection conn = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 20);
                cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 20);
                cmd.Parameters["@FirstName"].Value = author.FirstName;
                cmd.Parameters["@LastName"].Value  = author.LastName;
                try
                {
                    conn.Open();
                    authorid = (int)cmd.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            return(authorid);
        }
Exemplo n.º 2
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.Page.IsValid)
            {
                BusinessLayer.Book   book   = new BusinessLayer.Book();
                BusinessLayer.Author author = new BusinessLayer.Author();
                BusinessLayer.Type   type   = new BusinessLayer.Type();
                book.Title       = this.tb_title.Text;
                book.PageCount   = Convert.ToInt32(this.tb_pagecount.Text);
                book.Price       = Convert.ToDecimal(this.tb_price.Text);
                author.FirstName = this.tb_authorfirstname.Text;
                author.LastName  = this.tb_authorlastname.Text;
                type.Name        = this.tb_type.Text;


                int result = DataAccessLayer.UtilityTools.ExecuteInsertBook(book, author, type);

                if (result == 1)
                {
                    this.lbl_resultmessage.Text = "Submission Succesful!";
                }

                /*else if (result == 0)
                 *  this.lbl_resultmessage.Text = "There was an error at the Database level";
                 * else
                 *  this.lbl_resultmessage.Text = "There was an error at the Method level";*/
                else
                {
                    this.lbl_resultmessage.Text = result.ToString();
                }
            }
        }