Insert all records from RadGrid

2 posts, 0 answers
  1. Helen
    Helen avatar
    42 posts
    Member since:
    Nov 2013

    Posted 03 Dec 2013 Link to this post

    Hi,

    I have an implementation question which I'm hoping someone with much more experience and expertise could guide me in the right direction.

    I need to load a batch of 5000 ~ 10,000 phone numbers into a database.

    The user can select and upload a file using RadUpload and RadGrid with Paging turned on.

    The user can preview, add, delete the record in the RadGrid and then click on 'Insert' button to load the phone numbers into database.

    I'm really hoping to cache the data instead of using a saved file on the server for re-binding.

    I think RadGrid only holds data for the current page, so what would be the best way to implement this?

    Thank you in advance~

    Helen


    This is what I'm currently doing in code behind...newbie style!

    protected void Page_Load(object sender, EventArgs e)
    {
        this.tollFreeInventoryLogic = (TollFreeInventoryLogic)this.Session[SESSION_ATTR_NAME];
        if (tollFreeInventoryLogic == null)
        {
            this.tollFreeInventoryLogic = new TollFreeInventoryLogic();
            this.Session[SESSION_ATTR_NAME] = this.tollFreeInventoryLogic;
        }
    }
    protected void RadButtonUploadToGrid_Click(object sender, EventArgs e)
    {
        string targetFolder = RadUpload1.AppRelativeTemplateSourceDirectory + RadUpload1.TargetFolder;
        string fileName = targetFolder + "/" + RadUpload1.UploadedFiles[0].GetName();
        Session["fileName"] = fileName;
     
        GetDataFromMemory();
    }
     
    protected void GetDataFromFile()
    {
        if (Session["fileName"] != null)
        {
            string fileName = (string)Session["fileName"];
            try
            {
                using (StreamReader sr = new StreamReader(Server.MapPath(fileName)))
                {
     
                    string tollFreeList = sr.ReadToEnd();
                    string[] tollFreeArray = tollFreeList.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None);
     
                    DataTable dt = CreateDataTable(tollFreeArray);
                    RadGrid1.DataSource = dt;
                }
                 
            }
            catch (Exception err)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(err.Message);
            }
        }
    }
     
    protected void GetDataFromMemory()
    {
        foreach (UploadedFile file in RadUpload1.UploadedFiles)
        {
     
            byte[] bytes = new byte[file.ContentLength];
            file.InputStream.Read(bytes, 0, bytes.Length);
            string tollFreeList = System.Text.Encoding.Default.GetString(bytes);
            string[] tollFreeArray = tollFreeList.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None);
     
            DataTable dt = CreateDataTable(tollFreeArray);
     
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
     
        }
         
    }
     
    private DataTable CreateDataTable(string[] tollFreeArray)
    {
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add("TollFreeNumber");
        foreach (string line in tollFreeArray)
        {
            //Debug.WriteLine(line);
            if (line.Trim().Length > 0)
            {
                dr = dt.NewRow();
                dr["TollFreeNumber"] = line;
                dt.Rows.Add(dr);
            }
        }
        return dt;
    }
     
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        GetDataFromFile();
    }
  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2289 posts

    Posted 06 Dec 2013 Link to this post

    Hello Helen,

    If you would like to be able to make multiple updates on a page in RadGrid and submit all changes at once you could use batch editing mode. Online demo for this mode is also available here. Additionally you could use a flag to indicate if any changes were made and alert the user that changes would be lost.  A possible approach for implementing this is discussed in this forum thread (check the attached sample).

    Note that persisting the changes in RadGrid on paging and updating the items in all pages on Update command is not supported.

    On a side note I would like to point your attention to this article describing how NeedDataSource event should be used. Illustration is also available in this online demo.

    Generally when NeedDataSource is used RadGrid's DataSource property should be set only in the handler for the above mentioned event. When changes to the grid that need binding are made you should call Rebind(). Also when using advanced data-binding the DataBind() method must not be used. More information could be found in this article.

    This said, the logic in the provided code could be changed a little. For example a method returning DataTable could be used to set the RadGrid's DataSource. In this method you could handle getting the data from the data source.

    protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = GetData();
    }

    protected DataTable GetData()
    {
        DataTable data = new DataTable();
     
        // retrieve data
     
        return data;
    }



    Regards,
    Viktor Tachev
    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.
Back to Top