| |

VerySource

 Forgot password?
 Register
Search
View: 1578|Reply: 15

What should I do if the ID field number is automatically +1 when adding a new record?

[Copy link]

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-23 20:00:01
| Show all posts |Read mode
There is an ID field in the database, INT. Now every time a new record is added, the ID is automatically +1 from the segment. How to implement it? Here is my code:
 Public Overrides Sub SaveForAdd2 ()

        Dim drData As DataRow = DataSet11.Tables ("Application Form"). NewRow ()
        Try
            drData ("id") = TextBox36.Text
            drData ("Application Workshop") = TextBox30.Text ()
            drData ("Application Team") = TextBox31.Text ()
            drData ("Applicant") = TextBox33.Text ()
            drData ("Subject Leader") = TextBox34.Text ()
            drData ("Application Date") = DateTimePicker1.Value.ToString ("yyyy-MM-dd")
            drData ("name") = TextBox35.Text ()
            drData ("Unit Price") = TextBox36.Text ()
            drData ("Quantity") = TextBox37.Text ()
            drData ("Total") = TextBox38.Text ()
            drData ("Remarks") = TextBox39.Text ()
            drData ("Use") = TextBox40.Text ()

        Catch ex As Exception
            MessageBox.Show ("The data format is incorrect!")
            Exit Sub
        End Try

        Try
            DataSet11.Tables ("Application Form"). Rows.Add (drData)
            If Me.DataSet11.HasChanges = True Then
                SqlDataAdapter1.Update (DataSet11)
            End If
        Catch ex As Exception
            MessageBox.Show ("Data addition failed!")
        End Try
    End Sub
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-5-8 14:15:01
| Show all posts
Two options:
1. Check the maximum ID in the database first, and then add 1 to it.
Second, set the ID field to automatically increase in the database.
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 South Africa

 Author| Post time: 2020-5-9 18:15:02
| Show all posts
I want the code, thanks
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-5-10 19:45:01
| Show all posts
Define the IDENTITY column in the database
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-5-12 00:15:02
| Show all posts
Brother, how do you define the IDENTITY column in the database?
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-5-12 15:00:01
| Show all posts
Define the database: ID automatically grows:
such as:
create table #temp
(
id int identity (1,1),
value varchar (10)
)
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-5-15 13:45:01
| Show all posts
I want the program to find the maximum ID in the DataGrid, and then +1, I do n’t know how to write the statement in the DataGrid to find the largest ID record
Reply

Use magic Report

1

Threads

16

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-6-11 21:00:01
| Show all posts
select max(id) from table
Reply

Use magic Report

1

Threads

16

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-6-12 01:45:01
| Show all posts
DataSet11.Tables("Application Form").Compute("max(ID)","")Retrieve the maximum ID of the current data source
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-6-26 23:15:01
| Show all posts
You can change the ID field type to automatic growth in the database. You click on the data type of the ID field, and a drop-down box will appear.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list