Reputation: 313
I am creating a database application in vb.net. I have set an identity column(is identity = true) in visual studio. However when I run my application no identity column is recognized. Where am I going wrong?
My connections string is :
"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Administrator\Desktop\SKEDULERING LOCAL DATABASE\SkeduleringLD1.0\SkeduleringLD1.0\Databasis\Skeddatabasis.mdf;Integrated Security=True"
I have set this is the app.config file. I then open the connection with :
Public konneksie As New
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("konneksie").ConnectionString)
The connection does open. But the identity columns seems to be a problem.
Upvotes: 1
Views: 169
Reputation: 1268
Here's a general purpose utility method I use...
Assuming you have this declared:
Friend Shared ConnectionString As String = ConfigurationManager.ConnectionStrings("konneksie").ToString
You can pass SQL Query and an instantiated DataTable into this:
Friend Shared Sub LoadData(SQL As String, ByRef TableObject As DataTable, Optional Clear As Boolean = True)
If Clear Then TableObject.Clear()
Using SqlConnection As New SqlClient.SqlConnection(ConnectionString)
With New SqlClient.SqlDataAdapter(SQL, SqlConnection)
.SelectCommand.CommandTimeout = 60
.MissingSchemaAction = Data.MissingSchemaAction.AddWithKey
.FillSchema(TableObject, SchemaType.Source)
.Fill(TableObject)
.Dispose()
End With
End Using
For Each col As DataColumn In TableObject.PrimaryKey
With col
If .AutoIncrement Then
' Always set Step BEFORE Seed
.AutoIncrementStep = -1
.AutoIncrementSeed = -1
End If
End With
Next
End Sub
Upvotes: 0
Reputation: 10390
Identity
is a reserved word, so you need to call it with [Identity]
in your query, or use a different name for that column.
See Reserved Keywords List for more
Upvotes: 1