Click here to Skip to main content
15,890,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i upload a new image after choosing file and immediately replace the old image?

this is my current code

C#
public partial class AdminEditProduct : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.Count != 0)
        {

            int ProductID = Convert.ToInt32(Request.QueryString["ProductID"]);
             

            if (!IsPostBack)
            {
                string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection myConnect = new SqlConnection(strConnectionString);
                SqlCommand cmd = new SqlCommand("SELECT ProductID, ProductName, Price, Description, Sdesc, Picture FROM Product WHERE ProductID=" + ProductID, myConnect);

               
                myConnect.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {

                    txtProductName.Text = reader["ProductName"].ToString();
                    txtPrice.Text = reader["Price"].ToString();
                    txtDescription.Text = reader["Description"].ToString();
                    txtSdesc.Text = reader["Sdesc"].ToString();
                    Session["picURL"] =  reader["Picture"].ToString();
                    Image1.ImageUrl = "~/image/" + Session["picURL"].ToString();
                }
                reader.Close();
                myConnect.Close();
            }
        }
    }



    protected void ImageURL(object sender, EventArgs e)
    {
        {
            string picture = Session["Picture"].ToString();
            if ((string.IsNullOrEmpty(picture)))
            {

                string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection myConnect = new SqlConnection(strConnectionString);
                myConnect.Open();

                string strCommandText = "SELECT (Picture) FROM Product";
                SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
                SqlDataReader reader = cmd.ExecuteReader();

                myConnect.Close();

            }
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        Image1.ImageUrl = null;
        EditDescription();

    }

    private void EditDescription()
    {
        try
        {
            int ProductID = Convert.ToInt32(Request.QueryString["ProductID"]);
            string Description = txtDescription.Text;

            string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection myConnect = new SqlConnection(strConnectionString);

            string strCommandText = ("UPDATE Product " + " SET ProductName=@aProductName " + " , Price=@aPrice " + " , Description=@aDescription " + " , Sdesc=@aSdesc " + " , Picture=@aPicture " + "  WHERE ProductID=@aProductID ");

            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

            cmd.Parameters.Add("@aProductID", SqlDbType.Int).Value = ProductID;
            cmd.Parameters.AddWithValue("@aProductName", txtProductName.Text);
            cmd.Parameters.AddWithValue("@aPrice", Convert.ToDecimal(txtPrice.Text));
            cmd.Parameters.AddWithValue("@aDescription", txtDescription.Text);
            cmd.Parameters.AddWithValue("@aSdesc", txtSdesc.Text);
            if (FileUpload1.FileName != "")
            {
                Session["picURL"] = FileUpload1.FileName;
            }
            cmd.Parameters.AddWithValue("@aPicture", Session["picURL"]);

            myConnect.Open();


            int result = cmd.ExecuteNonQuery();

            myConnect.Close();
            
        }
        catch (Exception e)
        
}
Posted
Comments
asbis 7-Jan-14 3:38am    
When u select any image you want to display Pre-View of that image
Is that you looking for ??
shiiny 7-Jan-14 3:45am    
yup

1 solution

Hi Please try this this work
XML
<style type="text/css">
       #imagePreview
       {
           width: 60px;
           height: 60px;
           border: 0px solid;
           float: left;
           filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);
       }
   </style>
   <script type="text/javascript">
       var loadImageFile = (function () {
           if (window.FileReader) {
               var oPreviewImg = null, oFReader = new window.FileReader(),
           rFilter = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ief|image\/jpeg|image\/jpeg|image\/jpeg|image\/pipeg|image\/png|image\/svg\+xml|image\/tiff|image\/x\-cmu\-raster|image\/x\-cmx|image\/x\-icon|image\/x\-portable\-anymap|image\/x\-portable\-bitmap|image\/x\-portable\-graymap|image\/x\-portable\-pixmap|image\/x\-rgb|image\/x\-xbitmap|image\/x\-xpixmap|image\/x\-xwindowdump)$/i;

               oFReader.onload = function (oFREvent) {
                   if (!oPreviewImg) {
                       var newPreview = document.getElementById("imagePreview");
                       oPreviewImg = new Image();
                       oPreviewImg.style.width = (newPreview.offsetWidth).toString() + "px";
                       //   oPreviewImg.style.height = (newPreview.offsetHeight).toString() + "px";
                       newPreview.appendChild(oPreviewImg);
                   }
                   oPreviewImg.src = oFREvent.target.result;
               };
               return function () {
                   var aFiles = document.getElementById("fileUpload").files;
                   if (aFiles.length === 0) { return; }
                   if (!rFilter.test(aFiles[0].type)) { alert("You must select a valid image file!"); return; }
                   oFReader.readAsDataURL(aFiles[0]);
               }
           }
           if (navigator.appName === "Microsoft Internet Explorer") {
               return function () {
                   document.getElementById("imagePreview").filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = document.getElementById("fileUpload").value;
               }
           }
       })();
   </script>
<input id="fileUpload" type="file" name="fileUpload" onchange="loadImageFile(); " />
<span id="imagePreview"></span>


Please mark as answer if it works for you
 
Share this answer
 

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