Upload and Save Images into database using C# in ASP.Net

sofia
 
on Nov 09, 2021 10:25 PM
Sample_435279.zip
691 Views

hi sir,

i have to browse image in my web page and when image is uplaoded i have to save it on database. 

please help me for doing this task.

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />

 

protected void Upload(object sender, EventArgs e)
{
    byte[] bytes;
    using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream))
    {
        bytes = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
    }
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(constr))
    {
        string sql = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)";
        using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
            cmd.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName));
            cmd.Parameters.AddWithValue("@ContentType", FileUpload1.PostedFile.ContentType);
            cmd.Parameters.AddWithValue("@Data", bytes);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
 
    Response.Redirect(Request.Url.AbsoluteUri);
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Nov 10, 2021 12:31 AM

i have used same code which you shared earlier.

HTML

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<hr />
<asp:GridView ID="gvImages" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Image Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:TemplateField HeaderText="Image">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<div id="dialog" style="display: none">
</div>

Namespaces

using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(constr))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblFiles", conn))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                gvImages.DataSource = dt;
                gvImages.DataBind();
            }
        }
    }
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView dr = (DataRowView)e.Row.DataItem;
        string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["Data"]);
        (e.Row.FindControl("Image1") as Image).ImageUrl = imageUrl;
    }
}

protected void Upload(object sender, EventArgs e)
{
    byte[] bytes;
    using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream))
    {
        bytes = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
    }
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(constr))
    {
        string sql = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)";
        using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
            cmd.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName));
            cmd.Parameters.AddWithValue("@ContentType", FileUpload1.PostedFile.ContentType);
            cmd.Parameters.AddWithValue("@Data", bytes);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}

Screenshot