This is a migrated thread and some comments may be shown as answers.

Save File to Folder, File Name to Database

11 Answers 225 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
John Lyons
Top achievements
Rank 1
John Lyons asked on 20 Mar 2010, 08:52 PM
First of all I am very new to ASP.NET. Been writing classic ASP for 10 years.
I have a very basic insert form that is submitting to a sql database named "Users", table named "tblUsers".. I have a field named "ImageName" that I am trying to get the file name to post to when the form is submited. The image uploads but I cannot figure out how to make the file name post to the database. I am using RadUpload and I figured you could bind the field to the database column just like you do the text fields. But I don't see how. This is all very new to me. Thanks

11 Answers, 1 is accepted

Sort by
0
Genady Sergeev
Telerik team
answered on 23 Mar 2010, 04:39 PM
Hello John Lyons,

There is no control that automatically saves the uploaded image name into a database. However, I have attached sample project that shows how to easy achieve do this.


Kind regards,
Genady Sergeev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Neil
Top achievements
Rank 1
answered on 24 Jun 2011, 11:26 AM
Hi

I'm trying to get the attached sample project to work - the upload works fine & name is added to DB, but i am stuck with the "edit" command to change the file name of an existing record in the database - the file uploads, but the name reamins unchanged.
I have had to convert the code from C# to Vb, & wonder if that is where the problem lies

My converted code is below - could anyone point me in the right direction?

Imports System.Web.UI.WebControls
Imports System.Data
Imports Telerik.Web.UI
Imports System.IO
Imports System.Data.Common
 
Public Partial Class _Default
    Inherits System.Web.UI.Page
     
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        AddHandler RadGrid1.ItemDataBound, AddressOf RadGrid1_ItemDataBound
    End Sub
 
    Private Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
            Dim upload As RadUpload = TryCast(e.Item.FindControl("RadUpload1"), RadUpload)
            ClientScript.RegisterClientScriptBlock(Page.[GetType](), "Upload", String.Format("window['UploadId'] = '{0}';", upload.ClientID), True)
        End If
    End Sub
 
    Protected Sub RadGrid1_InsertCommand(source As Object, e As Telerik.Web.UI.GridCommandEventArgs)
        Dim upload As RadUpload = TryCast(e.Item.FindControl("RadUpload1"), RadUpload)
        If upload.UploadedFiles.Count > 0 Then
            Dim file As UploadedFile = upload.UploadedFiles(0)
            SqlDataSource1.InsertParameters.Add("ImageName", file.GetName())
        End If
    End Sub
 
End Class


thanks in advance

Neil
0
Cori
Top achievements
Rank 2
answered on 24 Jun 2011, 01:06 PM
Hello Neil,

You need to handle the UpdateCommand and, most likely, set the Update text for the SqlDataSource and pass the parameters in for that event.

Currently, you're just handling the InsertCommand and passing the parameters to the InsertParameters collection of the SqlDataSource.

I hope that helps.
0
Neil
Top achievements
Rank 1
answered on 24 Jun 2011, 03:29 PM
Thanks for the quick reply. I'm fairly new to the codebehind stuff & i'm  learning as i go - any chance you could be a little more specific?

0
Neil
Top achievements
Rank 1
answered on 24 Jun 2011, 05:02 PM
Ok ,I've added an update statement, but still not getting any joy. I've also declared a handler for the OnUpdateCommand event in the Radgrid tag
The added update statement is below
Protected Sub RadGrid1_UpdateCommand(source As Object, e As Telerik.Web.UI.GridCommandEventArgs)
        If e.CommandName = RadGrid.UpdateCommandName Then
            If TypeOf e.Item Is GridEditableItem Then
                Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
                Dim id As Integer = Convert.ToInt32(item.GetDataKeyValue("ImageID"))
                If id <> 0 Then
                    Dim upload As RadUpload = DirectCast(e.Item.FindControl("RadUpload1"), RadUpload)
                    If upload.UploadedFiles.Count > 0 Then
                        Dim file As UploadedFile = upload.UploadedFiles(0)
                        SqlDataSource1.UpdateParameters.Add("ImageName", file.GetName())
                        RadGrid1.Rebind()
                    End If
                End If
            End If
        End If
    End Sub


Am I on the right lines here ?
0
Bill
Top achievements
Rank 1
answered on 29 Jul 2012, 07:38 AM
I would really love to see the full INSERT UPDATE and DELETE sample and in C# if possible.  Can anyone help?
0
Peter Filipov
Telerik team
answered on 01 Aug 2012, 10:16 AM
Hello Bill,

Please review the following demos - RadUpload in Grid and RadAsyncUpload in Grid.

Greetings,
Peter Filipov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Bill
Top achievements
Rank 1
answered on 08 Aug 2012, 02:12 AM
Would it be possible to change the sample AnotherUploadInGrid/Default.aspx to have an image column bound to the file folder.  Could you update the sample for us?

Thanks.
Bill
0
Peter Filipov
Telerik team
answered on 10 Aug 2012, 09:54 AM
Hello Bill,

I am sending you a sample project. Please review the attachment.

Regards,
Peter Filipov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Bill
Top achievements
Rank 1
answered on 23 Aug 2012, 02:55 PM
Hello Peter,

I have another question that I couldn't find anywhere in the forums or samples.

How can I have the RadUpload = RadTextBox for a filename?

In a RadGrid Template I have a RadUpload control and a RadTextBox control.  During the upload I want the filename from the RadUpload to fill in the filename in the RadTextBox.  I know this should be simple but I am having difficulty.

Here is my screen-shot:

ASPX:
<telerik:GridTemplateColumn HeaderText="FileName" UniqueName="FileName" DataField="FileName">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="LblImage" Text='<%# Eval("FileName") %>'></asp:Label>
                            <asp:Image ID="Image" ImageUrl='<%# "~/Uploads/" + Eval("FileName") %>' Style="float: left;
                                padding-right: 10px;" runat="server" Width="40" Height="40" /><br />
                            <asp:HyperLink ID="HyperLink1" runat="server"
                                NavigateUrl='<%# "~/Uploads/" + Eval("FileName") %>'  Text='<%# Eval("FileName") %>' ForeColor="#3399FF"></asp:HyperLink>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadTextBox runat="server" ID="File" Text='<%# Bind("FileName") %>'
                                EmptyMessage="name must =  picture file" Width="175px"></telerik:RadTextBox>
                            <telerik:RadUpload runat="server" ID="RadUpload1" TargetFolder="~/Uploads">
                            </telerik:RadUpload>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>

I have no idea as far as the C# or Javascript goes?  Can you help?
Thanks.
Bill
0
Peter Filipov
Telerik team
answered on 28 Aug 2012, 07:45 AM
Hi Bill,

Regarding your case I setup a sample page for you. Please review the attachment.

Regards,
Peter Filipov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Upload (Obsolete)
Asked by
John Lyons
Top achievements
Rank 1
Answers by
Genady Sergeev
Telerik team
Neil
Top achievements
Rank 1
Cori
Top achievements
Rank 2
Bill
Top achievements
Rank 1
Peter Filipov
Telerik team
Share this question
or