Buenas tardes estoy intentando realizar un insert a una base de dato de SQL Server, en el momento de ejecutar mi procedimiento me genera el siguiente error:
System.Data.SqlClient.SqlException: Cannot insert duplicate key row in object 'dbo.TblSystems' with unique index 'SystemID'. The statement has been terminated. en System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection) en System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection) en System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj) en System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) en System.Data.SqlClient.SqlCommand.RunExecuteNonQuer yTds(String methodName, Boolean async) en System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) en System.Data.SqlClient.SqlCommand.ExecuteNonQuery() en dbOperations.InsertFields() en C:\Inetpub\wwwroot\test\App_Code\dbOperations.vb:l ínea 147
Pero el registro se carga ya que no hay duplicidad de clave.
Si corro el mismo strip por consola de SQL Server no da Problema.
Por lo tanto creo que es la manera como llamo mi procedimiento de insertar.
Este es el codigo
Public Function InsertFields() As Boolean
Dim oCon As New System.Data.SqlClient.SqlConnection(Me.GetStringCo nection.ConnectionString)
Dim myCommand As SqlCommand = New SqlCommand()
Dim myTrans As SqlTransaction
Dim success As Boolean
oCon.Open()
myCommand.Connection = oCon
myTrans = oCon.BeginTransaction()
myCommand.Transaction = myTrans
Try
myCommand.CommandText = Me.INSERT_COMMAND.CommandText
myCommand.ExecuteNonQuery() ´linea 147
myTrans.Commit()
success = True
Catch ex As Exception
myTrans.Rollback()
Me._ERROR = ex.ToString
success = False
Finally
oCon.Close()
End Try
Return success
End Function
‘--------------------------------
Protected Sub cmdInsertar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdInsertar.Click
GenerarConexiones()
Dim sql As String
Dim checkVa As Integer = 0
If Me.CheckSelect.Checked Then
checkVa = 1
End If
sql = "INSERT INTO dbo.TblSystems(SystemID, SystemDescrip, [SystClassN°], SelectSys) VALUES('" + _
Me.txtSistema.Text + "','" + Me.txtDecripcion.Text + "'," + Me.DropClasf.SelectedValue + "," + checkVa.ToString + ");"
Dim _insert As New System.Data.SqlClient.SqlCommand(sql)
oCon.INSERT_COMMAND = _insert
Dim status As Boolean = oCon.InsertFields()
If status = True Then
Me.mens.Text = "Registro Ingresado con exito"
Else
Me.mens.Text = "No se Ingreso el Regitro Error: " + oCon._ERROR
End If
End Sub
Si alquien me puede ayudar se lo agradesco.