table not updated with identity column of inserted row
table not updated with identity column of inserted row
table not updated with identity column of inserted row
table not updated with identity column of inserted row
table not updated with identity column of inserted row
table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row
table not updated with identity column of inserted row table not updated with identity column of inserted row
table not updated with identity column of inserted row
Go Back  Xtreme Visual Basic Talk > > > table not updated with identity column of inserted row


Reply
 
Thread Tools Display Modes
  #1  
Old 06-03-2014, 03:21 PM
AlanS AlanS is offline
Newcomer
 
Join Date: May 2003
Location: Philadelphia, PA, US
Posts: 18
Default table not updated with identity column of inserted row


This is a VB.Net 2005 application. EPConn is an already open SQLConnection object. The Packet table's primary key is PacketID (defined as
Code:
PacketID INT IDENTITY PRIMARY KEY CLUSTERED NOT NULL
). After executing this code, a new record has been properly added to the table, but it shows up in tbl with no value for the PacketID column. What do I need to do to capture that value in tbl?

Code:
Dim da As SqlDataAdapter, ds As DataSet, cb As SqlCommandBuilder, tbl As DataTable
Dim SelectStatement As String, NewRow As DataRow
SelectStatement = "SELECT * FROM Packet"
da = New SqlClient.SqlDataAdapter(SelectStatement, EPConn)
ds = New DataSet
cb = New SqlCommandBuilder(da)
da.Fill(ds, "Packet")
tbl = ds.Tables("Packet")
NewRow = tbl.NewRow
With NewRow
  .Item("FileName") = "TestFile"
  .Item("FileTimeStamp") = Now
  .Item("ProviderID") = 123
  'code to populate remaining columns
  '...
  'end code to populate remaining columns
End With
tbl.Rows.Add(NewRow)
da.Update(ds, tbl.TableName)
ds.AcceptChanges()
tbl.AcceptChanges()
tbl.Rows(tbl.Rows.Count - 1).AcceptChanges()
Reply With Quote
  #2  
Old 06-03-2014, 04:47 PM
AlanS AlanS is offline
Newcomer
 
Join Date: May 2003
Location: Philadelphia, PA, US
Posts: 18
Default

Solved. I removed the last three lines (various calls to AcceptChanges()), and replaced them with these two lines:

tbl.Clear()
da.Fill(ds, "Packet")

which clears out the table and then re-populates it from the database. Without the Clear() call, the table contains duplicate rows.
Reply With Quote
Reply

Tags
identity column datatable


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
table not updated with identity column of inserted row
table not updated with identity column of inserted row
table not updated with identity column of inserted row table not updated with identity column of inserted row
table not updated with identity column of inserted row
table not updated with identity column of inserted row
table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row table not updated with identity column of inserted row
table not updated with identity column of inserted row
table not updated with identity column of inserted row
 
table not updated with identity column of inserted row
table not updated with identity column of inserted row
 
-->