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