Principales respuestas
ID Clave primaria repetido

Pregunta
-
hola a todos, despues de conseguir que en el formulario de inserción de registros inserte uno nuevo y comprobase que el id (clave primaria) no estuviese vacío, me gustaría que el segundo paso fuese que comprobase si tu le metes un ID repetido y que no se cuelgue sino que te diga que esta repetido,alguna idea?? os dejo el codigo de ese formulario:
Public Class Form5
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
Me.PartidospoliticosBindingSource.AddNew()
End Sub
Private Sub PartidospoliticosBindingNavigatorSaveItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.PartidospoliticosBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.PoliticaDataSet)
End Sub
Private Sub IdPartidoTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdPartidoTextBox.TextChanged
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub PaginaWebLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub TelefónoContactoTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TelefónoContactoTextBox.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If (String.IsNullOrEmpty(IdPartidoTextBox.Text)) Then Throw New Exception("Debe indicar un ID de partido.")
Me.Validate()
Me.PartidospoliticosBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.PoliticaDataSet)
MsgBox("El partido se agregó satisfactoriamente")
Form2.Close()
Form2.Show()
Me.TableAdapterManager.PartidospoliticosTableAdapter.Fill(Me.PoliticaDataSet.Partidospoliticos)
Catch ex As Exception
MessageBox.Show("Debe indicar un ID de partido.")
End Try
End Sub
Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
End Sub
End Class
Muchas gracias
Respuestas
-
Para eso estamos!!
Prueba así:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try If (String.IsNullOrEmpty(IdPartidoTextBox.Text)) Then Throw New Exception("Debe indicar un ID de partido.") If Me.PoliticaDataSet.Partidospoliticos.Select("IDPARTIDO = '" + IdPartidoTextBox.Text + "'").Length > 0 Then Throw New Exception("Ya existe el ID de partido indicado.") End If Me.Validate() Me.PartidospoliticosBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll(Me.PoliticaDataSet) MsgBox("El partido se agregó satisfactoriamente") Form2.Close() Form2.Show() Me.TableAdapterManager.PartidospoliticosTableAdapter.Fill(Me.PoliticaDataSet.Partidospoliticos) Catch ex As Exception 'Aqui está el problema, debemos mostrar el texto que nos manda la excepción, quitamos esto: 'MessageBox.Show("Debe indicar un ID de partido.") 'Ponemos esto: MessageBox.Show(ex.message) End Try End Sub
Cuando hacemos la instrucción Throw New Exception("Nuestro texto aqui") estamos enviando este texto al Catch ex as Exception, por lo que en la propiedad ex.message tendremos nuestro texto, así lo unico que tenemos que hacer es mostrar este texto.
Espero que así funcione, Un gran saludo.
Todas las respuestas
-
Buenas Davsol
Prueba a hacer esto:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try If (String.IsNullOrEmpty(IdPartidoTextBox.Text)) Then Throw New Exception("Debe indicar un ID de partido.") 'Comprobamos que no exista este ID primario: if me.PoliticaDataSet.Partidospoliticos.Select("IDPARTIDO = " + IdPartidoTextBox.Text).Count > 0 then Throw New Exception("Ya existe el ID de partido indicado.") Me.Validate() Me.PartidospoliticosBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll(Me.PoliticaDataSet) MsgBox("El partido se agregó satisfactoriamente") Form2.Close() Form2.Show() Me.TableAdapterManager.PartidospoliticosTableAdapter.Fill(Me.PoliticaDataSet.Partidospoliticos) Catch ex As Exception MessageBox.Show("Debe indicar un ID de partido.") End Try End Sub
Con esto comprobamos que en tu dataset no se encuentre otro idpartido igual, no se si el valor de idpartido es un entero o un alfanumerico, en el segundo caso tendrías que usar comillas simples:
'Comprobamos que no exista este ID primario: if me.PoliticaDataSet.Partidospoliticos.Select("IDPARTIDO = '" + IdPartidoTextBox.Text + "'").Count > 0 then Throw New Exception("Ya existe el ID de partido indicado.")
Espero que te funcione, un gran saludo. -
Esa linea nueva :
if me .PoliticaDataSet.Partidospoliticos.Select ("IDPARTIDO = '" + IdPartidoTextBox.Text + "'" ).Count > 0 then Throw New Exception("Ya existe el ID de partido indicado." )
me da fallo, sale como tachada, de todas formas muchas gracias por molestarte
-
-
-
Ups Perdon jeje
Me equivoque :P
Prueba así:
if me .PoliticaDataSet.Partidospoliticos.Select ("IDPARTIDO = '" + IdPartidoTextBox.Text + "'" ).Length > 0 then Throw New Exception("Ya existe el ID de partido indicado." )
Al ser un array usamos la propiedad Length para saber la longitud (tamaño) del array, si es mayor que 0 significa que el ID ya existe.
Un saludo. -
ahora me salen más errores, son estos:
Error 1 'If' debe terminar con la instrucción 'End If' correspondiente.
Error 2 Un valor de tipo 'Politicator_v1._01.Form5' no se puede convertir en 'Boolean'.
Error 3 Se esperaba el fin de instrucción.
supongo que el uno y el tres van relacionados pero lo del 2 no lo comprendo -
Buenas DavSol Reestructuralo de esta forma:
if me.PoliticaDataSet.Partidospoliticos.Select("IDPARTIDO = '" + IDPartidoTextBox.Text + "'").Length > 0 then Throw New Exception("Ya existe el ID de partido indicado.") End If
La primera linea empieza con el If y acaba en el Then
La Segunda linea es el Throw new exception("Ya existe el id de partido")
Y la tercera linea es el End If
Así debería funcionarte perfectamente.
Un saludo. -
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If (String.IsNullOrEmpty(IdPartidoTextBox.Text)) Then Throw New Exception("Debe indicar un ID de partido.")
If Me.PoliticaDataSet.Partidospoliticos.Select("IDPARTIDO = '" + IdPartidoTextBox.Text + "'").Length > 0 Then
Throw New Exception("Ya existe el ID de partido indicado.")
End If
Me.Validate()
Me.PartidospoliticosBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.PoliticaDataSet)
MsgBox("El partido se agregó satisfactoriamente")
Form2.Close()
Form2.Show()
Me.TableAdapterManager.PartidospoliticosTableAdapter.Fill(Me.PoliticaDataSet.Partidospoliticos)
Catch ex As Exception
MessageBox.Show("Debe indicar un ID de partido.")
End Try
End Sub
Muchas gracias por ayudarme -
Para eso estamos!!
Prueba así:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try If (String.IsNullOrEmpty(IdPartidoTextBox.Text)) Then Throw New Exception("Debe indicar un ID de partido.") If Me.PoliticaDataSet.Partidospoliticos.Select("IDPARTIDO = '" + IdPartidoTextBox.Text + "'").Length > 0 Then Throw New Exception("Ya existe el ID de partido indicado.") End If Me.Validate() Me.PartidospoliticosBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll(Me.PoliticaDataSet) MsgBox("El partido se agregó satisfactoriamente") Form2.Close() Form2.Show() Me.TableAdapterManager.PartidospoliticosTableAdapter.Fill(Me.PoliticaDataSet.Partidospoliticos) Catch ex As Exception 'Aqui está el problema, debemos mostrar el texto que nos manda la excepción, quitamos esto: 'MessageBox.Show("Debe indicar un ID de partido.") 'Ponemos esto: MessageBox.Show(ex.message) End Try End Sub
Cuando hacemos la instrucción Throw New Exception("Nuestro texto aqui") estamos enviando este texto al Catch ex as Exception, por lo que en la propiedad ex.message tendremos nuestro texto, así lo unico que tenemos que hacer es mostrar este texto.
Espero que así funcione, Un gran saludo. -
-
-