I created insertdatafilelink.aspx to Insert an Uploaded .pdf files to a filed then when I run insertdatafilelink.aspx it inserts all data to all filed and Uploads
the .pdf files to the folder Documents put it doesn't save the .pdf files to the fied file_link. This is the code I used Please I want to know where I made the mistake.
1-This Is the insertdatafilelink.vb
Partial Class Insertdatatfilelink Inherits System.Web.UI.PageProtected Sub New_mail_system_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs) _ Handles New_mail_system.ItemInserting 'Reference the FileUpload controlsDim DocumentUpload As FileUpload = CType(New_mail_system.FindControl("DocumentUpload"), FileUpload) If DocumentUpload.HasFile Then 'Make Sure That a PDF file has been uploadedIf String.Compare(System.IO.Path.GetExtension _(DocumentUpload.FileName),
".pdf", True) <> 0 ThenuploadWarning.text = _
"only PDF documents May Used For A file link documents."UploadWarning.visible =
Truee.Cancel =
True Exit Sub End If Const DocumentDirectory As String = "~/Documents/"Dim DocumentPath As String = DocumentDirectory & DocumentUpload.FileNameDim filenamewithoutextension As String = _System.IO.Path.GetFileNameWithoutExtension(DocumentUpload.FileName)
Dim iteration As Integer = 1While System.IO.File.Exists(Server.MapPath(DocumentPath))DocumentPath = String.Concat(DocumentDirectory, filenamewithoutextension, "-", iteration, ".pdf")iteration += 1
End While 'Save The File To Disk And Set The Value Of The DocumentPath ParameterDocumentUpload.SaveAs(Server.MapPath(DocumentPath))
e.Values("DocumentPath") = DocumentPath End If End SubProtected Sub New_mail_system_ItemInserted(ByVal sender As Object, ByVal e As DetailsViewInsertedEventArgs) _ Handles New_mail_system.ItemInserted If e.Exception IsNot Nothing Then 'need delete document file, if it exists If e.Values("DocumentPath") IsNot Nothing ThenSystem.IO.File.Delete(Server.MapPath(e.Values("DocumentPath").ToString())) End If End IfEnd SubEnd
Class---------------------------------------------------------------------------------------------------------------------
2- And this is the insertdatafilelink.aspx
<
form id="form1" runat="server"> <div><
asp:DetailsView ID="New_mail_system" runat="server" AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="insertdata" Height="50px" Width="249px" DefaultMode="Insert"> <Fields> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="entry_date" HeaderText="entry_date" SortExpression="entry_date" /> <asp:BoundField DataField="reference" HeaderText="reference" SortExpression="reference" /> <asp:BoundField DataField="type" HeaderText="type" SortExpression="type" /> <asp:BoundField DataField="subject" HeaderText="subject" SortExpression="subject" /> <asp:BoundField DataField="dest_from" HeaderText="dest_from" SortExpression="dest_from" /> <asp:BoundField DataField="dest_to" HeaderText="dest_to" SortExpression="dest_to" /> <asp:BoundField DataField="done_by" HeaderText="done_by" SortExpression="done_by" /> <asp:BoundField DataField="comments" HeaderText="comments" SortExpression="comments" /> <asp:TemplateField headertext="Document" SortExpression="file_link"> <InsertItemTemplate> <asp:FileUpload id="DocumentUpload" runat="server" /> </InsertItemTemplate> </asp:TemplateField><asp:CommandField ShowInsertButton="True" /> </Fields> </asp:DetailsView> <p> <asp:Label ID="UploadWarning" runat="server" EnableViewState="false" Visible="false"> </asp:Label> </p>
<asp:SqlDataSource ID="insertdata" runat="server" ConnectionString="<%$ ConnectionStrings:HSCCConnectionString2 %>" DeleteCommand="DELETE FROM [hscc_mail_log] WHERE [ID] = @ID" InsertCommand="INSERT INTO [hscc_mail_log] ([entry_date], [reference], [type], [subject], [dest_from], [dest_to], [done_by], [comments], [file_link]) VALUES (@entry_date, @reference, @type, @subject, @dest_from, @dest_to, @done_by, @comments, @file_link)" SelectCommand="SELECT * FROM [hscc_mail_log]" UpdateCommand="UPDATE [hscc_mail_log] SET [entry_date] = @entry_date, [reference] = @reference, [type] = @type, [subject] = @subject, [dest_from] = @dest_from, [dest_to] = @dest_to, [done_by] = @done_by, [comments] = @comments, [file_link] = @file_link WHERE [ID] = @ID"> <DeleteParameters> <asp:Parameter Name="ID" Type="Int64" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="entry_date" Type="DateTime" /> <asp:Parameter Name="reference" Type="String" /> <asp:Parameter Name="type" Type="String" /> <asp:Parameter Name="subject" Type="String" /> <asp:Parameter Name="dest_from" Type="String" /> <asp:Parameter Name="dest_to" Type="String" /> <asp:Parameter Name="done_by" Type="String" /> <asp:Parameter Name="comments" Type="String" /> <asp:Parameter Name="file_link" Type="String" /> <asp:Parameter Name="ID" Type="Int64" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="entry_date" Type="DateTime" /> <asp:Parameter Name="reference" Type="String" /> <asp:Parameter Name="type" Type="String" /> <asp:Parameter Name="subject" Type="String" /> <asp:Parameter Name="dest_from" Type="String" /> <asp:Parameter Name="dest_to" Type="String" /> <asp:Parameter Name="done_by" Type="String" /> <asp:Parameter Name="comments" Type="String" /> <asp:Parameter Name="file_link" Type="String" /> </InsertParameters> </asp:SqlDataSource> <br />
</div></form>
</
body></
html>
![]() |
0 |
![]() |
Since your directory location is a constant (Const DocumentDirectory As String = "~/Documents/") I would save the fileName to your DB.
DIM fileName AS string = FileUpload.FileName
If everything happens for a reason what is the reason for this error?
![]() |
0 |
![]() |
Hi,
This may helpful to you,
http://www.beansoftware.com/ASP.NET-Tutorials/Binary-Files-To-Database.aspx
Mehedi Hasan
Mark as answer if the post meets your requirement!
![]() |
0 |
![]() |