Click here to Skip to main content
15,887,345 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Everyone...
I am new to MVC and i want to insert image into database but does find a reliable solution that how i can do it.. i have searched a lot but i did not found any answer that solve my issue.. i do not understand where i have to start, kindly help to get out of it thankx in advance

What I have tried:

Here is my "Product" Model
public class Products
  {
      [Required]
      public int P_ID { get; set; }
      [Required]
      public string P_Name { get; set; }
      [Required]
      public string P_Image { get; set; }
      [Required]
      public int P_Price { get; set; }
      [Required]
      public int P_CatedoryID { get; set; }
  }


My Controller:
[HttpPost]
     [ValidateAntiForgeryToken]
     public ActionResult CreateProductsByAjaxCall(Products product)
     {
         if (ModelState.IsValid)
         {
             db.CreateProducts(product);
         }
         return RedirectToAction("Index");
     }


My DB Function
public void CreateProducts(Products product)
 {
     string query = "Insert into Products (P_Name,P_Image,P_Price,P_CatagoryID) Values (@name,@image,@price,@category)";
     SqlConnection con = new SqlConnection(con_string);
     con.Open();
     SqlCommand cmd = new SqlCommand(query,con);
     cmd.Parameters.AddWithValue("@name",product.P_Name);
     cmd.Parameters.AddWithValue("@image", product.P_Image);
     cmd.Parameters.AddWithValue("@price",product.P_Price);
     cmd.Parameters.AddWithValue("@category", product.P_CatedoryID);
     cmd.ExecuteNonQuery();
     con.Close();
 }
Posted
Updated 16-Mar-17 20:46pm
Comments
Sunasara Imdadhusen 17-Mar-17 2:33am    
Do you want to insert image or path only in DB?
Muhammd Aamir 17-Mar-17 2:36am    
i want to insert image not its path
Kats2512 17-Mar-17 3:06am    
not the best of plans to store the blob image in the database, you rather store the file location of the image.
Muhammd Aamir 17-Mar-17 3:20am    
thankx Kats2512 my project is not at big deal i just want to store 10 to 15 images only just for my practices in mvc
F-ES Sitecore 17-Mar-17 5:16am    
You have two issues really that you need to break down separately. One is storing an image in the database and if you google you'll easily find lots of examples on how to do this. The second is that you appear to be submitting the data via ajax which makes handling files different. Chances are the code you're using (but haven't posted) won't support file uploads so you'll need to google how to upload a file via ajax also.

you will refer this link... Click here
 
Share this answer
 
You can refer following article Upload/Download Image in/from Database MVC[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900