You are on page 1of 9

Imports System.Data.

SqlClient Public Class Form1 Dim cadena As New SqlConnection("Data Source=DELL;Initial Catalog=agenda;Integrated Security=True") Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load LIMPIAR() muestra() deshabilita() llenacombo() End Sub Sub llenacombo() Dim datos As New DataSet Dim adaptador As New SqlDataAdapter("select descripcion from parentezco", cadena) adaptador.Fill(datos, "parentezco")

Dim fila As DataRow ComboBox1.Items.Clear() For Each fila In datos.Tables("parentezco").Rows ComboBox1.Items.Add(fila(0)) Next End Sub Sub muestra() ToolStrip1.Items(0).Visible = True ToolStrip1.Items(1).Visible = True ToolStrip1.Items(2).Visible = True ToolStrip1.Items(3).Visible = True ToolStrip1.Items(4).Visible = False ToolStrip1.Items(5).Visible = False ToolStrip1.Items(6).Visible = True Call checa() End Sub Sub checa() If TextBox1.Text = "" Then ToolStrip1.Items(1).Enabled = False ToolStrip1.Items(2).Enabled = False Else ToolStrip1.Items(1).Enabled = True ToolStrip1.Items(2).Enabled = True End If End Sub Sub oculta() ToolStrip1.Items(0).Visible = False ToolStrip1.Items(1).Visible = False ToolStrip1.Items(2).Visible = False ToolStrip1.Items(3).Visible = False ToolStrip1.Items(4).Visible = True

ToolStrip1.Items(5).Visible = True ToolStrip1.Items(6).Visible = False Call checa() End Sub Sub deshabilita() TextBox1.ReadOnly = True TextBox2.ReadOnly = True TextBox3.ReadOnly = True TextBox4.ReadOnly = True TextBox5.ReadOnly = True TextBox6.ReadOnly = True TextBox7.ReadOnly = True TextBox8.ReadOnly = True ComboBox1.Enabled = False RadioButton1.Enabled = False RadioButton2.Enabled = False End Sub Sub Habilita() TextBox1.ReadOnly = False TextBox2.ReadOnly = False TextBox3.ReadOnly = False TextBox4.ReadOnly = False TextBox5.ReadOnly = False TextBox6.ReadOnly = False TextBox7.ReadOnly = False TextBox8.ReadOnly = False ComboBox1.Enabled = True RadioButton1.Enabled = True RadioButton2.Enabled = True End Sub Sub LIMPIAR() TextBox1.Text = "" TextBox2.Text = ""

TextBox3.Text = "" TextBox4.Text = "" TextBox5.Text = "" TextBox6.Text = "" TextBox7.Text = "" TextBox8.Text = "" ComboBox1.SelectedIndex = -1 RadioButton2.Checked = True End Sub Private Sub Insertar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Insertar.Click LIMPIAR() Habilita() oculta() End Sub Private Sub Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Guardar.Click If validar_datos() = 0 Then 'insertar datos insertarDatos() deshabilita() muestra() End If End Sub Sub insertarDatos() Dim EC As Integer If RadioButton1.Checked Then EC = 1

ElseIf RadioButton2.Checked Then EC = 0 End If Dim cad As String cad = String.Format("insert into contactos values({0},'{1}','{2}','{3}','{4}',{5},{6},'{7 }','{8}',{9})", TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text, ComboBox1.Text, EC) Dim comando As New SqlCommand(cad, cadena) cadena.Open() comando.ExecuteNonQuery() cadena.Close() End Sub Function validar_datos() As Integer Dim b As Integer If TextBox1.Text.Length = 0 Then ErrorProvider1.SetError(TextBox1, "escribe el id") b = 1 Else ErrorProvider1.SetError(TextBox1, "") b = 0 End If If Not IsNumeric(TextBox1.Text) Then ErrorProvider1.SetError(TextBox1, "escriba un numero") b = 1 Else

ErrorProvider1.SetError(TextBox1, "") b = 0 End If If Not IsDate(TextBox5.Text) Then ErrorProvider1.SetError(TextBox5, "escriba una fecha correcta") b = 1 Else ErrorProvider1.SetError(TextBox5, "") b = 0 End If If b = 1 Then Return 1 Else Return 0 End If End Function Sub limpia_error() ErrorProvider1.SetError(TextBox1, "") ErrorProvider1.SetError(TextBox5, "") End Sub Private Sub Cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancelar.Click LIMPIAR() deshabilita() muestra() limpia_error() End Sub

Private Sub Modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Modificar.Click Habilita() oculta() End Sub Private Sub Eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Eliminar.Click If MsgBox("Seguro de eliminar el registro?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then Dim cad As String cad = String.Format("delete from contactos where id={0}", TextBox1.Text) Dim comando As New SqlCommand(cad, cadena) cadena.Open() comando.ExecuteNonQuery() cadena.Close() LIMPIAR() muestra() End If End Sub Private Sub Consultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Consultar.Click buscar.ShowDialog() End Sub End Class

Imports System.Data.SqlClient Public Class buscar Dim cadena As New SqlConnection("Data Source=DELL;Initial Catalog=agenda;Integrated Security=True") Private Sub buscar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim datos As New DataSet Dim adaptador As New SqlDataAdapter("select * from contactos", cadena) adaptador.Fill(datos, "contactos") DataGridView1.DataSource = datos.Tables("contactos") End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Form1.TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value Form1.TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value Form1.TextBox3.Text = DataGridView1.CurrentRow.Cells(2).Value Form1.TextBox4.Text = DataGridView1.CurrentRow.Cells(3).Value Form1.TextBox5.Text = DataGridView1.CurrentRow.Cells(4).Value Form1.TextBox6.Text = DataGridView1.CurrentRow.Cells(5).Value Form1.TextBox7.Text = DataGridView1.CurrentRow.Cells(6).Value Form1.TextBox8.Text = DataGridView1.CurrentRow.Cells(7).Value Form1.ComboBox1.Text = DataGridView1.CurrentRow.Cells(8).Value If DataGridView1.CurrentRow.Cells(9).Value = True Then Form1.RadioButton1.Checked = True Else Form1.RadioButton2.Checked = True End If Form1.deshabilita() Form1.muestra() Me.Close() End Sub End Class

You might also like