Using UserControl to Insert/Update

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
Hello community!

I am using and following this Demo, and i want the exact same result for Insert and Update operations, my problem is that my architcture is totally different and i can't addapt it to the solution.

My main problem is in the update and insert command events.. i tried several ways but i have no idea on how to do it (the problem uses session and DataTable, which are not usefull for me)
I will also share a demo (non functional), if you are willing to take a better look see my architecture and Get methods. I am using 3 layers architecture

InsertCommand method:
 protected void gvTimesheets_InsertCommand(object source, GridCommandEventArgs e)
        {
         
            GridEditableItem editedItem = e.Item as GridEditableItem;
            ViewEmissaoTimeSheetEdit userControl = (ViewEmissaoTimeSheetEdit)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

            //Create new row in the DataSource

            var editableItem = ((GridEditableItem)e.Item);
            var timesheetID = (int)editableItem.GetDataKeyValue("ID"); //evaluate ID value
            //WITH ERRORS, NEED HELP HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
            //Insert new values
            Hashtable newValues = new Hashtable();
 
                    newValues["IDRecursoHumano"] = (userControl.FindControl("rdpInvestigador") as RadTextBox).Text;
                    newValues["IDEstadoTimesheet"] = (userControl.FindControl("rdbEstado") as RadComboBox).Text;
                    newValues["AssinaturaTimesheet"] = (userControl.FindControl("txtAssinaturaTimesheet") as RadTextBox).Text;
                    newValues["Observações"] = (userControl.FindControl("Obervaçoestxt") as RadTextBox).Text;
                    newValues["DataEnvio"] = (userControl.FindControl("DataEnvio") as RadDatePicker).SelectedDate.ToString();
                    newValues["FileContent"] = (userControl.FindControl("RadAsyncUpload1") as RadAsyncUpload).TargetFolder.ToString();
 
            //make sure that unique primary key value is generated for the inserted roW
            //NEED HELP
        
            try
            {
                foreach (DictionaryEntry entry in newValues)
                {
                    //NEED HELP
                
                }
                   // SAVE CHANGES
            }
            catch (Exception ex)
            {
                e.Canceled = true;
            }
        }

//BLL methods "listagembll":
 public List<ListagemTimesheet> GetAllFiles(int IDRecursoHumano = 0) ///filtros de pesquisa por investigador, include FKs
        {
            try
            {
                using (GestaoProjectosEntities lt = new GestaoProjectosEntities())
                {
                    var query = lt.ListagemTimesheets.Include("RecursoHumano").Include("EstadoTimesheet").AsQueryable();
                    if (IDRecursoHumano > 0)
                        query = query.Where(a => a.IDRecursoHumano == IDRecursoHumano);

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                //log.Error("BLL => GetAllFiles:" + ex.Message);
                return new List<ListagemTimesheet>();
            }
        }


  
        // Save File

        public int SaveFile(ListagemTimesheet model)
        {
            try
            {
                using (GestaoProjectosEntities lt = new GestaoProjectosEntities())
                {
                    lt.ListagemTimesheets.AddObject(model);
                    lt.SaveChanges();
                    return model.ID;
                }
            }
            catch (Exception ex)
            {
                //log.Error("BLL => SaveFile:" + ex.Message);
                return 0;
            }
        }

        /// Update File
        public void UpdateFile(ListagemTimesheet model)
        {
            try
            {
                using (GestaoProjectosEntities lt = new GestaoProjectosEntities())
                {
                    var result = lt.ListagemTimesheets.SingleOrDefault(b => b.ID == model.ID);
                    if (result != null)
                    {

                        result.IDEstadoTimesheet = model.IDEstadoTimesheet;
                        result.FileContent = model.FileContent;
                        result.Ficheiro = model.Ficheiro;
                        result.DataEnvio = model.DataEnvio;
                        result.FileTipo = model.FileTipo;
                        result.AssinaturaTimesheet = model.AssinaturaTimesheet;
                        result.Observações = model.Observações;
                        lt.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                //log.Error("BLL => UpdateFile:" + ex.Message);
            }
        }

        public void InsertFile(ListagemTimesheet model)
        {
            try
            {
                using (GestaoProjectosEntities lt = new GestaoProjectosEntities())
                {
                    var result = lt.ListagemTimesheets.SingleOrDefault(b => b.ID == model.ID);
                    if (result != null)
                    {
                        result.ID = model.ID;
                        result.IDRecursoHumano = model.IDRecursoHumano;
                        result.IDEstadoTimesheet = model.IDEstadoTimesheet;
                        result.FileContent = model.FileContent;
                        result.Ficheiro = model.Ficheiro;
                        result.DataEnvio = model.DataEnvio;
                        result.FileTipo = model.FileTipo;
                        result.AssinaturaTimesheet = model.AssinaturaTimesheet;
                        result.Observações = model.Observações;
                        lt.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                //log.Error("BLL => UpdateFile:" + ex.Message);
            }
        }

//DataTable model:
   public partial class ListagemTimesheet
    {
        public int ID { get; set; }
        public System.DateTime DataEnvio { get; set; }
        public int IDRecursoHumano { get; set; }

        public string AssinaturaTimesheet { get; set; }
        public int IDEstadoTimesheet { get; set; }
        public string Observações { get; set; }
        public string Ficheiro { get; set; }
        public string FileTipo { get; set; }
        public byte[] FileContent { get; set; }

        public virtual EstadoTimesheet EstadoTimesheet { get; set; }
        public virtual RecursoHumano RecursoHumano { get; set; }
    }
}

The frontend follows the telerik framework Command Item template, which creates the add and edit buttons, which should be triggered by the command events.

Frontend:
            <CommandItemTemplate>
                                     <telerik:RadButton  CommandName="InitInsert" IsinEditmode="true" RenderMode="Lightweight" ID="AddNewRecordButton" Text="Adicionar nova versão" ToolTip="Adicionar versão" runat="server" style="background:none; border:none; color:green;" NavigateUrl="~/Views/Projectos/Equipa/ViewEmissaoTimeSheetEdit.ascx" CommandArgument="ID">                                      
                                         <Icon PrimaryIconCssClass="rbAdd"></Icon>
                                     </telerik:RadButton>                    
                                    <telerik:RadButton CommandName="Rebind" RenderMode="Lightweight" ID="Rebindbutton" Text="Atualizar lista" ToolTip="Atualizar a lista" runat="server" style="background:none; border:none; color:darkblue;" >                                      
                                        <Icon PrimaryIconCssClass=" rbRefresh"></Icon>
                                     </telerik:RadButton>                                
                                 </CommandItemTemplate>          
                            <Columns>                            
                                <telerik:GridEditCommandColumn  UniqueName="EditCommandColumn" ButtonType="PushButton" ItemStyle-ForeColor="Black">                              
                                </telerik:GridEditCommandColumn>
                                <EditFormSettings UserControlName="~/Views/ViewEmissaoTimeSheetEdit.ascx"  EditFormType="WebUserControl" >
                 <EditColumn UniqueName="EditCommandColumn1">
                 </EditColumn>      
             </EditFormSettings>


Thank you in advance!
 

Attachments

  • DemoSample.zip
    13.4 KB · Views: 19
Last edited:
Let's start off with what platform are you trying to do this on? ASP.NET, WinForms, WPF, UWP? The link you provided seems to point to ASP.NET using AJAX, but your use of e.Cancel suggests WinForms or WPF.
 
Let's start off with what platform are you trying to do this on? ASP.NET, WinForms, WPF, UWP? The link you provided seems to point to ASP.NET using AJAX, but your use of e.Cancel suggests WinForms or WPF.

Hi Skydiver.

Its telerik's ASP.NET Ajax.
 
Moving to ASP.NET 3rd Party...
 
What errors are you getting?
 
What errors are you getting?

I wasn't able to do this specific implementation properly at all.
All the information i found was with local DB and DataTable, so i just got syntax errors. When doing other other implementations, using DataBound event and commandName, i got no errors, and the control wasnt bound, so this should be the right way.
I was expecting some help on how to change the "DataRow" syntax on this Demo, for something appropriatte to my layers architecture...
 
Without seeing what the interfaces look like in your 3 tier architecture, it's kind of hard on us to even begin to point you down a particular direction. Your web interface is obviously your presentation layer. But what does your business logic layer look like? What does your data access layer look like? Or is all of that buried in the .ZIP file that you are expecting us to download and analyze? It would help us help you if you put the relevant information in a post instead of expecting us to dig. Recall that we are volunteers in this forum. We don't get paid to do this. The easier it is to understand what issues you are running into, the more likely we are to be engaged and try to help you.
 
Without seeing what the interfaces look like in your 3 tier architecture, it's kind of hard on us to even begin to point you down a particular direction. Your web interface is obviously your presentation layer. But what does your business logic layer look like? What does your data access layer look like? Or is all of that buried in the .ZIP file that you are expecting us to download and analyze? It would help us help you if you put the relevant information in a post instead of expecting us to dig. Recall that we are volunteers in this forum. We don't get paid to do this. The easier it is to understand what issues you are running into, the more likely we are to be engaged and try to help you.

Without seeing what the interfaces look like in your 3 tier architecture, it's kind of hard on us to even begin to point you down a particular direction. Your web interface is obviously your presentation layer. But what does your business logic layer look like? What does your data access layer look like? Or is all of that buried in the .ZIP file that you are expecting us to download and analyze? It would help us help you if you put the relevant information in a post instead of expecting us to dig. Recall that we are volunteers in this forum. We don't get paid to do this. The easier it is to understand what issues you are running into, the more likely we are to be engaged and try to help you.

Thank you for your feedback skydiver. I had the opposite idea: the bigger post, the worst, that's why i put it in a zip. I updated the question and shared relevant code!
 
Are you sure that you wrote that 3-tier architecture yourself? It seems like you don't know how to use it.

What I would do is call GetAllFiles() passing the ID that you got from line 10. If the list that comes back is not empty, then it should have only one item. Get a reference to this ListagemTimesheet from the list. If the list was empty create a new instance of ListagemTimesheet. Fill in the fields of this item with values pulled from the UI (e.g. your lines 16-21). Next if the list was not empty, call your UpdateFile(), otherwise call your SaveFile().

As a quick aside it looks like your InsertFile() is a misnamed. It looks like it essentially does an update instead of an insert. It's your SaveFile() which is actually doing the insert.
 
Back
Top Bottom