Usuario
dsdbdcc

Pregunta
-
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Private con As SqlConnection
Private dts As DataSet
Private ada As SqlDataAdapter
Private bmb As BindingManagerBase
Private dv As DataView
Private sugerenciaUserName As String
Private primerNom As Boolean = False
Private Cognom As Boolean = False
Private CogerID As SqlCommand
'Load
'Tema focus
Dim var As Control
For Each var In Me.Controls
If (TypeOf (var) Is System.Windows.Forms.TextBox) Then
Dim miTxtBox As TextBox = DirectCast(var, TextBox)
AddHandler miTxtBox.Enter, AddressOf teFocus
AddHandler miTxtBox.Leave, AddressOf noFocus
End If
Next
con = New SqlConnection
con.ConnectionString = "Data Source=.\SQLEXPRESS; Initial Catalog=COMPASSTRAVEL; Trusted_Connection=True;"
'con.ConnectionString = "Data Source=(localdb)\v11.0; Initial Catalog=COMPASSTRAVEL; Trusted_Connection=True;"
con.Open()
ada = New SqlDataAdapter("select * from MEMBERS", con)
Dim cmBase As SqlCommandBuilder = New SqlCommandBuilder(ada)
dts = New DataSet
ada.Fill(dts, "MEMBERS")
bmb = BindingContext(dts, "MEMBERS")
GetID()
AddHandler dts.Tables("MEMBERS").ColumnChanging, New DataColumnChangeEventHandler(AddressOf OnColumnChanging)
CarregarDataBindings()
idFocus()
'Té focus
Private Sub teFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim miTextBox As TextBox = DirectCast(sender, TextBox)
miTextBox.BackColor = Color.SpringGreen
End Sub
'No focus
Private Sub noFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim miTextBox As TextBox = DirectCast(sender, TextBox)
miTextBox.BackColor = Color.White
End Sub
'Carregar Bindings
Private Sub CarregarDataBindings()
'Utilitzem el binding per cridar les dades de la base de dades als textbox corresponents:
Dim oBind As Binding
oBind = New Binding("Text", dts, "MEMBERS.MEMBERID")
txtID.DataBindings.Add(oBind)
oBind = Nothing
bmb = BindingContext(dts, "MEMBERS")
'OnColumnChanging (Pass, Auto Username, Comprovació Username i Pass)
Private Sub OnColumnChanging(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs)
'Comprovació de si la contrasenya està entre 4 i 9 digits
Try
If e.Column.ColumnName = "PASSWORD" Then
If e.ProposedValue.ToString.Length < 4 Then
MsgBox("La contrasenya ha de ser de 4 digits o superior")
activarDesactivar(True)
e.Row.EndEdit()
Throw New System.Exception("error")
ElseIf e.ProposedValue.ToString.Length > 4 And e.ProposedValue.ToString.Length < 9 Then
MsgBox("La contrasenya es aconsellable que sigui de 9 digits com a mínim")
activarDesactivar(False)
Persistencia()
ElseIf e.ProposedValue.ToString.Length < 9 Then
MsgBox("La contrasenya es aconsellable que sigui de 9 digits")
activarDesactivar(False)
Persistencia()
End If
End If
Catch ex As Exception
bmb.CancelCurrentEdit()
End Try
'Autogeneració del nom utilitzan els 3 primers digits del nom
If e.Column.ColumnName = "FIRSTNAME" And e.ProposedValue.ToString.Length >= 3 And primerNom = False Then
sugerenciaUserName = Mid(txtNom.Text, 1, 3)
primerNom = True
End If
'i els 3 ultims del cognom
If e.Column.ColumnName = "LASTNAME" And e.ProposedValue.ToString.Length >= 3 And Cognom = False Then
sugerenciaUserName += Mid(txtCognom.Text, txtCognom.Text.Length - 2, txtCognom.Text.Length)
Cognom = True
End If
If primerNom And Cognom Then
txtUser.Text = sugerenciaUserName
End If
'Comprovació si ja existeix un nom d'usuari per no poder-lo duplicar ni se utilitzat per un nou membre
Try
If e.Column.ColumnName = "USERNAME" Then
For i As Integer = 0 To dts.Tables("MEMBERS").Rows.Count - 1
If txtUser.Text = dts.Tables("MEMBERS").Rows(i)("USERNAME").ToString Then
MsgBox("Aquest nom d'usuari ja existeix")
activarDesactivar(True)
Throw New System.Exception("error")
End If
Next
End If
Catch ex As Exception
activarDesactivar(True)
End Try
'Comprovació si ja existeix un ID per no poder-lo duplicar ni se utilitzat per un nou membre
Try
If e.Column.ColumnName = "MEMBERID" Then
For i As Integer = 0 To dts.Tables("MEMBERS").Rows.Count - 1
If txtUser.Text = dts.Tables("MEMBERS").Rows(i)("MEMBERID").ToString Then
MsgBox("Aquest ID ja existeix")
e.Row.EndEdit()
bmb.CancelCurrentEdit()
activarDesactivar(True)
Throw New System.Exception("error")
End If
Next
End If
Catch ex As Exception
bmb.CancelCurrentEdit()
activarDesactivar(False)
End Try
End Sub
'Agafar ID
Private Sub GetID()
' Amb @@Identity agafem el ID en la comanda sql
CogerID = New SqlCommand()
CogerID.CommandText = "SELECT @@IDENTITY"
CogerID.Connection = con
' Es crea el handler pel rowudpated
AddHandler ada.RowUpdated, AddressOf HandleRowUpdated
End Sub
' Event handler per RowUpdated event.
Private Sub HandleRowUpdated(ByVal sender As Object, ByVal e As SqlRowUpdatedEventArgs)
If e.Status = UpdateStatus.Continue AndAlso e.StatementType = StatementType.Insert Then
' Agafes el valor del ID de la row memberid
e.Row("MEMBERID") = Int32.Parse(CogerID.ExecuteScalar().ToString())
Debug.WriteLine(e.Row("MEMBERID"))
e.Row.AcceptChanges()
End If
End Sub
'Buscar per ID
Private Sub txtBuscaID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscaID.TextChanged
'Aquí es fa el control de busqueda pel filtre des del textbox pel ID
Try
For i As Integer = 0 To dts.Tables("MEMBERS").Rows.Count - 1
If txtBuscaID.Text = dts.Tables("MEMBERS").Rows(i)("MEMBERID").ToString Then
bmb.Position = i
End If
Next
Catch ex As Exception
MsgBox("Aquest membre no existeix")
End Try
End Sub
'Botons direcció
Private Sub btnInicio_Click(sender As Object, e As EventArgs) Handles btnInicio.Click
bmb.Position = 0
idFocus()
End Sub
Private Sub btnIzq_Click(sender As Object, e As EventArgs) Handles btnIzq.Click
bmb.Position = bmb.Position - 1
idFocus()
End Sub
Private Sub btnDerecha_Click(sender As Object, e As EventArgs) Handles btnDerecha.Click
bmb.Position = bmb.Position + 1
idFocus()
End Sub
Private Sub btnFinal_Click(sender As Object, e As EventArgs) Handles btnFinal.Click
bmb.Position = bmb.Count - 1
idFocus()
End Sub
'Tecles
Private Sub txtID_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtID.KeyDown
'alt + flecha esquerre per posar l'anterior
If e.Alt And e.KeyCode = Keys.Left Then
bmb.Position = bmb.Position - 1
End If
'alt + flecha dreta per posar el següent
If e.Alt And e.KeyCode = Keys.Right Then
bmb.Position = bmb.Position + 1
End If
'alt + flecha adalt per anar al primer
If e.Alt And e.KeyCode = Keys.Up Then
bmb.Position = 0
End If
'alt + flecha aball per anar a l'ultim
If e.Alt And e.KeyCode = Keys.Down Then
bmb.Position = bmb.Count - 1
End If
End Sub
'Focus
Private Sub idFocus()
txtID.Select()
End Sub
'Persistencia
Private Sub Persistencia()
Dim dt As DataTable
dt = dts.Tables("MEMBERS")
ada.Update(dt)
activarDesactivar(False)
End Sub
'btEliminar
Private Sub btnEliminar_Click(sender As Object, e As EventArgs) Handles btnEliminar.Click
Dim resposta As String
'Ens assegurem de que sí es vol eliminar el registre
resposta = MsgBox("Segur que vols eliminar el registre?", MsgBoxStyle.YesNo, "Eliminar registre")
If resposta = MsgBoxResult.Yes Then
bmb.RemoveAt(bmb.Position)
Persistencia()
MsgBox("El registre ha estat eliminat")
Else
MsgBox("El registre NO s'ha eliminat")
End If
idFocus()
End Sub
'btModificar
Private Sub btnModificar_Click(sender As Object, e As EventArgs) Handles btnModificar.Click
activarDesactivar(True)
End Sub
'btGuardar/Acceptar
Private Sub btnGuardar_Click(sender As Object, e As EventArgs) Handles btnGuardar.Click
If txtNom.Text = "" Or txtCognom.Text = "" Or txtUser.Text = "" Or txtPass.Text = "" Or txtPass.ToString.Length < 4 Then
MsgBox("Falten paràmetres")
activarDesactivar(True)
Else
bmb.EndCurrentEdit()
Persistencia()
activarDesactivar(False)
End If
End Sub
'btCancelar
Private Sub btnCancelar_Click(sender As Object, e As EventArgs) Handles btnCancelar.Click
bmb.CancelCurrentEdit()
activarDesactivar(False)
End Sub
'btNew
Private Sub btnCrear_Click(sender As Object, e As EventArgs) Handles btnCrear.Click
activarDesactivar(True)
bmb.AddNew()
End Sub
--------------------------------------------------------
-------------------
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions
Public Class Form2
Private con As SqlConnection
Private datset As DataSet
Private adapt As SqlDataAdapter
Private IndexCP As Integer
Private IndexPob As String
Private IndexCliente As Integer
Private datView As DataView
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Obrim la conexió amb la base de dades
con = New SqlConnection
'con.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=CIUTATS; Trusted_Connection=True;"
con.ConnectionString = "Data Source=(localdb)\v11.0; Initial Catalog=CIUTATS; Trusted_Connection=True;"
con.Open()
' Crearem el dataset, base de dades virtual en memoria.
datset = New DataSet()
' Crearem un adapter per parlar amb la taula Clientes
adapt = New SqlDataAdapter("select * from dbo.CIUTAT order by NOM_CIUTAT", con)
' Generem les sentencies de insert, update, delete ...
' si aixó no es fa no funcionara el update() del adapter.
Dim cmBase As SqlCommandBuilder = New SqlCommandBuilder(adapt)
' Executar el adapter i col·locar en mèmoria la informació i estructura.
adapt.Fill(datset, "dbo.CIUTAT")
IndexCP = 0
CarregarDadesListBoxPob()
If cbCP.Checked = True Then
lstCP.Items.Clear()
CarregarDadesListBoxCP()
End If
If cbPob.Checked = True Then
lstCP.Items.Clear()
CarregarDadesListBoxPob()
End If
IndexCliente = 0
prova()
SyncListBox()
End Sub
Private Sub SyncListBox()
lstCP.SelectedIndex = SelectedIndexByIndexCliente()
End Sub
Private Function SelectedIndexByIndexCliente() As Integer
Dim SelIndex As Integer
Dim i As Integer
SelIndex = -1
For i = 0 To IndexCliente
If datset.Tables("dbo.CIUTAT").Rows(i).RowState <> DataRowState.Deleted Then
SelIndex = SelIndex + 1
End If
Next
Return SelIndex
End Function
Private Sub prova()
txtCodiP.Text = datset.Tables("dbo.CIUTAT").Rows(IndexCliente)("CP").ToString()
End Sub
Private Sub btTancar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTancar.Click
Me.Close()
End Sub
Private Sub CarregarDadesListBoxCP()
Dim i As Integer
lstCP.Items.Clear()
For i = 0 To (datset.Tables("dbo.CIUTAT").Rows.Count - 1)
If datset.Tables("dbo.CIUTAT").Rows(i).RowState <> DataRowState.Deleted Then
lstCP.Items.Add(datset.Tables("dbo.CIUTAT").Rows(i)("CP").ToString)
End If
Next
End Sub
Private Sub CarregarDadesListBoxPob()
Dim i As Integer
lstCP.Items.Clear()
For i = 0 To (datset.Tables("dbo.CIUTAT").Rows.Count - 1)
If datset.Tables("dbo.CIUTAT").Rows(i).RowState <> DataRowState.Deleted Then
lstCP.Items.Add(datset.Tables("dbo.CIUTAT").Rows(i)("NOM_CIUTAT").ToString)
End If
Next
End Sub
Private Sub btEscollir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btEscollir.Click
Form1.txtCP.Text = txtCodiP.Text
Me.Close()
End Sub
Private Sub OrdenarPerCP()
lstCP.Items.Clear()
Try
Dim dv As DataView
dv = datset.Tables("dbo.CIUTAT").DefaultView
dv.RowFilter = "CP like '%" + txtCPOB.Text + "%'"
lstCP.Items.Clear()
For i = 0 To dv.Count - 1
lstCP.Items.Add(dv(i)("CP").ToString)
Next
Catch ex As Exception
MsgBox("Introdueix un Codi Postal per buscar segons un filtre")
End Try
End Sub
Private Sub OrdenarPerPob()
lstCP.Items.Clear()
Try
Dim dv As DataView
dv = datset.Tables("dbo.CIUTAT").DefaultView
dv.RowFilter = "NOM_CIUTAT like '%" + txtCPOB.Text + "%'"
lstCP.Items.Clear()
For i = 0 To dv.Count - 1
lstCP.Items.Add(dv(i)("NOM_CIUTAT").ToString)
Next
Catch ex As Exception
MsgBox("Introdueix una Població per buscar segons un filtre")
End Try
End Sub
Private Sub txtCPOB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCPOB.TextChanged
If cbCP.Checked = True Then
OrdenarPerCP()
End If
If cbPob.Checked = True Then
OrdenarPerPob()
End If
End Sub
Private Sub lstCP_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstCP.SelectedIndexChanged
IndexCliente = lstCP.SelectedIndex
prova()
End Sub
End Class
----------------
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions
Public Class Form1
Private con As SqlConnection
Private datset As DataSet
Private adapt As SqlDataAdapter
Private IndexCliente As Integer
Private dvClients As DataView
Dim contNElim As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Obrim la conexió amb la base de dades
con = New SqlConnection
'con.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=CIUTATS; Trusted_Connection=True;"
con.ConnectionString = "Data Source=(localdb)\v11.0; Initial Catalog=CIUTATS; Trusted_Connection=True;"
con.Open()
' Crearem el dataset, base de dades virtual en memoria.
datset = New DataSet()
' Crearem un adapter per parlar amb la taula Clientes
adapt = New SqlDataAdapter("select * from dbo.CONTACTE order by CODI asc", con)
' Generem les sentencies de insert, update, delete ...
' si aixó no es fa no funcionara el update() del adapter.
Dim cmBase As SqlCommandBuilder = New SqlCommandBuilder(adapt)
' Executar el adapter i col·locar en mèmoria la informació i estructura.
adapt.Fill(datset, "dbo.CONTACTE")
'Definirem que la columna NomClient ha de ser obligatoria
datset.Tables("dbo.CONTACTE").Columns("NOM").AllowDBNull = False
'Definim idcliente com a PK
Dim pk(1) As DataColumn
pk(0) = datset.Tables("dbo.CONTACTE").Columns("CODI")
datset.Tables("dbo.CONTACTE").PrimaryKey = pk
IndexCliente = 0
' Carreguem les dades en el listbox.
CarregarDadesListBox()
' Mostrar el primer registre d'informació
MostrarDadesClient()
SyncListBox()
btnCrearClient.Enabled = False
End Sub
Private Sub Form1_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If lblCanvis.Text > "0" Then
If MsgBox("Hi ha canvis guardat no passats a la persistència, vols pasar-los?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
Exit Sub
End If
' han dit que si
GuardarDades()
Persistencia()
End If
End Sub
Private Sub CarregarDadesListBox()
Dim i As Integer
lstClients.Items.Clear()
For i = 0 To (datset.Tables("dbo.CONTACTE").Rows.Count - 1)
If datset.Tables("dbo.CONTACTE").Rows(i).RowState <> DataRowState.Deleted Then
lstClients.Items.Add(datset.Tables("dbo.CONTACTE").Rows(i)("NOM").ToString)
End If
Next
End Sub
Private Sub SyncListBox()
lstClients.SelectedIndex = SelectedIndexByIndexCliente()
End Sub
Private Function SelectedIndexByIndexCliente() As Integer
Dim SelIndex As Integer
Dim i As Integer
SelIndex = -1
For i = 0 To IndexCliente
If datset.Tables("dbo.CONTACTE").Rows(i).RowState <> DataRowState.Deleted Then
SelIndex = SelIndex + 1
End If
Next
Return SelIndex
End Function
Private Sub MostrarDadesClient()
txtId.Text = datset.Tables("dbo.CONTACTE").Rows(IndexCliente)("CODI").ToString()
txtCliente.Text = datset.Tables("dbo.CONTACTE").Rows(IndexCliente)("NOM").ToString
txtCP.Text = datset.Tables("dbo.CONTACTE").Rows(IndexCliente)("CP").ToString
txtTelefon.Text = datset.Tables("dbo.CONTACTE").Rows(IndexCliente)("TELEFON").ToString
txtCategoria.Text = datset.Tables("dbo.CONTACTE").Rows(IndexCliente)("CATEGORIA").ToString
txtMail.Text = datset.Tables("dbo.CONTACTE").Rows(IndexCliente)("EMAIL").ToString
txtRiscM.Text = datset.Tables("dbo.CONTACTE").Rows(IndexCliente)("RISCMAXM").ToString
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
IndexCliente = 0
While datset.Tables("dbo.CONTACTE").Rows(IndexCliente).RowState = DataRowState.Deleted
IndexCliente = IndexCliente + 1
End While
MostrarDadesClient()
SyncListBox()
End Sub
Private Sub btnPrior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrior.Click
Dim aux As Integer
Dim trovat As Boolean
aux = IndexCliente
IndexCliente = IndexCliente - 1
trovat = False
While (IndexCliente >= 0) And (Not trovat)
If datset.Tables("dbo.CONTACTE").Rows(IndexCliente).RowState <> DataRowState.Deleted Then
trovat = True
Else
IndexCliente = IndexCliente - 1
End If
End While
If IndexCliente < 0 Then
IndexCliente = aux
End If
If IndexCliente <> aux Then
MostrarDadesClient()
SyncListBox()
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim aux As Integer
Dim trovat As Boolean
aux = IndexCliente
IndexCliente = IndexCliente + 1
trovat = False
While (IndexCliente <= (datset.Tables("dbo.CONTACTE").Rows.Count - 1)) And (Not trovat)
If datset.Tables("dbo.CONTACTE").Rows(IndexCliente).RowState <> DataRowState.Deleted Then
trovat = True
Else
IndexCliente = IndexCliente + 1
End If
End While
If IndexCliente > (datset.Tables("dbo.CONTACTE").Rows.Count - 1) Then
IndexCliente = aux
End If
If IndexCliente <> aux Then
MostrarDadesClient()
SyncListBox()
End If
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
Dim max As Integer = lstClients.Items.Count() - 1
IndexCliente = datset.Tables("dbo.CONTACTE").Rows.Count - 1
While datset.Tables("dbo.CONTACTE").Rows(IndexCliente).RowState = DataRowState.Deleted
IndexCliente = IndexCliente - 1
End While
If lstClients.SelectedIndex < max Then
lstClients.SelectedIndex = (max)
Else
lstClients.SelectedIndex = 1
End If
MostrarDadesClient()
SyncListBox()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCP.Click
Form2.ShowDialog()
End Sub
Private Sub lstClients_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstClients.SelectedIndexChanged
IndexCliente = lstClients.SelectedIndex
MostrarDadesClient()
End Sub
Private Sub cbEditar_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbEditar.CheckedChanged
btnCrearClient.Enabled = True
btCP.Enabled = True
If cbEditar.Checked = True Then
txtCliente.ReadOnly = False
txtCP.ReadOnly = False
txtCategoria.ReadOnly = False
txtTelefon.ReadOnly = False
txtId.ReadOnly = False
txtMail.ReadOnly = False
txtRiscM.ReadOnly = False
Else
btCP.Enabled = False
txtCliente.ReadOnly = True
txtCP.ReadOnly = True
txtCategoria.ReadOnly = True
txtTelefon.ReadOnly = True
txtId.ReadOnly = True
txtMail.ReadOnly = True
txtRiscM.ReadOnly = True
End If
End Sub
Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
Dim bln As Boolean = IsValidEmail(txtMail.Text)
If txtMail.Text <> "" Then
If bln = False Then
MsgBox("El format del correu electrònic és invàlid")
End If
End If
GuardarDades()
cbEditar.Checked = False
CarregarDadesListBox()
SyncListBox()
MostrarDadesClient()
ObtenirNCanvis()
End Sub
Public Function IsValidEmail(ByVal email As String) As Boolean
If email = String.Empty Then Return False
' Compruebo si el formato de la dirección es correcto.
Dim re As Regex = New Regex("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")
Dim m As Match = re.Match(email)
Return (m.Captures.Count <> 0)
End Function
Private Function ComprovarID(banderaid)
Dim banderaid2 As Boolean = banderaid
For i = 0 To (datset.Tables("dbo.CONTACTE").Rows.Count - 1)
If datset.Tables("dbo.CONTACTE").Rows(i)("CODI").ToString = txtId.Text Then
MsgBox("El ID ja existeix")
banderaid2 = True
End If
Next
Return banderaid2
End Function
Private Sub GuardarDades()
Try
datset.Tables("CONTACTE").Rows(IndexCliente)("CODI") = txtId.Text
datset.Tables("CONTACTE").Rows(IndexCliente)("NOM") = txtCliente.Text
datset.Tables("CONTACTE").Rows(IndexCliente)("CP") = txtCP.Text
datset.Tables("CONTACTE").Rows(IndexCliente)("TELEFON") = txtTelefon.Text
datset.Tables("CONTACTE").Rows(IndexCliente)("CATEGORIA") = txtCategoria.Text
datset.Tables("CONTACTE").Rows(IndexCliente)("EMAIL") = txtMail.Text
datset.Tables("CONTACTE").Rows(IndexCliente)("RISCMAXM") = txtRiscM.Text
Catch ex As Exception
If IsNumeric(txtCP.Text) = False Then
MsgBox("Introdueix un valor numèric en el Codi postal")
End If
If IsNumeric(txtCategoria.Text) = False Then
MsgBox("Introdueix un valor numèric en la Categoria")
End If
If IsNumeric(txtTelefon.Text) = False Then
MsgBox("Introdueix un valor numèric en el Telèfon")
End If
If IsNumeric(txtRiscM.Text) = False Then
MsgBox("Introdueix un valor numèric en el Risc màxim")
End If
If txtRiscM.Text = "" And txtRiscM.Text <= 0 Then
MsgBox("Introdueix un valor numèric en el risc màxim igual o més gran que 0")
End If
End Try
End Sub
Private Sub btnCrearClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrearClient.Click
Dim banderaid As Boolean = False
Dim fer As Boolean
Dim newCustomersRow As DataRow = datset.Tables("dbo.CONTACTE").NewRow()
Try
newCustomersRow("CODI") = txtId.Text
newCustomersRow("NOM") = txtCliente.Text
newCustomersRow("CP") = txtCP.Text
newCustomersRow("TELEFON") = txtTelefon.Text
newCustomersRow("CATEGORIA") = txtCategoria.Text
newCustomersRow("EMAIL") = txtMail.Text
newCustomersRow("RISCMAXM") = txtRiscM.Text
fer = ComprovarID(banderaid)
If fer = False Then
datset.Tables("dbo.CONTACTE").Rows.Add(newCustomersRow)
PersistenciaInsert()
MsgBox("Les dades han estat introduides")
End If
Catch ex As Exception
MsgBox("Les dades NO han estat introduides")
End Try
ObtenirNCanvis()
End Sub
Sub PersistenciaInsert()
adapt.Update(datset, "dbo.CONTACTE")
ObtenirNCanvis()
End Sub
Private Sub btnEliminarRegistre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarRegistre.Click
If MsgBox("Segur que vols eliminar el registre?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
Exit Sub
End If
' han dit que si
datset.Tables("dbo.CONTACTE").Rows(IndexCliente).Delete()
contNElim = contNElim + 1
' Verifiquem que no hagim borrat l'ultim registre
btnFirst_Click(sender, e)
CarregarDadesListBox()
ObtenirNCanvis()
MostrarDadesClient()
Persistencia()
End Sub
Private Sub btnPersistencia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPersistencia.Click
Try
PersistenciaInsert()
Persistencia()
MsgBox("La persistència ha estat realitzada")
Catch ex As Exception
MsgBox("La persistència ja ha estat realitzada anteriorment o té algun error")
End Try
End Sub
Private Sub Persistencia()
'Actualitzar només els eliminats
Dim datable As DataTable
Try
datable = datset.Tables("dbo.CONTACTE").GetChanges(DataRowState.Deleted)
If Not (datable Is Nothing) Then
adapt.Update(datable)
End If
lblCanvis.Text = "0"
MsgBox("La persistència ha estat realitzada")
Catch ex As Exception
MsgBox("La persistència ja ha estat realitzada anteriorment o té algun error")
End Try
End Sub
Private Sub btCercar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCercar.Click
Try
Dim i As Integer
Dim noId As Boolean = False
lstClients.Items.Clear()
For i = 0 To (datset.Tables("dbo.CONTACTE").Rows.Count - 1)
If datset.Tables("dbo.CONTACTE").Rows(i)("CODI").ToString = txtCid.Text Then
noId = True
txtId.Text = datset.Tables("dbo.CONTACTE").Rows(i)("CODI").ToString()
txtCliente.Text = datset.Tables("dbo.CONTACTE").Rows(i)("NOM").ToString
txtCP.Text = datset.Tables("dbo.CONTACTE").Rows(i)("CP").ToString
txtTelefon.Text = datset.Tables("dbo.CONTACTE").Rows(i)("TELEFON").ToString
txtCategoria.Text = datset.Tables("dbo.CONTACTE").Rows(i)("CATEGORIA").ToString
txtMail.Text = datset.Tables("dbo.CONTACTE").Rows(i)("EMAIL").ToString
txtRiscM.Text = datset.Tables("dbo.CONTACTE").Rows(i)("RISCMAXM").ToString
End If
Next
If noId = False Then
MsgBox("Escriu un ID o posa un vàlid")
End If
Catch ex As Exception
MsgBox("Escriu un ID o posa un vàlid")
End Try
End Sub
Private Sub ObtenirNCanvis()
If datset.HasChanges Then
If datset.Tables("dbo.CONTACTE").GetChanges.Rows.Count > 0 Then
lblCanvis.Text = datset.Tables("dbo.CONTACTE").GetChanges.Rows.Count
End If
End If
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles btNou.Click
txtId.Text = ""
txtCP.Text = ""
txtCliente.Text = ""
txtCategoria.Text = ""
txtTelefon.Text = ""
txtMail.Text = ""
txtRiscM.Text = ""
cbEditar.Checked = True
btnCrearClient.Enabled = True
End Sub
Private Sub btnRollBack_Click(sender As Object, e As EventArgs) Handles btnRollBack.Click
datset.Tables("CLIENTS").RejectChanges()
End Sub
End Class