Hi,
The site I am working on have two pages, one for inserting staff data (sql db) and one page for upload of file (picure).
Some of the file upload page:
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button id="UploadBtn" Text="Upload File" OnClick="UploadBtn_Click" runat="server" Width="105px" />
The code behind look like this:
Protected Sub UploadBtn_Click(sender As Object, e As EventArgs)
If FileUpLoad1.HasFile Then
FileUpload1.SaveAs("<path to folder>" + FileUpload1.FileName)
Label1.Text = "File Uploaded (remember to include this filename on the Staff Data): " + FileUpload1.FileName
Else
Label1.Text = "No File Uploaded."
End If
End Sub
The staff data insert page consist of a Formview, and once the insert button is cliked the data is inserted and rediredted to a confirmation page (that returns a record number).
Parts of the insert page code:
<asp:FormView ID="FormView2" runat="server" DataKeyNames="RecordNo"
DataSourceID="StaffData" DefaultMode="Insert" OnItemInserted="FormView2_ItemInserted" HorizontalAlign="Center">
Code behind page:
Protected Sub FormView2_ItemInserted(sender As Object, e As FormViewInsertedEventArgs)
Response.Clear()
Response.Redirect("confirmpage.aspx")
Response.[End]()
End Sub
I am trying to combine these into one page, i.e. data is inserted and picture uploaded when the insert button is cliked. Further, the selectec picture file name must be inserted in the db as well (PictureFileName).
I have tried a lot, but cannot make it work - anybody out there can help me ?
Best regards
You shouldn't have any problems with what you are trying to do, is your FormView inside of an update panel? Can you provide a sample of what you have tried so far so we can make it work?
You shouldn't have any problems with what you are trying to do, is your FormView inside of an update panel? Can you provide a sample of what you have tried so far so we can make it work?
Thanks,
The Formview is in insert mode as default, for the user to insert the data.
Yes, it works for now, but I want to combine the two functions (insert data and upload) into one page - and one button. I cannot figure out how to set the code, I think the sequence must be something like this:
1. upload file
2 insert data from the Formview, including the file name from the upload textbox
3. redirect to other page
I know it is possible, I am just not clever enough to figure out how :-)
It inserts file name in to Database when a file uploaded using a FileUpload control,
Thanks,
I have been looking at your solution; I think I understand - it looks partly what I am looking for. As far as I can see, it will create two buttons ? I need to have it all together, like this:
1. upload file
2 insert data from the Formview, including the file name from the upload textbox
3. redirect to other page
I think I have the building blocks (except insert of filename of uploaded file), just how do I get it to work together ?
No. When You click Insert Button both happens. Upload and inserting file name to DB along with other values.
basheerkal is correct.
When you click your insert button it fires a postback event. During the post back event the entire page is sent back to the server for processing. If a file was also selected in the FileUpload control then it will be posted to the server as well along with
the page at the exact same time.
If you want to handle it your way though one thing you could do is hook up to the FormViews
Inserting event, (not to be confused with Inserted). You can then handle the SaveAs() method in that event, which will handle the upload and saving
beforethe FormView actually inserts it's data.
ricas
Member
151 Points
339 Posts
Combine data insert and file upload into one page
Apr 23, 2012 09:02 PM|LINK
Hi,
The site I am working on have two pages, one for inserting staff data (sql db) and one page for upload of file (picure).
Some of the file upload page:
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button id="UploadBtn" Text="Upload File" OnClick="UploadBtn_Click" runat="server" Width="105px" />
The code behind look like this:
Protected Sub UploadBtn_Click(sender As Object, e As EventArgs)
If FileUpLoad1.HasFile Then
FileUpload1.SaveAs("<path to folder>" + FileUpload1.FileName)
Label1.Text = "File Uploaded (remember to include this filename on the Staff Data): " + FileUpload1.FileName
Else
Label1.Text = "No File Uploaded."
End If
End Sub
The staff data insert page consist of a Formview, and once the insert button is cliked the data is inserted and rediredted to a confirmation page (that returns a record number).
Parts of the insert page code:
<asp:FormView ID="FormView2" runat="server" DataKeyNames="RecordNo"
DataSourceID="StaffData" DefaultMode="Insert" OnItemInserted="FormView2_ItemInserted" HorizontalAlign="Center">
Code behind page:
Protected Sub FormView2_ItemInserted(sender As Object, e As FormViewInsertedEventArgs)
Response.Clear()
Response.Redirect("confirmpage.aspx")
Response.[End]()
End Sub
I am trying to combine these into one page, i.e. data is inserted and picture uploaded when the insert button is cliked. Further, the selectec picture file name must be inserted in the db as well (PictureFileName).
I have tried a lot, but cannot make it work - anybody out there can help me ?
Best regards
..... I am new to all this, so please be patient
N_EvilScott
Star
8179 Points
1466 Posts
Re: Combine data insert and file upload into one page
Apr 23, 2012 11:57 PM|LINK
You shouldn't have any problems with what you are trying to do, is your FormView inside of an update panel? Can you provide a sample of what you have tried so far so we can make it work?
basheerkal
Star
10672 Points
2426 Posts
Re: Combine data insert and file upload into one page
Apr 24, 2012 02:15 AM|LINK
Hi ricas
Please Check the answer post in this thread. It deals with what exactly you need,
http://forums.asp.net/t/1781297.aspx/2/10?How+to+save+the+filename+from+an+FileUpload+control+on+a+FormView+
It inserts file name in to Database when a file uploaded using a FileUpload control,
(Talk less..Work more)
ricas
Member
151 Points
339 Posts
Re: Combine data insert and file upload into one page
Apr 24, 2012 05:59 AM|LINK
Thanks,
The Formview is in insert mode as default, for the user to insert the data.
Yes, it works for now, but I want to combine the two functions (insert data and upload) into one page - and one button. I cannot figure out how to set the code, I think the sequence must be something like this:
1. upload file
2 insert data from the Formview, including the file name from the upload textbox
3. redirect to other page
I know it is possible, I am just not clever enough to figure out how :-)
Best regards
..... I am new to all this, so please be patient
basheerkal
Star
10672 Points
2426 Posts
Re: Combine data insert and file upload into one page
Apr 24, 2012 06:06 AM|LINK
You have not checked the link I suggested?
here is the code
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e) { FileUpload fu = (FileUpload)(FormView1.FindControl("FileUpload1")); if(fu.HasFile==true) { e.Values["filename"] =fu.FileName; fu.SaveAs(Server.MapPath("~/"+fu.FileName)); Response.Write(fu.FileName +" Uploaded"); } } <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" InsertCommand="INSERT INTO filetable (filename) VALUES (@FileName)" SelectCommand="SELECT filetable.* FROM filetable"> <InsertParameters> <asp:Parameter Name="FileName" /> </InsertParameters> </asp:SqlDataSource> <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" oniteminserting="FormView1_ItemInserting" AllowPaging="True" BorderColor="#009900" BorderStyle="Solid"> <InsertItemTemplate> Select File: <asp:FileUpload ID="FileUpload1" runat="server" /> <br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" /> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </InsertItemTemplate> <ItemTemplate> Id: <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' /> <br /> File: <asp:Label ID="filenameLabel" runat="server" Text='<%# Bind("FileName") %>'/> <br /> <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" /> </ItemTemplate> </asp:FormView>(Talk less..Work more)
ricas
Member
151 Points
339 Posts
Re: Combine data insert and file upload into one page
Apr 24, 2012 06:09 AM|LINK
Thanks,
I have been looking at your solution; I think I understand - it looks partly what I am looking for. As far as I can see, it will create two buttons ? I need to have it all together, like this:
1. upload file
2 insert data from the Formview, including the file name from the upload textbox
3. redirect to other page
I think I have the building blocks (except insert of filename of uploaded file), just how do I get it to work together ?
Best regards
..... I am new to all this, so please be patient
basheerkal
Star
10672 Points
2426 Posts
Re: Combine data insert and file upload into one page
Apr 24, 2012 06:19 AM|LINK
No. When You click Insert Button both happens. Upload and inserting file name to DB along with other values.
(Talk less..Work more)
N_EvilScott
Star
8179 Points
1466 Posts
Re: Combine data insert and file upload into one page
Apr 24, 2012 09:35 PM|LINK
basheerkal is correct.
When you click your insert button it fires a postback event. During the post back event the entire page is sent back to the server for processing. If a file was also selected in the FileUpload control then it will be posted to the server as well along with the page at the exact same time.
If you want to handle it your way though one thing you could do is hook up to the FormViews Inserting event, (not to be confused with Inserted). You can then handle the SaveAs() method in that event, which will handle the upload and saving before the FormView actually inserts it's data.
ricas
Member
151 Points
339 Posts
Re: Combine data insert and file upload into one page
Apr 25, 2012 06:35 AM|LINK
Thanks,
I have been working hard to ge it to work, but no joy yet :-(
I have altered the Formview to include:
<asp:FormView ID="FormView2" runat="server" DataKeyNames="RecordNo"
DataSourceID="" DefaultMode="Insert" OnItemInserted="FormView2_ItemInserting" HorizontalAlign="Center">
And added the File upload:
<td class="style16" colspan="3">
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
I know something is missing here, but cannot figure out how to use your code to work here ?
The Code behind page look like this:
Protected Sub FormView2_ItemInserting(sender As Object, e As FormViewInsertEventArgs)
Dim fu As FileUpload = DirectCast(FormView2.FindControl("FileUpload1"), FileUpload)
If fu.HasFile = True Then
e.Values("PictureFilename") = fu.PictureFileName
fu.SaveAs("<path to folder>" + FileUpload1.PictureFileName)
Response.Clear()
Response.Redirect("confirmpage.aspx")
Response.[End]()
End If
End Sub
How do I proceed from here ?
Best regards
..... I am new to all this, so please be patient
ricas
Member
151 Points
339 Posts
Re: Combine data insert and file upload into one page
Apr 25, 2012 06:37 AM|LINK
@ Scott,
Sorry if I was misleading - no, I do want it all to take place in one instant !
best regards
..... I am new to all this, so please be patient