Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

previous asyncfileupload.filename Inserted when adding news without Image

$
0
0

hi,

I have an asyncfileupload, two textboxes for title and content and one button for inserting news.

when user choose an Image and click insert button every thing is ok

but

when she/he want to insert secend news witout Image, the previous Image name inserted in database(although I expect nopic.png to be inserted for Image name in table)

steps:

1. User Uploads Image for news If she/he wants.

2. User fills textboxes

3. storing asyncfileupload.filename in session.

4. empty session in insert button click event after inserting

please download below files and let me know what's my wrong? what's the problem?

codes:

http://www.4shared.com/rar/lFkUhWqj/asyncproblem.html

database script

http://www.4shared.com/document/Mua0yrID/asyncproblem.html

also you can see below codes:

DALBase MyDALBase = new DALBase();
    MyClasses MyClasses = new MyClasses();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        Insertnews();

        
    }
   


    
    public void Insertnews()
    {

        try
        {
            string filename = "nopic.png";
            if (Session["picname"]!=null)
            {
                string tempfilename = Session["picname"].ToString();
                string tempPath = Server.MapPath("~/ImagesTemp");
                string targetpath = Server.MapPath("~/Images");
                filename = MyClasses.setfilename(tempfilename, tempPath);

                using (WebClient client = new WebClient())
                {
                    client.DownloadFile(tempPath + "/" + tempfilename, targetpath + "/" + filename);
                }
                string FileToDelete;
                // Set full path to file
                FileToDelete = Server.MapPath("~/ImagesTemp/" + tempfilename);
                // Delete a file
                File.Delete(FileToDelete);
            }

          


            MyDALBase.ExecuteScaler(System.Data.CommandType.StoredProcedure, "InsertNews", new SqlParameter[]{
        new SqlParameter("@Title",txt_title.Text),
            new SqlParameter("@Contents",txtbody.Text),
            new SqlParameter("@Pic",filename)
        });

            
            txt_title.Text = string.Empty;
            txtbody.Text = string.Empty;
            
            Session["picname"] = null;
            Page.ClientScript.RegisterStartupScript(GetType(), "Javascript", "<script>alert('خبر با موفقيت ثبت شد')</script>");
        }
        catch (Exception)
        {
            //throw;
            Page.ClientScript.RegisterStartupScript(GetType(), "Javascript", "<script>alert('عدم موفقيت')</script>");

        }

    }
    protected string UploadFolderPath = "~/ImagesTemp/";
    protected void FileUploadComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
            string filename = "";

            if (AsyncFileUpload1.HasFile)
            {
                filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
                AsyncFileUpload1.SaveAs(
                    Server.MapPath(this.UploadFolderPath) + filename);

                ClearContents(sender as Control);
                
            }
           
                Session["picname"] = filename;

               
    
       
        
       
    }
    private void ClearContents(Control control)
    {
        for (var i = 0; i < Session.Keys.Count; i++)
        {
            if (Session.Keys[i].Contains(control.ClientID))
            {
                Session.Remove(Session.Keys[i]);
                break;
            }
        }
    }


Thanks


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>