-4

I am trying to insert an image into a database table, but after choosing an image, the uploaded image is shown as NULL in the table. Help me to upload image correctly into the database.

Here is my code for inserting data into the database table:

protected void addProduct_Click(object sender, EventArgs e)
{
    using (TradersRehmanEntities db = new TradersRehmanEntities())
    {
        tblProduct ad = new tblProduct();
        ad.ProductName = txtProducttName.Text;
        ad.ProductPrice = txtSalePrice.Text;
        ad.SalePrice = txtSalePrice.Text;
        ad.StockAvaliable = txtStock.Text;
        ad.CategoriesId = Convert.ToInt32(catergoriesDDL.SelectedValue);

        ad.ProductImage = flImageUpload.SaveAs('ProductImage' + flImageUpload.FileName);

        db.tblProducts.Add(ad);

        db.SaveChanges();

        Response.Write("Product added successfully");
    }
} 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Well, what does the `flImageUpload.SaveAs()` method return?? Does it return the actual image bytes as a byte array? Or just some status code?? Since we don't know what `flImageUpload` really is - how can we even begin to help ?? – marc_s Mar 29 '20 at 08:10
  • @marc_s sir flImageUpload is an ID of FileUpload Control – Usama Hafeez Official Mar 29 '20 at 08:28
  • 1
    So are you using ASP.NET **webforms** ? Then why do you have an `asp.net-mvc` tag ?!?!? That's just confusing readers.... And if you're using ASP.NET webforms - the `FileUpload.SaveAs()` method doesn't return anything - it's a `void` method.... – marc_s Mar 29 '20 at 08:35
  • @marc_s yes sir am using ASP.NET webforms. FileUpload,SaveAs() doesn't returns any thing. sir plz help to find a proper way to upload image on DB sql sever. – Usama Hafeez Official Mar 29 '20 at 08:44
  • Updated your tags - removed the **wrong** `asp.net-mvc`, added `webforms`. Also: **search** SO !! There are **plenty** of questions (and answers) already that deal with storing images in SQL Server - e.g. https://stackoverflow.com/questions/6333553/problem-with-storing-images-in-sql-server or https://stackoverflow.com/questions/60834490/store-and-view-image-in-sql-server-using-asp-net-c-sharp or **many more!** – marc_s Mar 29 '20 at 10:09

2 Answers2

0

Try this - grab the uploaded file's byte array from the .FileBytes property of the FileUpload control:

protected void addProduct_Click(object sender, EventArgs e)
{
    using (TradersRehmanEntities db = new TradersRehmanEntities())
    {
        tblProduct ad = new tblProduct();
        ad.ProductName = txtProducttName.Text;
        ad.ProductPrice = txtSalePrice.Text;
        ad.SalePrice = txtSalePrice.Text;
        ad.StockAvaliable = txtStock.Text;
        ad.CategoriesId = Convert.ToInt32(catergoriesDDL.SelectedValue);

        // grab the file's byte array from the FileUpload control    
        ad.ProductImage = flImageUpload.FileBytes();

        db.tblProducts.Add(ad);

        db.SaveChanges();

        Response.Write("Product added successfully");
    }
} 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
-1

Try to save it as a blob. But it is better to save it in an object store (e.g amazon S3) and keep a reference to it in the db

Or Chen
  • 379
  • 3
  • 5