JUlius

JUlius

  • NA
  • 71
  • 0

I need help with an autonumber

Jul 11 2008 11:31 AM

Hello everyone:

I need your assistance on the following code that is allowing me to do an autonumber. The code works great, the problem is that is giving double number. Example. When I add a new record will give me #12, when I add another record it gives me #14 and another record it gives me #16. What is wrong with my code?

One more thing, I also notice that is adding a blank record every time I do an add a new record

I have verified my Sql Server Management Studio and check the primary key under Identity Specification. Ensure the Identity Increment is set to one and the Indentity seed is set to one as well

I am hoping to see #12, when I add anew record to be #13

Thanks

[CODE] Dim conn As New SqlClient.SqlConnection("you need a correct connection string here")
Dim sql As String = "INSERT INTO CallLog (CallNumber) VALUES ('StringValue')"

'it would be good to use a parameter instead of the stringvalue
conn.Open()
Dim command As New SqlClient.SqlCommand(sql, conn)
command.ExecuteNonQuery()

'reuse the sql string for the next command
sql = "SELECT MAX(ID) FROM Calllog"

'resuse and initialize the command for the next execution
command = New SqlClient.SqlCommand(sql, conn)

Dim id As Integer = command.ExecuteScalar()
MessageBox.Show("Newly added ID: " & id.ToString())

command.Dispose()
conn.Close()[/CODE]

Answers (1)