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