469,588 Members | 2,829 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

VB.NET: How to Enter Values in AutoIncrement Columns of DataTable.

Hello,

I am trying to create a Dataset and save it to an XML file. I have one column “JobID” for which I have set the autoincrement property to true. Here is the function that creates and save the dataset:

Public Sub CreateDataSet()

Dim tbMain As DataTable = New DataTable("UploadLog")

'Job ID – Auto increments
Dim drJobID As New DataColumn("JobID")
drJobID.DataType = Type.GetType("System.Int64")
drJobID.Unique = True
drJobID.AllowDBNull = False
drJobID.ReadOnly = True
drJobID.AutoIncrement = True
drJobID.AutoIncrementSeed = 1000
drJobID.AutoIncrementStep = 1
tbMain.Columns.Add(drJobID)


'Upload Type
Dim drUploadType As New DataColumn("UploadType")
drUploadType.DataType = Type.GetType("System.String")
drUploadType.AllowDBNull = False
tbMain.Columns.Add(drUploadType)

'Status
Dim drStatus As New DataColumn("Status")
drStatus.DataType = Type.GetType("System.String")
tbMain.Columns.Add(drStatus)


Dim ds As New DataSet
ds = New DataSet("MediaTraqLogs")
ds.Tables.Add(tbMain)

ds.WriteXmlSchema(sFileName)

End Sub


Now if I try to add new row in this data table the auto increment column does not auto-increments. Here is how I am adding row into this datatable:

Public Sub AddDataRow(ByVal Status As String, ByVal UploadType As String)

Dim DS As New DataSet()

DS.ReadXml(sFileName)

Dim tbUpload As DataTable = DS.Tables("UploadLog")

Dim oRow As DataRow = tbUpload.NewRow

oRow.Item("Status") = Status
oRow.Item("UploadType") = UploadType

tbUpload.Rows.Add(oRow)

DS.AcceptChanges()

DS.WriteXml(sFileName)

End Sub

But for some reasons it give first row as Job ID = 1000 and does not give any JobID to next rows which I add to this table.

Please help, I don’t understand what I am doing wrong in here.
Sep 12 '07 #1
2 5028
Try declaring the DS as Global(to use it only once). I think that should work.

Thanks
Sep 13 '07 #2
Hi Thanks for the reply.

I figured it out. What I did was every time inserting a row before reading XML file I would create the schema for my DS. And it did the trick. Anyways, Thanks very much for the reply.
Sep 13 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.