ASP.NET 3.5 DetailsView and FileUpload on ItemInsert

I’m currently due to exam and we have been using your book for learning - I’m not sure how capable our teacher is here at eCademy(Norway). with only a few days left before exam i really need this question sorted out… our teacher is of no help.

my problem: i have a details view which will only be used for inserting data. i have a fileupload control for uploading images and the meaning is that the pictures url should be stored in the database. as the fileupload does not have “value” field how can i get the url into the db?

the file upload works great but i get a error with inserting null (which is right because i dont want nulls in the db)
since the fileupload cant handle a value item i’ve been trying to get the url into an hidden texbox as you can see in the code but with no luck.

…as i said time is of essence and i would appreciate if anyone could help me out here:

my aspx code:
<asp:DetailsView ID=“DetailsView1” runat=“server”
DataSourceID=“dvDS” DataKeyNames=“prodID” AutoGenerateRows=“False”
DefaultMode=“Insert”>
<Fields>
<asp:BoundField DataField=“prodID” HeaderText=“prodID” InsertVisible=“False” ReadOnly=“True” SortExpression=“prodID” />
<asp:TemplateField HeaderText=“SubID” SortExpression=“SubID”>
<InsertItemTemplate>
<asp:DropDownList ID=“ddlSubCategory” runat=“server” DataSourceID=“SqlDataSource1” DataTextField=“subName” DataValueField=“subID” SelectedValue=‘<%# Bind(“SubID”) %>’ >
<asp:ListItem Value=“”>Make a choice!</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=“prodName” HeaderText=“prodName” SortExpression=“prodName” />
<asp:BoundField DataField=“prodTxt” HeaderText=“prodTxt” SortExpression=“prodTxt” />
<asp:BoundField DataField=“prodPrice” HeaderText=“prodPrice” SortExpression=“prodPrice” />
<asp:TemplateField HeaderText=“prodIMG” SortExpression=“prodIMG”>
<InsertItemTemplate>
<asp:FileUpload ID=“FileUpload1” runat=“server” />
<asp:TextBox ID=“tbxFile” runat=“server” Visible=“false” ReadOnly=“True” Text=‘<%# Bind(“prodIMG”) %>’></asp:TextBox>
<%–<asp:TextBox ID=“TextBox1” runat=“server” Text=‘<%# Bind(“prodIMG”) %>’></asp:TextBox>–%>
</InsertItemTemplate>
</asp:TemplateField>

<asp:CommandField ShowInsertButton=“True” CausesValidation=“false”/>
</Fields>
</asp:DetailsView>

<asp:SqlDataSource ID=“dvDS” runat=“server” ConnectionString=“<%$ ConnectionStrings:csShop %>”
InsertCommand=“INSERT INTO [Products] ([SubID], [prodName], [prodTxt], [prodPrice], [prodIMG], [prodDate]) VALUES (@SubID, @prodName, @prodTxt, @prodPrice, @prodIMG, GETDATE())”>
<InsertParameters>
<asp:Parameter Name=“SubID” Type=“Int32” />
<asp:Parameter Name=“prodName” Type=“String” />
<asp:Parameter Name=“prodTxt” Type=“String” />
<asp:Parameter Name=“prodPrice” Type=“Decimal” />
<asp:Parameter Name=“prodIMG” Type=“String” />

</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID=“SqlDataSource1” runat=“server” ConnectionString=“<%$
ConnectionStrings:csShop %>”
SelectCommand=“SELECT [subID], [subName] FROM [SubCategory]”>
</asp:SqlDataSource>

and my vb code:
Protected Sub dvDS_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEven tArgs) Handles dvDS.Inserting

Dim FileUpload1 As FileUpload = CType(DetailsView1.FindControl(“FileUpload1”), FileUpload)
Dim virtualFolder As String = “~/productimages/”
Dim physicalFolder As String = Server.MapPath(virtualFolder)
Dim fileName As String = Guid.NewGuid().ToString()
Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName)

FileUpload1.SaveAs(System.IO.Path.Combine(physical Folder, fileName + extension))

'Dim myPicture As String = virtualFolder + fileName + extension
'lblURL.Text
'dvDS.InsertParameters.Item(“prodIMG”).DefaultValu e = myPicture
'e.Values[“prodIMG”] = virtualFolder + fileName + extension

'Dim FileCtrl As FileUpload = CType(DetailsView1.Controls(0).FindControl(“FileUp load1”), FileUpload)
Dim TextCtrl As TextBox = CType(DetailsView1.Controls(0).FindControl(“tbxFil e”), TextBox)
'If FileCtrl.HasFile Then
Dim url As String = virtualFolder + fileName + extension
'FileCtrl.SaveAs(Server.MapPath(FileName))
TextCtrl.Text = url
TextCtrl.Visible = True
'FileCtrl.Visible = False
'End If

End Sub

Ok, well what you want to do is save the file onto the server and then use that path.

FileUpload1.PostedFile.SaveAs() method is what you are looking for. Your code should check to see if that filename already exists and if it does, rename it until it is unique. Then return that filename and save that in ur db.

You will be using the System.IO namepsace for that.

I can give you sample code, but only in C#. So let me know if you want that

the code works for uploading a file and giving it a uniqe name but im not getting the filename and path returned to db

i guess C# code is ok so i could look at it as an example if you have? thanks

after 72 hours i actually managed to solve this

I had to move the vb code to DetailsView_iteminserting - the code to use was:

Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting

    Dim FileUpload1 As FileUpload = CType(DetailsView1.FindControl("FileUpload1"), FileUpload)
    Dim virtualFolder As String = "~/productimages/"
    Dim physicalFolder As String = Server.MapPath(virtualFolder)
    Dim fileName As String = Guid.NewGuid().ToString()
    Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName)
    'Upload file:
    FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension))
    'Assign url to db:
    e.Values("prodIMG") = virtualFolder + fileName + extension

End Sub