locked
update is not working RRS feed

  • Question

  • User-1106823036 posted

    I have a page with textboxes and ckeditors where I want to update some fields

    I have 2 tables the first table includes the main data and the second table includes related data (with foreign key the first record ID)

    so when hitting the update button it updates the record in the first table(main data) and not updating the record in the second table( the error shows that the textboxes are empty)

    here is my code:please forgive me if I didnt explain well im new to vb

     <EditItemTemplate>
    //here goes the textboxes that contain the main data where I have no problem
    
    //and here is the update of the second table's fields
     <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource8">
                                                 <ItemTemplate>
                                                     <td width="40%" valign="top">
                                                     <asp:TextBox ID="txt31" runat="server" Style="width: 90%;" cols="6" Rows="5" Text='<%# Bind("Youtube") %>'></asp:TextBox>
                                                 </td>
                                                 <td width="20%" valign="top">
                                                     <asp:TextBox ID="txt221" runat="server" Style="width: 90%;" cols="6" Rows="5" Text='<%# Bind("Title") %>'></asp:TextBox>
                                                 </td>
                                                 <td width="20%" valign="top">
                                                     <asp:TextBox ID="txt41" runat="server" Style="width: 90%;" cols="6" Rows="5" Text='<%# Bind("Title_ar") %>'></asp:TextBox>
                                                 </td>
                                              </ItemTemplate>
                                             </asp:Repeater> 
                                              
                                                    
                         
                                         
                                            <asp:SqlDataSource runat="server" ID="SqlDataSource8" ConnectionString='<%$ ConnectionStrings:MyConn %>' SelectCommand="SELECT * FROM [Active_LicensesTrainingYoutube] WHERE ([Actualite_ID] = @Actualite_ID)">
                                         <SelectParameters>
                                             <asp:QueryStringParameter QueryStringField="Item_ID" Name="Actualite_ID" Type="Int32"></asp:QueryStringParameter>
                                         </SelectParameters>
                                     </asp:SqlDataSource>

    the update query of the first table goes in the aspx page itself using this code

     <asp:SqlDataSource ID="ManageData" runat="server" 
    UpdateCommand="UPDATE [Active_Training] SET [Item_Desc_ar] = @Item_Desc_ar,[datetxt] = @dat................
    <UpdateParameters>
                                 <asp:Parameter Name="News_Date" Type="DateTime" />
                               <asp:Parameter Name="Cat_ID" Type="Int32" />
                               <asp:Parameter Name="SubCat_ID" Type="Int32" />................
    and so on

    the update of the second table goes in the backcode

      Dim youtube As TextBox
            youtube = CType(Me.FormView1.FindControl("txt31"), TextBox)
    
           
    
    
            Dim titleEn As TextBox
            titleEn = CType(Me.FormView1.FindControl("txt221"), TextBox)
    
           
            Dim titleAr As TextBox
            titleAr = CType(Me.FormView1.FindControl("txt41"), TextBox)
    
            
    
      SQLExec.Execute("Update Active_LicensesTrainingYoutube SET Youtube = '" & youtube.Text & "', Title = N'" & titleEn.Text  & "', Title_ar =  N'" & titleAr.Text  & "'  where Actualite_ID=" & Item_ID & "")

    I tried putting random data in the above query, It's working but when getting the data from the textboxes it throws an error Object reference not set to an instance of an object. at youtube.Text

    so I can assume it is not reading the texbox value

    what is going wrong.

    NOTE: if I remove the repeater , and I enter data into the textboxes it will update correctly, but I'm using the repeater to retrieve previous saved data

    Tuesday, January 26, 2016 8:38 AM

Answers

  • User-1716253493 posted

    txt31 is inside repeater items

    First, get the repeater first then loop the items to get textboxes

            Dim Repeater1 As Repeater = CType(FormView1.FindControl("Repeater1"), Repeater)
            For Each item As RepeaterItem In Repeater1.Items
                Dim txt31 As TextBox = CType(item.FindControl("txt31"), TextBox)
            Next

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 26, 2016 8:54 AM