Example #1
0
        public ActionResult Detail(int id, ExpenseDetail expenseDetail)
        {
            try
            {
                ExpenseManager expenseManager = new ExpenseManager();
                expenseDetail.ExpenseSummaryID = id;
                int value = expenseManager.CreateExpenseDetail(expenseDetail);
                ////create path to store in database
                //// expenseDetail.user_image = "~/image/" + expenseDetail.expenseAttachment.FileName;
                //var t = Server.MapPath("Images") + "/" + expenseDetail.expenseAttachment.FileName;
                ////store image in folder
                //expenseDetail.expenseAttachment.SaveAs(Server.MapPath("~/Images") + "/" + expenseDetail.expenseAttachment.FileName);
                byte[] bytes;
                if (expenseDetail.expenseAttachment != null)
                {
                    using (BinaryReader br = new BinaryReader(expenseDetail.expenseAttachment.InputStream))
                    {
                        bytes = br.ReadBytes(expenseDetail.expenseAttachment.ContentLength);
                    }
                    string constr = ConfigurationManager.AppSettings["PayMe-Connectionstring"];
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        string query = "INSERT INTO ExpenseDetailDocument VALUES (@ExpenseDetailID,@Name, @ContentType, @Data)";
                        using (SqlCommand cmd = new SqlCommand(query))
                        {
                            cmd.Connection = con;
                            cmd.Parameters.AddWithValue("@ExpenseDetailID", value);
                            cmd.Parameters.AddWithValue("@Name", Path.GetFileName(expenseDetail.expenseAttachment.FileName));
                            cmd.Parameters.AddWithValue("@ContentType", expenseDetail.expenseAttachment.ContentType);
                            cmd.Parameters.AddWithValue("@Data", bytes);

                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                    }
                }
                TempData["Message"] = "Expense Detail Created Successfully";

                return(RedirectToAction("Detail/" + id));
            }
            catch (Exception ex)
            {
                TempData["Message"] = "Error Occured";
                return(View());
            }
        }