Exemple #1
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();
                }
            }
        }
        public static int ExecuteInsertBook(Book book, Author author, BusinessLayer.Type type)
        {
            SqlConnection conn;
            SqlCommand    cmd;

            using (conn = new SqlConnection(GetConnectionString()))
            {
                try
                {
                    string sql = "sp_InsertBookInfo";

                    conn.Open();

                    using (cmd = new SqlCommand(sql, conn))
                    {
                        SqlParameter[] param = new SqlParameter[6];
                        param[0] = new SqlParameter("@Title", System.Data.SqlDbType.VarChar, 30);
                        param[1] = new SqlParameter("@PageCount", System.Data.SqlDbType.Int);
                        param[2] = new SqlParameter("@Price", System.Data.SqlDbType.Decimal);
                        param[3] = new SqlParameter("@FirstName", System.Data.SqlDbType.VarChar, 30);
                        param[4] = new SqlParameter("@LastName", System.Data.SqlDbType.VarChar, 30);
                        param[5] = new SqlParameter("@Name", System.Data.SqlDbType.VarChar, 30);

                        param[0].Value = book.Title;
                        param[1].Value = book.PageCount;
                        param[2].Value = book.Price;
                        param[3].Value = author.FristName;
                        param[4].Value = author.LastName;
                        param[5].Value = type.Name;

                        foreach (SqlParameter p in param)
                        {
                            cmd.Parameters.Add(p);
                        }

                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        return((int)cmd.ExecuteScalar());
                    }
                }
                catch (SqlException ex)
                {
                    return(ex.ErrorCode);
                }
            }
        }
Exemple #3
0
        /*public static int ExecuteInsertAuthor(Author author)
         * {
         *  SqlConnection conn;
         *  SqlCommand cmd;
         *
         *  using (conn = new SqlConnection(GetConnectionString()))
         *  {
         *      try
         *      {
         *          int authorid = 0;
         *          string sql = "INSERT INTO Author (FirstName, LastName) VALUES (@FirstName, @LastName)";
         *          //string sql = "sp_InsertBookInfo";
         *
         *          conn.Open();
         *
         *          using (cmd = new SqlCommand(sql, conn))
         *          {
         *
         *              SqlParameter[] param = new SqlParameter[2];
         *              param[0] = new SqlParameter("@FirstName", System.Data.SqlDbType.VarChar, 20);
         *              param[1] = new SqlParameter("@LastName", System.Data.SqlDbType.VarChar, 20);
         *
         *              param[0].Value = author.FirstName;
         *              param[1].Value = author.LastName;
         *
         *              foreach (SqlParameter p in param)
         *              {
         *                  cmd.Parameters.Add(p);
         *              }
         *
         *              cmd.CommandType = System.Data.CommandType.Text;
         *              authorid = (int)cmd.ExecuteScalar();
         *              return (int)authorid;
         *          }
         *      }
         *      catch (SqlException ex)
         *      {
         *          return ex.ErrorCode;
         *      }
         *  }
         * }*/

        public static int ExecuteInsertType(BusinessLayer.Type type)
        {
            int    typeid = 0;
            string sql    = "INSERT INTO Type (Name) VALUES (@Name)" +
                            "SELECT COUNT(*) FROM Type";

            using (SqlConnection conn = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add("@Name", SqlDbType.VarChar, 20);
                cmd.Parameters["@Name"].Value = type.Name;
                try
                {
                    conn.Open();
                    typeid = (int)cmd.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            return(typeid);
        }