Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys! I'v been trying to upload six images using ajaxfileupload in asp.net to MSSQL server but it always save only one image. Kindly help me on solving this one. You can also provide better alternatives on achieving the same goal.

What I have tried:

Here is the aspx code:

C#
<div class="divTableBody">
                  <div class="divTableRow">
                     <div class="divTableCell">Thumbnail Images:
                       </div>
                        <div class="divTableCell"> 
                           <asp:AjaxFileUpload ID="itemFileUpload3" 
                           runat="server" 
                           OnUploadComplete="itemImage3OnUpload" 
                           MaximumNumberOfFiles="10" Width="420px"/>
                           <p  id="itemImage3Validate"></p> 
                       </div>
                  </div>
              </div>   


And here is the aspx.cs code:

C#
protected void itemImage3OnUpload(object sender, AjaxFileUploadEventArgs 
      e)
    {

    string filename = e.FileName;
    Session["PicturePath3"] = filename;
    if (itemType1.Checked)
    {
        itemFileUpload3.SaveAs(Server.MapPath("~/Images/Sub Images/") +  
   filename);
    }
    else if (itemType2.Checked)
    {
        itemFileUpload3.SaveAs(Server.MapPath("~/Images/Sub Images/") + 
   filename);
    }

}

   protected void itemSaveButton_Click(object sender, EventArgs e)
   {

    try
    {

        string itemThumbImage = Session["PicturePath3"].ToString();


        int itemInstrumentID = 
       ConnectionClassGuitarItems.GetItemIDByNameAndModel
             (item_brandId,item_model);

        var subImg = new thumbnailImage
        {
            instrumentId = itemInstrumentID,
            subimages = itemThumbImage
        };


        ConnectionClassGuitarItems.AddThumnailImage(subImg);
        ScriptManager.RegisterStartupScript(this.Page,this.GetType()
       , "msgboxScc", "btnClickSuccess();", true);

        ClearTextFields2();

    }
    catch (Exception)
    {
        ScriptManager.RegisterStartupScript(this.Page, this.GetType()
     , "msgboxErr", "btnClickFail();", true);
    }


   } 


And then I will add my images using this code:

C#
public static void AddThumnailImage(thumbnailImage subImg)
{
    using (MusicStoreDBEntities obj = new MusicStoreDBEntities())
    {

        obj.thumbnailImages.Add(subImg);
        obj.SaveChanges();
    }
}
Posted
Updated 29-Apr-18 7:48am

UploadedComplete is the event on the server that you should be listen to:
Quote:
Raised on the server when a file is uploaded successfully. In this event an instance of AjaxFileUploadEventArgs is passed in the argument that contains file name, size and content type.

More information about the control's events here: AjaxFileUpload example to upload multiple files - CodingFusion[^]

NOTE: Google Search is your friend - lots of information out there... I'm a MVC programmer, so had to look this up. The information above was found using this: asp.net ajaxfileupload example - Google Search[^]
 
Share this answer
 
Comments
BebeSaiyan 17-Sep-17 6:56am    
Thanks but is there a difference between OnUploadComplete and UploadedComplete?
Graeme_Grant 17-Sep-17 9:32am    
Check the link above
please try below code

protected void ajaxUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
id += 1;
string filename = Path.GetFileName(e.FileName);
string filepath = Server.MapPath("~/Images/Gallery/" + filename);
ajaxUpload1.SaveAs(filepath);

string Insert = "Insert into slider (slid,slurl) values (" + @id + ",'" + @IMAGE_PATH + "')";
SqlCommand cmd = new SqlCommand(Insert, con);
cmd.CommandType = CommandType.Text;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
cmd.Dispose();
}
}
 
Share this answer
 
Comments
Richard Deeming 30-Apr-18 12:27pm    
Unformatted; incomplete; possible SQL Injection[^] vulnerability; and not even attempting to answer the question.

(Not to mention, almost eight months late.)

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