Save File to Folder, File Name to Database

Thread is closed for posting
12 posts, 0 answers
  1. John Lyons
    John Lyons avatar
    1 posts
    Member since:
    Mar 2010

    Posted 20 Mar 2010 Link to this post

    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
  2. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1596 posts

    Posted 23 Mar 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Neil
    Neil avatar
    4 posts
    Member since:
    Jun 2011

    Posted 24 Jun 2011 Link to this post

    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
  5. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 24 Jun 2011 Link to this post

    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.
  6. Neil
    Neil avatar
    4 posts
    Member since:
    Jun 2011

    Posted 24 Jun 2011 Link to this post

    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?

  7. Neil
    Neil avatar
    4 posts
    Member since:
    Jun 2011

    Posted 24 Jun 2011 Link to this post

    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 ?
  8. Bill
    Bill avatar
    26 posts
    Member since:
    Jun 2008

    Posted 29 Jul 2012 Link to this post

    I would really love to see the full INSERT UPDATE and DELETE sample and in C# if possible.  Can anyone help?
  9. Peter Filipov
    Admin
    Peter Filipov avatar
    1028 posts

    Posted 01 Aug 2012 Link to this post

    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.
  10. Bill
    Bill avatar
    26 posts
    Member since:
    Jun 2008

    Posted 07 Aug 2012 Link to this post

    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
  11. Peter Filipov
    Admin
    Peter Filipov avatar
    1028 posts

    Posted 10 Aug 2012 Link to this post

    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.
  12. Bill
    Bill avatar
    26 posts
    Member since:
    Jun 2008

    Posted 23 Aug 2012 Link to this post

    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
  13. Peter Filipov
    Admin
    Peter Filipov avatar
    1028 posts

    Posted 28 Aug 2012 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017