Rad upload and rad grid

Thread is closed for posting
12 posts, 0 answers
  1. feras
    feras avatar
    24 posts
    Member since:
    Sep 2008

    Posted 15 Nov 2008 Link to this post

    hi, i am more and more getting in love testing your product and i am thinking seriously to move all my work in the future to depend on your control .
    i am having this scenario
    i want to have a gird with articles dta source where the article db will have an image
    now what i want to know how to do is
    - how can i save the name of the uploaded image in the image field in DB
    - if the user uploads a file that don't have the same name of the one already in DB how can i delete that and upload the new file and update the Image DB filed name with it ?
    thx in advanced.

  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 17 Nov 2008 Link to this post

    Hello feras,

    You can check this online example. Have in mind it is using the SessionDataSource control which does not persist the changes in the database for example purpose.

    All the best,
    Albert
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. feras
    feras avatar
    24 posts
    Member since:
    Sep 2008

    Posted 17 Nov 2008 Link to this post

    hi,
    thanks for the reply,
    but still this example does not answer my question as i am using sql data source and automatic operations.
    if you reread my question you would see that i want the db field to be inserted or updated with the image name being uploaded ,
    plus i want to compare new file upload with old one if the name is not the same i want the old one to be removed from hard disk and replaced with the new one.
    plus as i am reading the manual there is suppose to be auto saving of file how does htis work in my requirement ?
    please this is urgent, and i am going to buy telerk full suite today.
  5. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 18 Nov 2008 Link to this post

    Hello feras,

    RadUpload does not support automatic uploads in databases. This example is the closest we got to your scenario so you can use it as a start.

    Regards,
    Albert
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. feras
    feras avatar
    24 posts
    Member since:
    Sep 2008

    Posted 19 Nov 2008 Link to this post

    ok,
    you know what, i am really disappointed, with this short none elaborating answer.
    first for me it is misleading that it is not mentioned anywhere that you cant do automatic operation with radupload and grid.
    second that example bare bone and does not answer my questions
    third the other part of my question was not answered which is how to retrieve the name of the uploaded file and compare it with already uploaded one, and if it is not the same delete the old one and update the image filed name in DB, but if it is the same just overwrite the file and update the image filed name .
    now as i understood saving the file part is done automatically  through setting target directory.

    i  really hate to say it as i really see you have great set of products, but is this the premium support ?

    thx in advanced for listening
     
  7. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 21 Nov 2008 Link to this post

    Hello feras,

    I am sorry for your frustration.

    Please note that these are public forums where you can get a help from the community, but there is no guarantee that a Telerik member will respond (although we do that in 99% of the cases). You can use the support ticketing system to get a faster response from Telerik Support.

    Now, onto the questions - here is what you can do

    1. Do not use TargetFolder or TargetPhysicalFolder because in this way the files are saved to the file system automatically. You need to save the file manually. This will give you an access to the uploaded file's properties like name, extension, contentlength, etc.
    2. Having the name of the uploaded file - you can query the database and compare it with the value in the database. If they are different - you can use the .NET framework classes to delete the old file from the disk. Then you can update the database with the new name.

    I hope that helps.

    Greetings,
    Veselin Vasilev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. Arni
    Arni avatar
    17 posts
    Member since:
    Oct 2008

    Posted 03 Dec 2008 Link to this post

     int counter = 1;  
     
            //string date = DateTime.Now.ToString("dd_MM_yyy_hh_mm");  
            if (RadUpload1.UploadedFiles.Count > 0)  
                {  
                    foreach (Telerik.Web.UI.UploadedFile file in RadUpload1.UploadedFiles)  
                    {  
                        AttachmentsEN loAtten = new AttachmentsEN();  
                        Attachment loAttbs = new Attachment();  
                        int i = 0;  
                        bool recaff = new bool();  
                        loAtten.TaskID = Convert.ToInt32(ddlTaskID.SelectedValue);  
                        string targetFolder = Server.MapPath(RadUpload1.TargetFolder);  
                        string targetFileName = System.IO.Path.Combine(targetFolder,  
                                    file.GetNameWithoutExtension() + Counter.ToString()+ file.GetExtension());  
                          
                            while (System.IO.File.Exists(targetFileName))  //.(Used to compare the old file and New File)
                            {  
                              counter++;  
                                 targetFileName = System.IO.Path.Combine(targetFolder,  
                            file.GetNameWithoutExtension() + Counter.ToString+ file.GetExtension());  
                            }  
                          
                        loAtten.PhysicalFileName = file.FileName;  
                        file.SaveAs(targetFileName);  
                        loAtten.ActualFileName =  targetFileName;
                        loAtten.TypeCategory = file.GetExtension();  
                        loAtten.Path = targetFolder.ToString();  
                       try 
                        {  
                              
                            recaff = loAttbs.Insert(loAtten);  
                            ErrorDescription = ("Records added Successfully");  
                            lblMesg.Text = ErrorDescription;  
                        }  
                        catch (Exception ex)  
                        {  
                            lblMesg.Visible = true;  
                            lblMesg.Text = ex.Message.ToString();  
                        }  
     
                    }      


    Hi Feras,
    I have given Code Example How if same named file uploaded .. asign variable int counter =1.(Used to compare the old file and New File)
    check each uploadingg file if file already exist it add one in counter  so tht tht file is saved as file1.doc..im the target folder. and saved as the same way in db...
    THis I hv done for Inserting..even if its is the image file it wil saved to the db..U rimage url field in db shd be nvarchar(max).so tht it stores the path.. regarding updating I am under process after I hv done I wil reply U.
    Thank u.
  9. feras
    feras avatar
    24 posts
    Member since:
    Sep 2008

    Posted 03 Dec 2008 Link to this post

    thanks alot man for the code,
    i will try it out  :)

  10. Arni
    Arni avatar
    17 posts
    Member since:
    Oct 2008

    Posted 03 Dec 2008 Link to this post

    The above code has small   mistake.
    that is.. once u set targetfolder in properties ..
    It is storing the file first b4 going ito the loop and again the same file with the given name.. so twicely storing..
      do not set target folder b4 loop or in  properties.
    put radupload1.targetfolder="~/files";  after the loop.

    rest for updating is also the same code. and regarding deleting the record is also the  simple asusual.U can try easily
  11. feras
    feras avatar
    24 posts
    Member since:
    Sep 2008

    Posted 04 Dec 2008 Link to this post

    thanks alot man for the heads up and your efforts :)

  12. Ramkumar
    Ramkumar avatar
    12 posts
    Member since:
    Nov 2013

    Posted 28 Nov 2013 Link to this post

    Hi Shinu,
      I tried with the gridattachment column but can't able to insert a data into a grid i am attaching file pls review it and make you suggesions

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RadgridAttachment.Default" %>
      
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
      
    <!DOCTYPE html>
      
    <head runat="server">
        <title></title>
        <telerik:RadCodeBlock ID="codeblock1" runat="server">
            <script type="text/javascript">
                  
                function ClientFileSelected(sender, args) {
                    var filePath = sender.getFileInputs()[0].value;
                    var fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
                    //Access your file name here
      
                    var fileinputs = sender.getFileInputs();
                    for (var i = fileinputs.length - 1; i >= 0; i--) {
                        sender.clearFileInputAt(i);
                    }
                }
        </script>
      
        </telerik:RadCodeBlock>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Height="200px" Width="300px">
                <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound" OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand">
                    <MasterTableView CommandItemDisplay="Top">
                        <Columns>
                            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
      
                            </telerik:GridBoundColumn>
                            <telerik:GridAttachmentColumn UniqueName="Attachment" HeaderText="Attachment" UploadControlType="RadAsyncUpload">
      
                            </telerik:GridAttachmentColumn>
                            <telerik:GridEditCommandColumn>
      
                            </telerik:GridEditCommandColumn>
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>
            </telerik:RadAjaxPanel>
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
      
    namespace RadgridAttachment
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
      
            }
      
            protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
            {
                try
                {
                    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                    {
                        GridEditableItem item = e.Item as GridEditableItem;
                        RadUpload up = item["Attachment"].Controls[0] as RadUpload;
                        up.OnClientFileSelected = "ClientFileSelected";
                    }
                }
                catch (Exception m)
                {
                    Response.Write(m.Message);
                }
            }
      
            protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                dynamic data = new[]{
                    new {ID=1,Name="Name1"}
                };
                RadGrid1.DataSource = data;
            }
      
            protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
            {
                try
                {
      
                    GridEditableItem item = e.Item as GridEditableItem;
      
                    RadUpload up = item["Attachment"].Controls[0] as RadUpload;
      
      
                    UploadedFile uf = up.UploadedFiles[0];
                    string filename = up.UploadedFiles[0].FileName;
                }
                catch (Exception m)
                {
                    Response.Write(m.Message);
                }
            }
        }
    }

    just apply the code in ur local and test
  13. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 29 Nov 2013 Link to this post

    Hi Ramkumar,

    The RadGrid provides a GridAttachmentColumn, there please try to  set the UploadControlType property to either RadAsyncUpload or RadUpload for upload to or download from the data source. Please have a look into this online demo. Please elaborate your requirement if it doesn't help you.

    Thanks,
    Shinu.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017