multiple insert into a grid - when error - doesn't refresh the grid

3 posts, 0 answers
  1. Myriam
    Myriam avatar
    153 posts
    Member since:
    Sep 2008

    Posted 10 Sep 2009 Link to this post

    Let me explain.
    I have a grid which is made like that:

    <td valign=bottom>  
                                        <telerik:RadGrid   
                                            ID="RadGrid2"   
                                            Skin="Vista"   
                                            runat="server"   
                                            OnNeedDataSource="RadGrid2_NeedDataSource" 
                                            ShowFooter=true   
                                            CommandItemStyle-HorizontalAlign=Center>    
                                              <MasterTableView   
                                                ShowFooter=true    
                                                CommandItemDisplay="bottom"   
                                                EditMode="InPlace" >    
                                                  <CommandItemTemplate> 
                                                      <asp:LinkButton ID="LinkButton1" CommandName="UpdateChanges" Runat="server" CssClass="TexteBlanc16">Importer tous</asp:LinkButton> 
                                                  </CommandItemTemplate> 
                                                  <Columns>                       
                                                      <telerik:GridTemplateColumn UniqueName="CodePermanent" HeaderText="Code Permanent">    
                                                          <ItemTemplate>    
                                                              <asp:TextBox ID="TxtCodePermanent" runat="server"></asp:TextBox>    
                                                          </ItemTemplate>    
                                                      </telerik:GridTemplateColumn>    
                                                 </Columns>    
                                           </MasterTableView>    
                                        </telerik:RadGrid>   
                                    </td> 
                                    <td valign=bottom>  
                                        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Ajouter une ligne" /> 
                                    </td> 

    Public Sub RadGrid2_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)  
            If Not IsPostBack Then  
                dt = AddRow(dt)  
                ' call the method to create row   
                ViewState("dt") = dt  
            End If  
            dt = DirectCast(ViewState("dt"), DataTable)  
            RadGrid2.DataSource = dt 
        End Sub  
     
    Private Function AddRow(ByVal dt As DataTable) As DataTable  
            ' method to create row   
            Dim dr As DataRow = dt.NewRow()  
            dr("RowNumber") = ""  
            dt.Rows.Add(dr)  
            Return dt  
        End Function  
     
     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)  
            Dim countitem As Integer  
            Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable)  
     
            countitem = RadGrid2.Items.Count  
            Dim cpt As Integer = 0 
            For Each Item As GridDataItem In RadGrid2.Items  
     
                Dim txtcode As TextBox = DirectCast(Item("CodePermanent").FindControl("TxtCodePermanent"), TextBox)  
                If Not (txtcode.Text Is Nothing) And txtcode.Text <> "" Then  
                    liste.Add(txtcode.Text)  
     
                End If  
     
            Next  
            ViewState("dt") = AddRow(dt)  
            listcount = liste.Count  
     
            RadGrid2.Rebind()  
            While cpt < listcount 
                Dim txtcode As TextBox = DirectCast(RadGrid2.Items(cpt)("CodePermanent").FindControl("TxtCodePermanent"), TextBox)  
                txtcode.Text = liste.Item(cpt)  
                cptcpt = cpt + 1  
            End While  
        End Sub  
     
        Public Sub RadGrid2_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid2.ItemCommand  
            If e.CommandName = "UpdateChanges" Then  
                For Each Item As GridDataItem In RadGrid2.Items  
                    Dim txtcode As TextBox = DirectCast(Item("CodePermanent").FindControl("TxtCodePermanent"), TextBox)  
                    If Not (txtcode.Text Is Nothing) Then  
                        ImporterEtudiants(txtcode.Text)  
     
                    End If  
                Next  
                Me.RadGrid_BanqueCV.Rebind()  
                If CodePermInvalide <> "" Then  
                    Session("sCodePermInvalide") = CodePermInvalide  
     
                    Response.Redirect("FEN_ERREUR.aspx?NoErreur=3")  
                End If  
     
            End If  
        End Sub  
     
        Public Function ImporterEtudiants(ByVal sCodePerm As String) As Boolean  
            Dim strSQLIdent As String  
            Dim cmdIdent As New SqlCommand  
            Dim myDAIdent As New SqlDataAdapter  
            Dim dsIdent As New DataSet  
            Dim strSQLIdentValide As String  
            Dim cmdIdentValide As New SqlCommand  
            Dim myDAIdentValide As New SqlDataAdapter  
            Dim dsIdentValide As New DataSet  
            Dim strSQLIdentInsert As String  
            Dim cmdIdentInsert As New SqlCommand  
            Dim myDAIdentInsert As New SqlDataAdapter  
            Dim dsIdentInsert As New DataSet  
            Dim DatedeNaissance As Date  
     
            strSQLIdentValide = "SELECT * FROM T_USAGER WHERE CODE_PERMANENT ='" & sCodePerm & "'"  
            cmdIdentValide = New SqlCommand(strSQLIdentValide, Conn)  
            myDAIdentValide = New SqlDataAdapter(cmdIdentValide)  
            Conn.Open()  
            myDAIdentValide.Fill(dsIdentValide)  
            Conn.Close()  
            If dsIdentValide.Tables(0).Rows.Count > 0 Then 'L'usager existe déjà dans la table T_USAGER  
                Effacer = 1 
                Return True  
            Else  
     
                strSQLIdent = "SELECT E_Ele.Fiche,CodePerm,DateNaiss,Nom,Pnom,NoCiv,Rue,Ville,CodePost,IndReg,Tel " & _  
                "FROM E_Ele " & _  
                "INNER JOIN E_Adr " & _  
                "ON E_Ele.Fiche = E_Adr.Fiche " & _  
                "INNER JOIN E_Freq ON E_Ele.Fiche = E_Freq.Fiche " & _  
                "GROUP BY E_Ele.Fiche, E_Ele.CodePerm, E_Ele.DateNaiss, E_Ele.Nom, E_Ele.Pnom, E_Adr.NoCiv, E_Adr.Rue, E_Adr.Ville, E_Adr.CodePost, E_Adr.IndReg, E_Adr.Tel, E_Adr.DateFin, E_Adr.EnvoiMeq " & _  
                "HAVING (((Max(E_Freq.DateFin))>'2008-01-01') AND ((E_Adr.DateFin)='') AND ((E_Adr.EnvoiMeq)='1')) AND CodePerm='" & sCodePerm & "'"  
     
                cmdIdent = New SqlCommand(strSQLIdent, ConnJadeAdulte)  
                myDAIdent = New SqlDataAdapter(cmdIdent)  
                ConnJadeAdulte.Open()  
                myDAIdent.Fill(dsIdent)  
                ConnJadeAdulte.Close()  
                If dsIdent.Tables(0).Rows.Count > 0 Then  
                    DatedeNaissance = Left(dsIdent.Tables(0).Rows(0).Item("DateNaiss"), 4) & "-" & Mid(dsIdent.Tables(0).Rows(0).Item("DateNaiss"), 5, 2) & "-" & Right(dsIdent.Tables(0).Rows(0).Item("DateNaiss"), 2)  
                    strSQLIdentInsert = "INSERT INTO T_USAGER (CODE_PERMANENT,MOT_PASSE,PRENOM,NOM,ADRESSE,VILLE,CODE_POSTAL,NO_TEL,COURRIEL,DATE_NAISSANCE,LETTRE_CONSENTEMENT,RECEVOIR_NOUVELLE_OFFRE,DIPLOME, PREMIERE_FOIS) " & _  
                                        "VALUES ('" & sCodePerm & "'," & dsIdent.Tables(0).Rows(0).Item("DateNaiss") & ",'" & Replace(Trim(dsIdent.Tables(0).Rows(0).Item("Pnom")), ",", "''") & "', '" & Replace(Trim(dsIdent.Tables(0).Rows(0).Item("Nom")), "'", "''") & "','" & Replace(Trim(dsIdent.Tables(0).Rows(0).Item("NoCiv")), "'", "''") & " " & Replace(Trim(dsIdent.Tables(0).Rows(0).Item("Rue")), "'", "''") & "','" & Replace(Trim(dsIdent.Tables(0).Rows(0).Item("Ville")), "'", "''") & "','" & dsIdent.Tables(0).Rows(0).Item("CodePost") & "','" & dsIdent.Tables(0).Rows(0).Item("IndReg") & dsIdent.Tables(0).Rows(0).Item("Tel") & "','','" & DatedeNaissance & "',0,0,0,1)"  
                    cmdIdentInsert = New SqlCommand(strSQLIdentInsert, Conn)  
                    myDAIdentInsert = New SqlDataAdapter(cmdIdentInsert)  
                    Conn.Open()  
                    myDAIdentInsert.Fill(dsIdentInsert)  
                    Conn.Close()  
                    Effacer = 1 
                    Return True  
                Else  
                    If CodePermInvalide = "" Then  
                        CodePermInvalide = sCodePerm 
                    Else  
                        CodePermInvalideCodePermInvalide = CodePermInvalide & " <br> " & sCodePerm  
                    End If  
                    Effacer = 0 
                    Return False  
                End If  
            End If  
             
              
        End Function  
     
         
        Private Sub RadGrid2_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid2.PreRender  
            RadGrid2.MasterTableView.RenderColumns(1).Display = False 
            RadGrid2.MasterTableView.RenderColumns(3).Display = False 
        End Sub 
    Often, the user want to add more than one data at the time so I put 15 lines which are in edit mode. The user can enter 3 rows and then click on the "importer tous" button which valid if the values are ok. If they are, it's working fine. But if only 1 is not good I put it in a variable "CodePermInvalide" and redirect to an error page which display the CodePermInvalide value. Then I got a button on my error page like this:
    <HREF="Javascript:history.go(-1)" >Retour</a> 
    When I click on this link, I'm back to my first page and I see all my 3 records, but I would like to see only the values which are not good. It insert the good value in my database so I would like to not see them when I come back to the first page.
    Hope that make sense. If you need more informations, please be free to ask as I don't know if I'm clear enought.
    Thank you!
  2. Kiara
    Kiara avatar
    309 posts
    Member since:
    Jul 2006

    Posted 15 Sep 2009 Link to this post

    Myriam, I suppose this line loads previuos page from the browser history:

    <HREF="Javascript:history.go(-1)" >Retour</a> 

    If so, if the grid source is updated and the control is rebound, it will show all items. If you want to add/update only items which passed validation, cancel the operation for those which do not by setting e.Canceled = true inside the ItemCommand handler's conditional block.

    Kiara 
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Myriam
    Myriam avatar
    153 posts
    Member since:
    Sep 2008

    Posted 15 Sep 2009 Link to this post

    Unfortunetly, it doesn't work
    please see this link to see what I see before clicking on "Importer tous" button and this is the same thing I see when I click on the "Retour" button.
    [url=http://img98.imageshack.us/i/importation.png/][img=http://img98.imageshack.us/img98/5342/importation.png][/url]
    Note: the POUG05036005  didn't pass but the 2 others passed.
    And this is where I put the e.canceled=true
    Public Sub RadGrid2_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid2.ItemCommand  
            Dim CptBon As Integer = 0  
            Dim cpt As Integer = 0  
            Dim status As Boolean 
            If e.CommandName = "UpdateChanges" Then 
                For Each Item As GridDataItem In RadGrid2.Items  
                    Dim txtcode As TextBox = DirectCast(Item("CodePermanent").FindControl("TxtCodePermanent"), TextBox)  
                    If Not (txtcode.Text Is NothingThen 
                        status = ImporterEtudiants(txtcode.Text)  
                        If status = False Then 
                            e.Canceled = True 
                        End If 
                    End If 
                Next 
                Me.RadGrid2.Rebind()  
                Me.RadGrid_BanqueCV.Rebind()  
                If CodePermInvalide <> "" Then 
                    Session("sCodePermInvalide") = CodePermInvalide  
     
                    Response.Redirect("FEN_ERREUR.aspx?NoErreur=3")  
                End If 
            End If 
        End Sub 

    Any other idea?
Back to Top