Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I want to bind a image in file uploader control which stored in folder and in database only a path of that particular image.
Posted
Updated 14-Jun-16 19:39pm
v2
Comments
CHill60 29-Jan-15 6:36am    
AFAIK The file upload control requires the user to select a file (for security reasons). What are you actually trying to do? Is it that you want to store the selected image on a folder with it's path in a database OR you have an image path in a database and you want to display that image?

1 solution

ASP form code
ASP.NET
<div align="center">
        <form id="frmUpload" runat="server">
            <h2>Save Book Details</h2><br />
            Book Name   : &nbsp; <asp:TextBox runat="server" ID="txtBookname" Text=""></asp:TextBox><br /><br />
            Choose File : &nbsp; <asp:FileUpload runat="server" ID="fupPdfBrowse"/><br /><br />
            <asp:Button runat="server" ID="btnSave" text="Upload" onclick="btnSave_Click" /><br /><br />
            <asp:Label runat="server" ID="lblStatus" text="Click on Browsre to Select File and Enter Book Name then Click Save." />
        </form>
</div>


C# Code
Include this two
C#
using System.IO;
using System.Data.SqlClient;


button Save Click event
C#
protected void btnSave_Click(object sender, EventArgs e)
    {
        if (fupPdfBrowse.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(fupPdfBrowse.FileName);
                fupPdfBrowse.SaveAs(Server.MapPath("~/") + filename);
                lblStatus.Text = "File Uploaded";
                SaveBookDetails(txtBookname.Text, filename);
                
            }
            catch (Exception ex)
            {
                lblStatus.Text = "The file could not be uploaded.Exception : " + ex.Message;
            }
        }
    }



method to Save in Database

C#
private void SaveBookDetails(string strBookName, string strFilename)
    {
        try
        {
            string strCmd = "INSERT INTO book (name,filename) VALUES (@name, @filename)";
            string strCon = "your connection string";
            using (SqlConnection con = new SqlConnection(strCon))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = strCmd;
                    cmd.Parameters.AddWithValue("@name", strBookName);
                    cmd.Parameters.AddWithValue("@filename", strFilename);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    lblStatus.Text = "File Saved & uploaded";
                }
            }
        }
        catch (Exception ex)
        {
            lblStatus.Text = "Failed to Save book details in Database. Exception : " + ex.Message.ToString();
        }
    }
 
Share this answer
 
v2

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