locked
SQL DataView will not update records? RRS feed

  • Question

  • User1909155429 posted

    i have declared SQLdatasource as such:

    <asp:SqlDataSource ID="SqlCorrespondance" runat="server" DataSourceMode="DataSet" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            
            
            SelectCommand="SELECT * FROM [Messages] WHERE ([MessageID_Reply] = @MessageID_Reply2)">
             <SelectParameters>
                 <asp:Parameter    Name="MessageID_Reply2" Type="Int64"  />
             </SelectParameters>
        </asp:SqlDataSource>
    

    but in code page i cannot seem to update messages table through Dataview source code:

     Dim dv As DataView = CType(SqlCorrespondance.Select(DataSourceSelectArguments.Empty), DataView)
      
            If Not dv Is Nothing Then
                dv.AllowNew = True
                Dim newRow As DataRowView = dv.AddNew()
                newRow.BeginEdit()
                newRow.Item("PRODUCTID") = ProdID
                newRow.Item("RefItemID") = RefItemID
                newRow.Item("MessageID_Reply") = MessageID.Value
                newRow.Item("subject") = lblSubject.Text
                newRow.Item("body") = Server.HtmlDecode(TxtReply.Text)
                newRow.Item("datentime") = Date.Now
                newRow.EndEdit()
              
    
            End If
         
            RepCorrespondance.DataSource = dv

    Sunday, October 18, 2020 2:19 PM

Answers

All replies

  • User-1330468790 posted

    Hi peterthegreat,

     

    If there is no error in your codes, the only possible reason is that you forget to call DataBind() method for the repeater control.

    Add the below line of codes after the DataSource line

    RepCorrespondance.DataSource = dv
        RepCorrespondance.DataBind()

     

    I tested your codes and could not understand the rationale of your design.

    You don't store the new row into the database? Just directly display it?

    The common implementation would be inserting the new row to the database and then select the updated result from database and bind the data to the controls. 

    Could you please check if you need to modify the current codes?

     

    Hope helps.

    Best regards,

    Sean

    Monday, October 19, 2020 3:42 AM
  • User1909155429 posted

    i have not used this method before and presumed it would update database automatically?

    So do i need to include insert statements to my SQLDatasource object wizard for it to work?

    Thanks

    Thursday, October 22, 2020 1:41 PM
  • User-1330468790 posted

    Hi peterthegreat,

     

    You definitely should use this method as long as you want to bind data to the data control.

    It does nothing about database but only deal with the data binding for the contorls.

     

    You could refer to this link:

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.repeater.databind?view=netframework-4.8

     

    Hope helps.

    Best regards,

    Sean

    Friday, October 23, 2020 9:58 AM
  • User1909155429 posted

    So, the database table will not be updated with the values added? 

    it just displays the data in the bound repeater control? 

    What purpose does it then serve or where else does it serve?

    Friday, October 23, 2020 1:58 PM
  • User475983607 posted

    So, the database table will not be updated with the values added? 

    Correct.  Database tables are not affected by updating a DataView.  

    In ADO.NET, the DataAdapter is used to update a data source. 

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/updating-data-sources-with-dataadapters

    it just displays the data in the bound repeater control? 

    The DataView  is a UI construct used to present different "data views" of an in memory DataTable .  For example, sorting.  The DataView is disconnected from the data store.

    What purpose does it then serve or where else does it serve?

    Please see the DataView reference documentation.

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/dataviews

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 23, 2020 2:38 PM