Click here to Skip to main content
15,887,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am new to Asp.net MVC i want to store the image name into database and my image into file system everything works fine but the name of the image store into database like this "
System.Web.HttpPostedFileWrapper
"
please help me to get out of this problem thanks in advance ...

What I have tried:

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; }
}


Contoller:
public ActionResult CreateProductsByAjaxCall(Products p)
   {
       if (Request.Files.Count > 0)
       {
           var file = Request.Files[0];
           if (file != null && file.ContentLength > 0)
           {
               var fileName = Path.GetFileName(file.FileName);
               var path = Path.Combine(Server.MapPath("~/misc/"), fileName);
               file.SaveAs(path);
               db.CreateProducts(p);

           }

       }
       return RedirectToAction("Index");
   }


My Database 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 4-Apr-17 21:29pm
Comments
Karthik_Mahalingam 5-Apr-17 3:16am    
what is the value you are getting in fileName ?
var fileName = Path.GetFileName(file.FileName);
Muhammd Aamir 5-Apr-17 3:27am    
@Karthik i get "flowers.jpg" in var fileName, its correct here
Karthik_Mahalingam 5-Apr-17 3:30am    
then you shall save the same
Muhammd Aamir 5-Apr-17 3:29am    
and when i come to db.CreateProducts(p); the value in p, P_Image is System.Web.HttpPostedFileWrapper here is the problem
Karthik_Mahalingam 5-Apr-17 3:36am    
what do you need to save in P_Image ?

1 solution

try
var fileName = Path.GetFileName(file.FileName);
              var path = Path.Combine(Server.MapPath("~/misc/"), fileName);
              file.SaveAs(path);
              p.P_Name = fileName;
              db.CreateProducts(p);
 
Share this answer
 
Comments
Muhammd Aamir 5-Apr-17 3:37am    
Great work @Karthik Thank very much for sharing your knowledge now it works p.P_Image = fileName :)
Karthik_Mahalingam 5-Apr-17 3:38am    
cool :)
drcarlosx111 1-Apr-18 16:56pm    
you save my job
thanks you are tha MAN
Karthik_Mahalingam 1-Apr-18 21:15pm    
Welcome

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