473,506 Members | 11,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataTable and AutoIncrement

I have set a DataTable and one of the columns I set "AutoIncrement" to True.
I then populate the Table by setting the columns to values then add the row
to the table. I inadverently set the AutoIncrement Columns to different
values but didn't get any errors. Should I be able to set the value of an
AutoIncrement Column? I would have thought it couldn't be done as the column
value was set when a row was added.
--
Dennis in Houston
Nov 21 '05 #1
6 16106
Dennis,

I think that you can the best ave a look at this

autoincrement class
http://msdn.microsoft.com/library/de...ementtopic.asp

You cannot set the values, they will not even been in the database.
Setting the seed to -1 is the advised method.

See for this as well the article of Bill
http://msdn.microsoft.com/library/de...anidcrisis.asp

I hope this helps?

Cor

"Dennis" <De****@discussions.microsoft.com>
I have set a DataTable and one of the columns I set "AutoIncrement" to
True.
I then populate the Table by setting the columns to values then add the
row
to the table. I inadverently set the AutoIncrement Columns to different
values but didn't get any errors. Should I be able to set the value of an
AutoIncrement Column? I would have thought it couldn't be done as the
column
value was set when a row was added.
--
Dennis in Houston

Nov 21 '05 #2
If I understand you correctly, I can add numbers to an AutoIncrement Column
in a talbe in a DataSet but when the actual database is updated, the numbers
I added won't appear in the Database...is this correct?

"Cor Ligthert" wrote:
Dennis,

I think that you can the best ave a look at this

autoincrement class
http://msdn.microsoft.com/library/de...ementtopic.asp

You cannot set the values, they will not even been in the database.
Setting the seed to -1 is the advised method.

See for this as well the article of Bill
http://msdn.microsoft.com/library/de...anidcrisis.asp

I hope this helps?

Cor

"Dennis" <De****@discussions.microsoft.com>
I have set a DataTable and one of the columns I set "AutoIncrement" to
True.
I then populate the Table by setting the columns to values then add the
row
to the table. I inadverently set the AutoIncrement Columns to different
values but didn't get any errors. Should I be able to set the value of an
AutoIncrement Column? I would have thought it couldn't be done as the
column
value was set when a row was added.
--
Dennis in Houston


Nov 21 '05 #3
Dennis,

I even do not believe you can add numbers to that. However the last
statement in your message is right.

"Dennis" <De****@discussions.microsoft.com>
If I understand you correctly, I can add numbers to an AutoIncrement
Column
in a talbe in a DataSet but when the actual database is updated, the
numbers
I added won't appear in the Database...is this correct?

"Cor Ligthert" wrote:
Dennis,

I think that you can the best ave a look at this

autoincrement class
http://msdn.microsoft.com/library/de...ementtopic.asp

You cannot set the values, they will not even been in the database.
Setting the seed to -1 is the advised method.

See for this as well the article of Bill
http://msdn.microsoft.com/library/de...anidcrisis.asp

I hope this helps?

Cor

"Dennis" <De****@discussions.microsoft.com>
>I have set a DataTable and one of the columns I set "AutoIncrement" to
>True.
> I then populate the Table by setting the columns to values then add the
> row
> to the table. I inadverently set the AutoIncrement Columns to
> different
> values but didn't get any errors. Should I be able to set the value of
> an
> AutoIncrement Column? I would have thought it couldn't be done as the
> column
> value was set when a row was added.
> --
> Dennis in Houston


Nov 21 '05 #4
Cor, the below code is what I'm talking about. I would have thought I should
get an error when I set the AutoIncrement Column 1 to the random integer.
Private Function CreateDataTable(ByVal TableName As String) As DataTable
'Create DataTable and add Columns
Dim t As DataTable = New DataTable
t.TableName = TableName
Dim col1 As DataColumn = New DataColumn("Col1",
Type.GetType("System.Int32"))
Dim col2 As DataColumn = New DataColumn("Col2",
Type.GetType("System.String"))

col1.AutoIncrement = True

'Add DataColumns to DataTable
t.Columns.Add(col1)
t.Columns.Add(col2)

'Populate the Columns
Dim irand As New Random(123)
Dim k as integer
For i = 1 To 25
k = irand.Next
newrow = t.NewRow
newrow("col1") = CType(k, Int32) 'AutoIncrement
newrow("col2") = "ABCD" 'String
t.Rows.Add(newrow)
Next i
End Sub

"Cor Ligthert" wrote:
Dennis,

I even do not believe you can add numbers to that. However the last
statement in your message is right.

"Dennis" <De****@discussions.microsoft.com>
If I understand you correctly, I can add numbers to an AutoIncrement
Column
in a talbe in a DataSet but when the actual database is updated, the
numbers
I added won't appear in the Database...is this correct?

"Cor Ligthert" wrote:
Dennis,

I think that you can the best ave a look at this

autoincrement class
http://msdn.microsoft.com/library/de...ementtopic.asp

You cannot set the values, they will not even been in the database.
Setting the seed to -1 is the advised method.

See for this as well the article of Bill
http://msdn.microsoft.com/library/de...anidcrisis.asp

I hope this helps?

Cor

"Dennis" <De****@discussions.microsoft.com>
>I have set a DataTable and one of the columns I set "AutoIncrement" to
>True.
> I then populate the Table by setting the columns to values then add the
> row
> to the table. I inadverently set the AutoIncrement Columns to
> different
> values but didn't get any errors. Should I be able to set the value of
> an
> AutoIncrement Column? I would have thought it couldn't be done as the
> column
> value was set when a row was added.
> --
> Dennis in Houston


Nov 21 '05 #5
Dennis,

My believe was not true, however the rest is. Therefore I have not to
believe anymore I know.

You can try this sample it needs a button and a datagrid on a form and a
reference to COM adox ext 2.x for dll and security
\\\ When you have tested it than change the part where I have told that add
your adding of the key
Private da As New OleDb.OleDbDataAdapter
Private Sub Form7_Load(ByVal sender As Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Dim catNewDB As New ADOX.Catalog
Dim fi As New IO.FileInfo("c:\test1\db1.mdb")
If fi.Exists Then
If MessageBox.Show("Delete?", "Existing File db1.mdb", _
MessageBoxButtons.YesNo) = DialogResult.Yes Then
fi.Delete()
Else
Exit Sub
End If
End If
catNewDB.Create("Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=c:\test1\db1.mdb")
'To make tables we use Adonet
Dim conn As New
OleDb.OleDbConnection("Provider=Microsoft.Jet.OLED B.4.0;" & _
" Data Source=C:\test1\db1.mdb;User Id=admin;Password=;")
Dim cmd As New OleDb.OleDbCommand("CREATE TABLE Dennis ( " & _
"Col1 int identity ," & _
"Col2 NVarchar(50)," & _
"CONSTRAINT [pk_Col1] PRIMARY KEY (Col1)) ", conn)
conn.Open()
Try
cmd.ExecuteNonQuery()
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.Message, "OleDbException")
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message, "GeneralException")
Exit Sub
End Try
Dim dt As New DataTable
Dim selectstring As String = "Select * from Dennis"
da = New OleDb.OleDbDataAdapter(selectstring, conn)
da.FillSchema(dt, SchemaType.Mapped)
dt.Columns(0).AutoIncrementSeed = -1
dt.Columns(0).AutoIncrementStep = -1
'Populate the Columns
Dim irand As New Random(123)
Dim k As Integer
For i As Integer = 1 To 25
k = irand.Next
Dim newrow As DataRow = dt.NewRow
newrow("col1") = CType(k, Int32)
'delete this row to see it with the autoseed
newrow("col2") = "ABCD"
dt.Rows.Add(newrow)
Next i
DataGrid1.DataSource = dt
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim cmb As New OleDb.OleDbCommandBuilder(da)
Dim dt As DataTable = DirectCast(DataGrid1.DataSource, DataTable)
da.Update(dt)
dt.Clear()
da.Fill(dt)
DataGrid1.DataSource = dt
End Sub
///

I hope this helps?

Cor
Nov 21 '05 #6
Thanks Cor. You help a lot of people on this newsgroup.

"Cor Ligthert" wrote:
Dennis,

My believe was not true, however the rest is. Therefore I have not to
believe anymore I know.

You can try this sample it needs a button and a datagrid on a form and a
reference to COM adox ext 2.x for dll and security
\\\ When you have tested it than change the part where I have told that add
your adding of the key
Private da As New OleDb.OleDbDataAdapter
Private Sub Form7_Load(ByVal sender As Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Dim catNewDB As New ADOX.Catalog
Dim fi As New IO.FileInfo("c:\test1\db1.mdb")
If fi.Exists Then
If MessageBox.Show("Delete?", "Existing File db1.mdb", _
MessageBoxButtons.YesNo) = DialogResult.Yes Then
fi.Delete()
Else
Exit Sub
End If
End If
catNewDB.Create("Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=c:\test1\db1.mdb")
'To make tables we use Adonet
Dim conn As New
OleDb.OleDbConnection("Provider=Microsoft.Jet.OLED B.4.0;" & _
" Data Source=C:\test1\db1.mdb;User Id=admin;Password=;")
Dim cmd As New OleDb.OleDbCommand("CREATE TABLE Dennis ( " & _
"Col1 int identity ," & _
"Col2 NVarchar(50)," & _
"CONSTRAINT [pk_Col1] PRIMARY KEY (Col1)) ", conn)
conn.Open()
Try
cmd.ExecuteNonQuery()
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.Message, "OleDbException")
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message, "GeneralException")
Exit Sub
End Try
Dim dt As New DataTable
Dim selectstring As String = "Select * from Dennis"
da = New OleDb.OleDbDataAdapter(selectstring, conn)
da.FillSchema(dt, SchemaType.Mapped)
dt.Columns(0).AutoIncrementSeed = -1
dt.Columns(0).AutoIncrementStep = -1
'Populate the Columns
Dim irand As New Random(123)
Dim k As Integer
For i As Integer = 1 To 25
k = irand.Next
Dim newrow As DataRow = dt.NewRow
newrow("col1") = CType(k, Int32)
'delete this row to see it with the autoseed
newrow("col2") = "ABCD"
dt.Rows.Add(newrow)
Next i
DataGrid1.DataSource = dt
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim cmb As New OleDb.OleDbCommandBuilder(da)
Dim dt As DataTable = DirectCast(DataGrid1.DataSource, DataTable)
da.Update(dt)
dt.Clear()
da.Fill(dt)
DataGrid1.DataSource = dt
End Sub
///

I hope this helps?

Cor

Nov 21 '05 #7

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

Similar topics

0
1678
by: rodrigo | last post by:
How to get get the last inserted key (value NOT column) in a specific dataTable dt right after dt.Rows.InsertAt according to code and schema below. The key is a autoincrement column. foreach...
2
11140
by: Newbie | last post by:
Im getting an exception: System.FormatException: Input string was not in a correct format. at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at...
0
3127
by: Chris Ericoli | last post by:
Hi, I am working with an 'in session' ado dataset with an asp.net application. My dataset is comprised of two tables, one of which maintains a few calculated datacolumns. For some reason these...
0
1390
by: Neil | last post by:
Hi, I'm getting some strange results using the autoincrement column on my datatable. I'm populating a datatable with data from my database and displaying this in a datagrid. The first time I get...
3
3177
by: Jon | last post by:
I'm learning about datatables. When using the example provided by MS in the ..NET Framework Class Library for DATATABLE (see below) I get an error on line 3 that says "Type expected". Is something...
5
8058
by: JC Voon | last post by:
Hi: How to reset the autoincrement value generated by DataTable ? I've master and detail table, the detail table has a autoincrement column, each time i add a new master record, i need to...
2
5806
by: PulkitZery | last post by:
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...
3
1397
by: costas | last post by:
Hi everyone, I've created a form with a datagrid and a datatable here is a sample: private System.Windows.Forms.DataGrid dataGrid1; private DataTable A; public POS() { // //...
1
3747
rizwan6feb
by: rizwan6feb | last post by:
I have a DataTable with thousands of records, i want to show these records on per page basis ( i.e a DataGridView showing first 20 records and next button to show next 20 records ...) To achieve...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7308
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
7371
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...
1
7023
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...
1
5037
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...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.