473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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.Cells(2).Controls(1),
DropDownList)
Dim NewID As Integer = DDL.SelectedIndex
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 dsTechnicianList As DataTable
dsTechnicianList =
CType(HttpContext.Current.Session("ILApplicators") ,
System.Data.DataTable)
Dim dcFirst As DataColumn = dsTechnicianList.Columns(0)
Dim dcPrimary(1) As DataColumn
dcPrimary(0) = dcFirst
dsTechnicianList.PrimaryKey = dcPrimary

NewRow(0) = RowID
NewRow(7) = SupervisorName
NewRow(8) = SupervisorID
Dim myRow As DataRow
dsTechnicianList.BeginLoadData()
myRow = dsTechnicianList.LoadDataRow(NewRow, True)
dsTechnicianList.EndLoadData()
Any help or ideas would be GREATLY appreciated!

TIA,
Mike
Nov 20 '05 #1
1 4365
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.com> wrote:
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.Cells(2).Controls(1),
DropDownList)
Dim NewID As Integer = DDL.SelectedIndex
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 dsTechnicianList As DataTable
dsTechnicianList =
CType(HttpContext.Current.Session("ILApplicators" ),
System.Data.DataTable)
Dim dcFirst As DataColumn = dsTechnicianList.Columns(0)
Dim dcPrimary(1) As DataColumn
dcPrimary(0) = dcFirst
dsTechnicianList.PrimaryKey = dcPrimary

NewRow(0) = RowID
NewRow(7) = SupervisorName
NewRow(8) = SupervisorID
Dim myRow As DataRow
dsTechnicianList.BeginLoadData()
myRow = dsTechnicianList.LoadDataRow(NewRow, True)
dsTechnicianList.EndLoadData()
Any help or ideas would be GREATLY appreciated!

TIA,
Mike


Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bill Toulias | last post by:
I want to be able to display the tuples of a select query (which may change at runtime, so no prior knowledge for column characteristics) and edit them as well. I used this code, that executes...
1
by: Mike | last post by:
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...
6
by: Danny Ni | last post by:
Hi, If I want to programatically add rows to a DataTable, do I call AcceptChanges per row? Or do I call AcceptChanges after all rows added? TIA
0
by: Flack | last post by:
I have pasted at the end of this message a small sample program I whipped up to do some testing. It's a form with a datagrid and two buttons. Each button clears the dataTable that is the source...
1
by: cindy | last post by:
this is the call private void Page_Load(object sender, System.EventArgs e) { OdbcConnection connection = new OdbcConnection ("DSN=PFW52"); CreateDataAdapter(connection); } this is the code,...
1
by: Tim Cowan | last post by:
Hi, I was wondering if there is a quicker way to do this? I have a select all check box for data in a grid and when checked I want the checkboxes in the grid to be checked. I am currently...
4
by: George | last post by:
Got a question about the side effect of DataAdapter.Update() and DataTable.GetChanges(). Say I set up a DataTable and a DataAdapter in a class. Delete (Not remove) a row in the data table and...
3
by: John Cosmas | last post by:
I have a DATATABLE which I have populated in my application, and I need it written out to a particular table I specify in my ACCESS database. My code works to the point of the MERGE and UPDATE,...
11
by: inpuarg | last post by:
I have 2 datatables. They are identical. I want to compare them by cell's content. They are all same. But dt1 == dt2 or dt1.GetHashCode() == dt2.GetHashCode() doesn 't work. There are big...
12
by: joaotsetsemoita | last post by:
Hello everyone, im completly new to vb.net and I was assigned to do a simple piece of software that just had to select from um db in a MS access data base and insert into a SQL server Database....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.