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

Image upload not binary in grid

12 Answers 119 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Teo B
Top achievements
Rank 1
Teo B asked on 29 Apr 2010, 10:33 PM
Hello,

does anybody has an example how to add an upload image inside a radgrid in edit and insert template and when edited to save the filename in database and image in a specified folder?
I just installed the trial version and I'm beginner in .NET .. sorry if there was an easy way and i didn't found it ,

thank you

12 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 30 Apr 2010, 08:15 AM
Hello Teo,

You can check this code library. Although it is for the "classic" RadControls for ASP.NET, it can be easily ported for RadControls for ASP.NET AJAX.

All the best,
Rosen
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
Teo B
Top achievements
Rank 1
answered on 30 Apr 2010, 10:04 AM
Thank you ,

but because I'm beginner there's no other easy er way to accomplish this with less code?

Just wondering,

thank you
Teodor
0
Rosen
Telerik team
answered on 03 May 2010, 12:09 PM
Hi Teo,

You can use RadUpload's TargetFolder property to set where the files should be uploaded and then you may use UploadedFiles to get the ImageFile's name and update the appropriate record in the database.  I have attached a small sample which demonstrates a possible implementation.

Sincerely yours,
Rosen
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
Teo B
Top achievements
Rank 1
answered on 03 May 2010, 02:11 PM
Thank you very much,

based on your sample i tried to do this:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <telerik:RadScriptManager ID="RadScriptManager1" Runat="server"
        </telerik:RadScriptManager> 
        <telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="False" AutoGenerateEditColumn="True" 
            DataSourceID="pictures" GridLines="None" AllowAutomaticInserts="True" 
            AllowAutomaticUpdates="True"
             <MasterTableView DataKeyNames="Id" ShowHeadersWhenNoRecords="true" CommandItemDisplay="Top" 
                DataSourceID="pictures"
                <Columns> 
                    <telerik:GridBoundColumn HeaderText="ID" ReadOnly="true" DataField="Id" /> 
                    <telerik:GridBoundColumn HeaderText="Brand" DataField="Brand" /> 
                    <telerik:GridTemplateColumn HeaderText="Logo"
                        <ItemTemplate> 
                            <asp:Image ID="Image1" ImageUrl='<%#string.Format("~/images/{0}", Eval("Logo")) %>' runat="server" 
                                Width="100px" Height="100px" /> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            <telerik:RadUpload runat="server" ID="RadUpload1" TargetFolder="~/Images" OverwriteExistingFiles="false" 
                                ControlObjectsVisibility="None" /> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                </Columns> 
            </MasterTableView> 
        </telerik:RadGrid> 
        <asp:SqlDataSource ID="pictures" runat="server"  
            ConnectionString="<%$ ConnectionStrings:sunglassesConnectionString %>"  
             OnInserting="pictures_Inserting" OnUpdating="pictures_Updating"
            <DeleteParameters> 
                <asp:Parameter Name="id" Type="Int32" /> 
            </DeleteParameters> 
            <UpdateParameters> 
                <asp:Parameter Name="brand" Type="String" /> 
                <asp:Parameter Name="logo" Type="String" /> 
                <asp:Parameter Name="id" Type="Int32" /> 
            </UpdateParameters> 
            <InsertParameters> 
                <asp:Parameter Name="brand" Type="String" /> 
                <asp:Parameter Name="logo" Type="String" /> 
            </InsertParameters> 
        </asp:SqlDataSource> 
    </div> 
    </form> 
</body> 
</html> 
and code behind:
Imports System 
Imports System.Web.UI 
Imports System.Web.UI.WebControls 
Imports Telerik.Web.UI 
Imports System.Data 
Partial Class _Default 
    Inherits System.Web.UI.Page 
 
 
 
    Protected Sub pictures_Inserting(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) 
        e.InputParameters("logo") = ExtractImageName(RadGrid1.MasterTableView.GetInsertItem()) 
    End Sub 
 
    Protected Sub pictures_Updating(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) 
        e.InputParameters("logo") = ExtractImageName(DirectCast(RadGrid1.EditItems(0), GridDataItem).EditFormItem) 
    End Sub 
 
    ''' <summary> 
    ''' Extracts uploaded file's name 
    ''' </summary> 
    ''' <param name="gridItem">GridItem which holds the RadUpload control</param> 
    ''' <returns>Name of the file which has been uploaded or empty string otherwise</returns> 
    Private Shared Function ExtractImageName(ByVal gridItem As GridItem) As String 
        Dim logo As String = [String].Empty 
        Dim radUpload = TryCast(gridItem.FindControl("RadUpload1"), RadUpload) 
        If radUpload IsNot Nothing AndAlso radUpload.UploadedFiles.Count > 0 Then 
            logo = radUpload.UploadedFiles(0).GetName() 
        End If 
        Return logo 
    End Function 
 
End Class 
 
 

and it upload the image but won't add the file name in the db for the logo column just uploaded..
Could you please look into maybe you could solve my problem?
If you see where's the mistake here i won't bother anymore thank you very much

Teodor
0
Rosen
Telerik team
answered on 03 May 2010, 04:58 PM
Teo,

When using SqlDataSource control you should specify SelectCommand and in your case also UpdateCommand and InsertCommand.

I have also noticed that SqlDataSource's Updating and Inserting event handlers are not correctly attached (you have AutoEventWireup set to false) nor declared (method signatures are for ObjectDataSource).

I hope this helps.

All the best,
Rosen
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
Teo B
Top achievements
Rank 1
answered on 04 May 2010, 12:48 PM
Ok added these still don't know how to make it work in my example:

<%@ Page Language="VB" AutoEventWireup="True" CodeFile="Default.aspx.vb" Inherits="_Default" %> 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <telerik:RadScriptManager ID="RadScriptManager1" Runat="server"
        </telerik:RadScriptManager> 
        <telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="False" AutoGenerateEditColumn="True" 
            DataSourceID="pictures" GridLines="None" AllowAutomaticInserts="True" 
            AllowAutomaticUpdates="True"
             <MasterTableView DataKeyNames="id" ShowHeadersWhenNoRecords="true" CommandItemDisplay="Top" 
                DataSourceID="pictures"
                 <Columns>  
                    <telerik:GridBoundColumn HeaderText="ID" ReadOnly="true" DataField="Id" />  
                    <telerik:GridBoundColumn HeaderText="Brand" DataField="Brand" />  
                    <telerik:GridTemplateColumn HeaderText="Logo">  
                        <ItemTemplate>  
                            <asp:Image ID="Image1" ImageUrl='<%#string.Format("~/images/{0}", Eval("Logo")) %>' runat="server"  
                                Width="100px" Height="100px" />  
                        </ItemTemplate>  
                        <EditItemTemplate>  
                            <telerik:RadUpload runat="server" ID="RadUpload1" TargetFolder="~/Images" OverwriteExistingFiles="false"  
                                ControlObjectsVisibility="None" />  
                        </EditItemTemplate>  
                    </telerik:GridTemplateColumn>  
                </Columns>  
            </MasterTableView> 
        </telerik:RadGrid> 
        <asp:SqlDataSource ID="pictures" runat="server"  
            ConnectionString="<%$ ConnectionStrings:sunglassesConnectionString %>"  
              
            DeleteCommand="DELETE FROM [brands] WHERE [id] = @id"  
            InsertCommand="INSERT INTO [brands] ([brand], [logo]) VALUES (@brand, @logo)"  
            SelectCommand="SELECT * FROM [brands]"  
            UpdateCommand="UPDATE [brands] SET [brand] = @brand, [logo] = @logo WHERE [id] = @id"
            <DeleteParameters> 
                <asp:Parameter Name="id" Type="Int32" /> 
            </DeleteParameters> 
            <UpdateParameters> 
                <asp:Parameter Name="brand" Type="String" /> 
                <asp:Parameter Name="logo" Type="String" /> 
                <asp:Parameter Name="id" Type="Int32" /> 
            </UpdateParameters> 
            <InsertParameters> 
                <asp:Parameter Name="brand" Type="String" /> 
                <asp:Parameter Name="logo" Type="String" /> 
            </InsertParameters> 
        </asp:SqlDataSource> 
    </div> 
    </form> 
</body> 
</html> 
 
and

Imports System 
Imports System.Web.UI 
Imports System.Web.UI.WebControls 
Imports Telerik.Web.UI 
Imports System.Data 
Partial Class _Default 
    Inherits System.Web.UI.Page 
 
 
 
    Protected Sub pictures_Inserting(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) 
        e.InputParameters("logo") = ExtractImageName(RadGrid1.MasterTableView.GetInsertItem()) 
    End Sub 
 
    Protected Sub pictures_Updating(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) 
        e.InputParameters("logo") = ExtractImageName(DirectCast(RadGrid1.EditItems(0), GridDataItem).EditFormItem) 
    End Sub 
 
    ''' <summary> 
    ''' Extracts uploaded file's name 
    ''' </summary> 
    ''' <param name="gridItem">GridItem which holds the RadUpload control</param> 
    ''' <returns>Name of the file which has been uploaded or empty string otherwise</returns> 
    Private Shared Function ExtractImageName(ByVal gridItem As GridItem) As String 
        Dim logo As String = [String].Empty 
        Dim radUpload = TryCast(gridItem.FindControl("RadUpload1"), RadUpload) 
        If radUpload IsNot Nothing AndAlso radUpload.UploadedFiles.Count > 0 Then 
            logo = radUpload.UploadedFiles(0).GetName() 
        End If 
        Return logo 
    End Function 
 
End Class 
 
 

0
Rosen
Telerik team
answered on 10 May 2010, 07:45 AM
Teo,

If you look at the code, you may noticed that SqlDataSource's Inserting and Updating event handlers are still not attached.

Rosen
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
Teo B
Top achievements
Rank 1
answered on 10 May 2010, 07:33 PM
Told you I'm beginner in the top ..
i suppose is just 2 lines what you should write to attach the handlers .. and i'm sure this will help others ... like me ..

0
Bill
Top achievements
Rank 1
answered on 11 Aug 2012, 11:42 PM
I downloaded this sample app but one of the things I noticed is that the "Name" attribute automatically gets a .jpg added to the end of the text string.  In my case the way I want to implement this is to allow the image to be uploaded to a separate folder like in this sample however I don't want it to change the attribute of the name.

For example:

If I upload an image called test the image file will be called test.jpg and "Name" will get changed to test.jpg instead of test.

I don't want it to be test.jpg in the Name field of the database. JUST test with no extension

Is there a way to implement this? I'm stumped since there is now examples on this.

Thanks.
Bill.
0
Radoslav
Telerik team
answered on 15 Aug 2012, 11:12 AM
Hi Bill,

I tried to reproduce the described behavior with the latest version of the RadControls, but to no avail. I am sending you the mentioned example with the latest version of the RadControls. Also on the following link I attached a small video which demonstrates how the application works on my end. Please check it out and let me know if it helps you.

Looking forward for your reply.

Regards,
Radoslav
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
PG
Top achievements
Rank 1
answered on 15 Oct 2013, 04:29 AM
Hello Radoslav,

I download the example.zip and test it was the closer solution what i need, but the example using the ObjectDataSource,  I'll like to using the SqlDataSource, try to modify the code behind just cannot make it, pls help....Thanks

protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
        e.Command.Parameters["@Imagename"].Value = ExtractImagename(RadGrid1.MasterTableView.GetInsertItem());
    }

    protected void SqlDataSource1_Updated(object sender, SqlDataSourceStatusEventArgs e)
    {
        e.Command.Parameters["@Imagename"].Value = ExtractImagename(((GridDataItem)RadGrid1.EditItems[0]).EditFormItem);
    }
    private static string ExtractImagename(GridItem gridItem)
    {

        string Imagename = String.Empty;
        var radUpload = (gridItem.FindControl("RadAsyncUpload1") as RadUpload);
        if (radUpload != null && radUpload.UploadedFiles.Count > 0)
        {
            Imagename = radUpload.UploadedFiles[0].GetName();
        }
        return Imagename;
       
    }

0
Radoslav
Telerik team
answered on 17 Oct 2013, 08:41 AM
Hello PG,

From the provided code snippet I saw that you handle OnInserted and OnUpdated events of an SqlDataSource. However to achieve the desired functionality you need to handle the OnInserting, and OnUpdating events, like into the provided example.

Please give it try and let me know if you experience any problems.

Regards,
Radoslav
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Teo B
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Teo B
Top achievements
Rank 1
Bill
Top achievements
Rank 1
Radoslav
Telerik team
PG
Top achievements
Rank 1
Share this question
or