Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to have a function that runs on server side but does not do a post back. So i have come up with the following code. The problem is that the code executes fine until it gets to the String dir = Server.MapPath("files/News/" + FileUpload1.PostedFile.FileName), im not sure why this is happing and i dont know how i can fix this so it also uploads the file.

Code Behind
C#
protected void btn_Click(object sender, EventArgs e)
    {
        String name = TextBox1.Text;
        String desc = TextBox2.Text;

//CODE STOPS HERE, DOES NOT EXECUTE PASS THIS POINT
        String dir = Server.MapPath("files/News/" + FileUpload1.PostedFile.FileName);
        FileUpload1.PostedFile.SaveAs(dir);
        String SQL = String.Format(" INSERT INTO SF_news "
             + " (newsHeader, newsDescription, newsSrc, newsDate, newsYear) "
             + " VALUES "
             + " ('{0}', '{1}', '{2}', '{3}')",
             name, desc, FileUpload1.PostedFile.FileName);                                      
        new DBHelper().runSQL(SQL);
    }



HTML Code
JavaScript
<asp:ScriptManager ID="ScriptManager2" runat="server"></asp:ScriptManager>
 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
     <table>
       <tr id="hiddenForm" style="visibility:hidden;">
         <td colspan="4">
           <div>
<span>News Header: </span><span><asp:TextBox ID="TextBox1"runat="server"> </asp:TextBox></span>
<span>News Description: </span><span><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></span>
             <span>News File: </span><span>
<dx:ASPxUploadControl ID="FileUpload1"  runat="server" UploadMod="auto" Width="280px"></dx:ASPxUploadControl></span>
            <div>  
            <asp:Button ID="btn" runat="server" Text="Button" OnClick="btn_Click" />
    <input type="button" id="jsBtn" />
           </div>    
        </div>
       </td>
     </tr>
   </table> 
 </ContentTemplate>
</asp:UpdatePanel>
Posted
Comments
ZurdoDev 8-Apr-15 10:59am    
Your code has to postback to send the file. What exactly is the issue?
Member 10395722 8-Apr-15 11:33am    
the problem is that is doesnt upload and that the code just stops doing anything. Did you not read the title of this question?? i want to do an upload WITH OUT A POST BACK
Richard Deeming 8-Apr-15 15:15pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Member 10395722 9-Apr-15 5:40am    
Thank you for the tip, this is however the only way i know how to do it, can you give me an example of what you mean?
Richard Deeming 9-Apr-15 7:09am    
Parametrized SQL statement - C#[^]

If your custom DBHelper class doesn't provide a way to pass parameters, then you'll either need to ditch it, or get it updated to support parameterized queries.

File upload is the process which will not work within an Update Panel.

And here in your code your are actually post backing the data, but as you add an update panel so the part of the page is just refreshing internally. But remember its actually post backing the data.

Solution 1: You can use a trigger to perform the operation.
XML
<Triggers>
       <asp:PostBackTrigger ControlID="btn" />
</Triggers>

http://dotnet4europeanhosting.hostforlife.eu/post/European-ASPNET-Hosting-Amsterdam-Tips-to-Fix-FileUpload-Control-is-not-Working-in-UpdatePanel.aspx[^]

Solution 2: You can use AsyncFileUpload from Ajax Control Tool kit
http://www.aspsnippets.com/Articles/Perform-AJAX-Asynchronous-File-Upload-and-upload-files-asynchronously-to-server-in-ASPNet.aspx[^]

And add your sql code to insert it into db.
 
Share this answer
 
File Upload does not work well with UpdatePanel. I would suggest you to have a ashx handler and call that from jQuery Ajax and inside the handler method, do all the task.
 
Share this answer
 
"FileUpload1.PostedFile" as the name says, requires a postback. However making it Async using the AsyncFileUpload control as described above would solve your issue
 
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