Não estou conseguindo criar auto numeração no campo codigo (pK) em uma tabela do sql server ,
fiz de 2 formas e da´o mesmo erro em ambas :
codigo :
Protected Sub CmdIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdIncluir.Click
Dim objComando As SqlCommand
Dim strSql As String
Dim objTransacao As SqlTransaction
Dim blnTransacao As Boolean = False
Dim intLinhas As Integer
strSql =
"INSERT INTO FUNCIONARIO" & vbCrLf & _
"(NOME,CARGO)" & vbCrLf & _
"VALUES('" & txtNome.Text & " ','" & txtCargo.Text & " ' )"
objComando =
New SqlCommand
objComando.Connection = objConexao
objComando = objConexao.CreateCommand
If objComando.Connection.State <> ConnectionState.Open Then
objConexao.ConnectionString = strConexao
objConexao.Open()
End If
objTransacao = objConexao.BeginTransaction
blnTransacao =
True
With objComando
.Transaction = objTransacao
.CommandType = CommandType.Text
.CommandText = strSql
intLinhas = .ExecuteNonQuery
End With
objTransacao.Commit()
blnTransacao =
False
Limpatela()
GeraComboCadastrados()
objComando =
Nothing
End Sub
Não é possível inserir o valor NULL na coluna 'codigo', tabela 'empregados.dbo.funcionario'; a coluna não permite nulos. Falha em INSERT. A instrução foi finalizada.
1ª forma :
USE
[empregados]
GO
/****** Object: Table [dbo].[funcionario] Script Date: 05/31/2008 23:45:04 ******/
SET
ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER ON
GO
CREATE
TABLE [dbo].[funcionario](
[codigo] [uniqueidentifier default newid()]
NOT NULL,
[nome] [nvarchar]
(50) NOT NULL,
[cargo] [nvarchar]
(max) NOT NULL
)
ON [PRIMARY]
2ª forma :
USE
[empregados]
GO
/****** Object: Table [dbo].[funcionario] Script Date: 06/01/2008 00:01:50 ******/
SET
ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER ON
GO
CREATE
TABLE [dbo].[funcionario](
[codigo] [int identity]
(1,1) NOT NULL,
[nome] [nvarchar]
(50) NOT NULL,
[cargo] [nvarchar]
(max) NOT NULL
)
ON [PRIMARY]