473,394 Members | 1,787 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,394 software developers and data experts.

Updating a row in a dataset

Here's the scenerio: I am importing records into my database through the
program, the first time through the database is empty and so is the dataset.
Everything works fine until the second time through and I'm bring in a new
set of records, that may or may not already exist. I populate the dataset
and if the record doesn't already exist in the database/dataset, I add a new
row to the dataset. However, the problem comes in when the record does
already exist in the database/dataset. I don't want to delete the record, I
just want to modify some of the fields. How can I update a datarow in a
dataset with modified fields that I've collected in a datarow? Once I have
the dataset updated, I then pass it to my DataAdapter to write back to the
database. This is done after all the rows have been added or modified. Is
this possible or do I need to do something different with modified rows?

Code sample:
DataRow1 = FindExisting(DataSetMain, comparisonfield1, comparisonfield2)

if DataRow1 isnot nothing then
DataRowNew(0) = DataRow1(0)

DataRowNew("ID") = DataRow1("ID")

DataRowNew("fieldsupdated") = updatedvalues

DataRow1(0) = DataRowNew(0)

???? How to add/update the DataRow1 to DataSetMain ????
else
DataRowNew = newvalues

DataSetMain.Rows.Add(DataRowNew)
end if

DataAdapter.UpdateTable(DataSetMain) 'Updates the database

***********
Public Function UpdateTable(ByRef Target As DataTable, _
Optional ByVal Source As String = Nothing, _
Optional ByVal Trans As SqlTransaction =
Nothing) As Boolean

Try
If Source = Nothing Then
Source = "SELECT * FROM " & Target.TableName
End If

Using lcn As New SqlClient.SqlConnection(mstrConnString)
Using lda As New SqlClient.SqlDataAdapter(Source, lcn)
Using lbd As New SqlClient.SqlCommandBuilder(lda)
With lda
.InsertCommand = lbd.GetInsertCommand
.UpdateCommand = lbd.GetUpdateCommand
.DeleteCommand = lbd.GetDeleteCommand

.Update(Target)

Return True

End With
End Using
End Using
End Using

Catch ex As System.Exception
Debug.WriteLine("UpdateTable: " & ex.Message)

Return False

End Try

End Function

--
Cindy
Dec 10 '07 #1
0 1098

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

Similar topics

0
by: Robin | last post by:
In a .Net solution that has a DAL, Class and Business logic. When recreating a dataset from the class the records are inserted into the database from the dataset instead of updating. This is using...
5
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
10
by: jaYPee | last post by:
does anyone experienced slowness when updating a dataset using AcceptChanges? when calling this code it takes many seconds to update the database SqlDataAdapter1.Update(DsStudentCourse1)...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
4
by: Geoff | last post by:
Hi I'm hoping somebody can help me with the following problem that has occurred to me. Suppose I have two tables in an SQL Server database. Let's call these tables A and B. Assume that A has...
2
by: susan.f.barrett | last post by:
Hi, Despite me being able to type the following in to SQL Server and it updating 1 row: > updatestockcategory 1093, 839 In my code, it is not updating any rows. dataSet = new DataSet();
6
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
6
by: mike11d11 | last post by:
I'm trying to create an application that will have multiple users working off a table on a SQL server. Since multi users will be updating different records at any given moment, how can i get those...
0
by: OldStd | last post by:
Updating data using 2 data sets I am having some problems in updating the database using two datasets as suggested by someone. 1. Data is displayed in a data grid from a dataset generated using...
1
by: jonbartlam | last post by:
Hi There I'm not sure what exactly is going wrong here. I'm writing an application that retreives a table from a database (tbl_internalfaults) and updates it. (Actually, just the status column will...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.