Help with DataTable update (BeginLoadData method)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    Help with DataTable update (BeginLoadData method)

    I have an ASP.NET/VB app that updates values in a DataTable over the
    course of about 3 different pages. On the way out of the first of
    these pages, I explicitly build the DataTable from values in a
    DataGrid, and set the PrimaryKey of the DataTable to be the first cell
    in the grid (which is a UserID value). I then store the DataTable in
    a session object, from which it is retrieved for subsequent pages.
    All this seems to be working fine.

    On the second page, I retrieve the DataTable from the session object,
    apply a filter to it, and display the filtered data in a DataGrid. In
    this grid, the user can change the data, specifically assigning an
    employee to a different supervisor. The Grid is populated with the
    current employee/supervisor assignment in the database, and then any
    necessary changes are made by the user. This, too, seems to be
    working fine.

    The problem I'm having is that on the update command of the Grid,
    which is where I'm updating the DataTable (the thought being that I
    would just do an "in-place" update of the DataTable and then rebind
    the grid). I am able to trap the new assignment values (which are set
    in a listbox within the grid) and can trap the employee ID value from
    the grid (which is my primary key), but whenever I call the
    LoadDataRow method, rather than updating the row based on the primary
    key, a new row insert is *attempted*. I say attempted because I get
    an error on the EndLoadData method, which I believe is due to not
    passing in values for all the columns.

    I was hoping to be able to just pass in the key value and the values
    of the columns that had changed, but apparently that is not
    sufficient. Which is the first question - when attempting a
    LoadDataRow call, do I need to pass in valid values for *all* of the
    columns in the datatable (except, of course, those for which there are
    default values, of which I have none in this instance). You'll see in
    my example code that I'm just passing in the first, eighth and ninth
    columns, which I suspect is the problem.

    However, the *real* question is why isn't the LoadDataRow method
    finding the pre-existence of the key value in the table? I've checked
    and the value returned by my code is exactly the same as the one in
    the table, length is the same, etc. I set the primary key when I
    first build the table on the previous page, but I thought perhaps that
    attribute somehow gets lots when I set the DataTable to a session
    object so, as you will see in the example, I explicitly set the
    primary key again on this page, which seems to work, but the code
    still inserts a new row (or attempts to).

    Here is the relevant code from the page in question:

    Dim DDL As DropDownList = CType(e.Item.Ce lls(2).Controls (1),
    DropDownList)
    Dim NewID As Integer = DDL.SelectedInd ex
    Dim SupervisorID As String = DDL.Items(NewID ).Value
    Dim SupervisorName As String = DDL.Items(NewID ).Text
    Dim NewRow(9) As Object
    Dim Key As TextBox = e.Item.Cells(1) .Controls(0)
    Dim RowID As String = Key.Text

    Dim dsTechnicianLis t As DataTable
    dsTechnicianLis t =
    CType(HttpConte xt.Current.Sess ion("ILApplicat ors"),
    System.Data.Dat aTable)
    Dim dcFirst As DataColumn = dsTechnicianLis t.Columns(0)
    Dim dcPrimary(1) As DataColumn
    dcPrimary(0) = dcFirst
    dsTechnicianLis t.PrimaryKey = dcPrimary

    NewRow(0) = RowID
    NewRow(7) = SupervisorName
    NewRow(8) = SupervisorID
    Dim myRow As DataRow
    dsTechnicianLis t.BeginLoadData ()
    myRow = dsTechnicianLis t.LoadDataRow(N ewRow, True)
    dsTechnicianLis t.EndLoadData()


    Any help or ideas would be GREATLY appreciated!

    TIA,
    Mike


  • Mike

    #2
    Re: Help with DataTable update (BeginLoadData method)

    I found the answer to my own problem. Although it is not documented
    you must call the AcceptChanges method *before* calling the
    LoadDataRow method. This really doesn't make sense, as passing in
    True for the fAcceptChanges argument of LoadDataRow forces it to be
    called in conjunction with LoadDataRow.

    At any rate, I added AcceptChanges just before LoadDataRow and all is
    well!




    On Thu, 04 Dec 2003 19:22:46 GMT, Mike
    <ga_harley_guy@ _REMOVE_yahoo.c om> wrote:
    [color=blue]
    >I have an ASP.NET/VB app that updates values in a DataTable over the
    >course of about 3 different pages. On the way out of the first of
    >these pages, I explicitly build the DataTable from values in a
    >DataGrid, and set the PrimaryKey of the DataTable to be the first cell
    >in the grid (which is a UserID value). I then store the DataTable in
    >a session object, from which it is retrieved for subsequent pages.
    >All this seems to be working fine.
    >
    >On the second page, I retrieve the DataTable from the session object,
    >apply a filter to it, and display the filtered data in a DataGrid. In
    >this grid, the user can change the data, specifically assigning an
    >employee to a different supervisor. The Grid is populated with the
    >current employee/supervisor assignment in the database, and then any
    >necessary changes are made by the user. This, too, seems to be
    >working fine.
    >
    >The problem I'm having is that on the update command of the Grid,
    >which is where I'm updating the DataTable (the thought being that I
    >would just do an "in-place" update of the DataTable and then rebind
    >the grid). I am able to trap the new assignment values (which are set
    >in a listbox within the grid) and can trap the employee ID value from
    >the grid (which is my primary key), but whenever I call the
    >LoadDataRow method, rather than updating the row based on the primary
    >key, a new row insert is *attempted*. I say attempted because I get
    >an error on the EndLoadData method, which I believe is due to not
    >passing in values for all the columns.
    >
    >I was hoping to be able to just pass in the key value and the values
    >of the columns that had changed, but apparently that is not
    >sufficient. Which is the first question - when attempting a
    >LoadDataRow call, do I need to pass in valid values for *all* of the
    >columns in the datatable (except, of course, those for which there are
    >default values, of which I have none in this instance). You'll see in
    >my example code that I'm just passing in the first, eighth and ninth
    >columns, which I suspect is the problem.
    >
    >However, the *real* question is why isn't the LoadDataRow method
    >finding the pre-existence of the key value in the table? I've checked
    >and the value returned by my code is exactly the same as the one in
    >the table, length is the same, etc. I set the primary key when I
    >first build the table on the previous page, but I thought perhaps that
    >attribute somehow gets lots when I set the DataTable to a session
    >object so, as you will see in the example, I explicitly set the
    >primary key again on this page, which seems to work, but the code
    >still inserts a new row (or attempts to).
    >
    >Here is the relevant code from the page in question:
    >
    >Dim DDL As DropDownList = CType(e.Item.Ce lls(2).Controls (1),
    >DropDownList )
    > Dim NewID As Integer = DDL.SelectedInd ex
    > Dim SupervisorID As String = DDL.Items(NewID ).Value
    > Dim SupervisorName As String = DDL.Items(NewID ).Text
    > Dim NewRow(9) As Object
    > Dim Key As TextBox = e.Item.Cells(1) .Controls(0)
    > Dim RowID As String = Key.Text
    >
    > Dim dsTechnicianLis t As DataTable
    > dsTechnicianLis t =
    >CType(HttpCont ext.Current.Ses sion("ILApplica tors"),
    >System.Data.Da taTable)
    > Dim dcFirst As DataColumn = dsTechnicianLis t.Columns(0)
    > Dim dcPrimary(1) As DataColumn
    > dcPrimary(0) = dcFirst
    > dsTechnicianLis t.PrimaryKey = dcPrimary
    >
    > NewRow(0) = RowID
    > NewRow(7) = SupervisorName
    > NewRow(8) = SupervisorID
    > Dim myRow As DataRow
    > dsTechnicianLis t.BeginLoadData ()
    > myRow = dsTechnicianLis t.LoadDataRow(N ewRow, True)
    > dsTechnicianLis t.EndLoadData()
    >
    >
    >Any help or ideas would be GREATLY appreciated!
    >
    >TIA,
    >Mike
    >[/color]

    Comment

    Working...