0% found this document useful (0 votes)
30 views4 pages

Chimu Elo

This document contains code for two classes - Form1 and Form2 - that interact with a SQL database called Producto2. Form1 allows the user to view, add, and search for product data. It contains methods to retrieve area and brand names from IDs. Form2 allows filtering products by area and displays the results in a data table. It contains a method to populate an area list and retrieve area IDs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Chimu Elo

This document contains code for two classes - Form1 and Form2 - that interact with a SQL database called Producto2. Form1 allows the user to view, add, and search for product data. It contains methods to retrieve area and brand names from IDs. Form2 allows filtering products by area and displays the results in a data table. It contains a method to populate an area list and retrieve area IDs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Imports System.Data.

SqlClient
Imports System.Data
Public Class Form1
Dim cn As New
SqlConnection("Server=localhost;uid=sa;password=123;database=Producto2")
Public Function DevolverIdArea(ByVal nom As String) As String
Dim codigo As String = ""
Dim dr As SqlDataReader
Dim cmd As New SqlCommand("select IdA from Area where Nombre='" + nom + "'",
cn)
cn.Open()
dr = cmd.ExecuteReader
While dr.Read
codigo = dr("IdA")
End While
cn.Close()
cmd.Dispose()
DevolverIdArea = codigo
End Function

Public Function DevolverNombreArea(ByVal IdA As String) As String
Dim nom As String = ""
Dim dr As SqlDataReader
Dim cmd As New SqlCommand("select Nombre from Area where IdA='" + IdA + "'",
cn)
cn.Open()
dr = cmd.ExecuteReader
While dr.Read
nom = dr("Nombre")
End While
cn.Close()
cmd.Dispose()
DevolverNombreArea = nom
End Function
Public Sub PoblarAreas()
Dim dr As SqlDataReader
Dim cmd As New SqlCommand("select Nombre from Area", cn)
cboArea.Items.Clear()
cn.Open()
dr = cmd.ExecuteReader
While dr.Read
cboArea.Items.Add(dr("Nombre"))
End While
cn.Close()
cmd.Dispose()
End Sub












Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PoblarAreas()
PoblarMarcas()
Limpiar()
bloquear()
End Sub
Public Function DevolverIdMarca(ByVal nom As String) As String
Dim codigo As String = ""
Dim dr As SqlDataReader
Dim cmd As New SqlCommand("select IdM from Marca where Nombre='" + nom +
"'", cn)
cn.Open()
dr = cmd.ExecuteReader
While dr.Read
codigo = dr("IdM")
End While
cn.Close()
cmd.Dispose()
DevolverIdMarca = codigo
End Function
Public Function DevolverNombreMarca(ByVal IdM As String) As String
Dim nom As String = ""
Dim dr As SqlDataReader
Dim cmd As New SqlCommand("select Nombre from Marca where IdM='" + IdM +
"'", cn)
cn.Open()
dr = cmd.ExecuteReader
While dr.Read
nom = dr("Nombre")
End While
cn.Close()
cmd.Dispose()
DevolverNombreMarca = nom
End Function
Public Sub PoblarMarcas()
Dim dr As SqlDataReader
Dim cmd As New SqlCommand("select Nombre from Marca", cn)
cboMarca.Items.Clear()
cn.Open()
dr = cmd.ExecuteReader
While dr.Read
cboMarca.Items.Add(dr("Nombre"))
End While
cn.Close()
cmd.Dispose()
End Sub

Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGrabar.Click

Dim codigo As String = DevolverIdArea(cboArea.Text)
Dim cod As String = DevolverIdMarca(cboMarca.Text)
Dim sql As String = "Insert into Producto values('" + txtCod.Text + "','" +
codigo + "','" + cod + "','" + txtNombre.Text + "','" + txtreg.Text + "','" +
txtPrecio.Text + "','" + txtS.Text + "','" + cboE.Text + "')"
Dim cmd As New SqlCommand(sql, cn)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
cmd.Dispose()
MsgBox("REGISTRO ALMACENADO")

End Sub

Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnConsultar.Click
Dim ds As New DataSet
Dim codigo As String = InputBox("INGRESE CODIGO DE Producto:")
Dim da As New SqlDataAdapter("select * from Producto where IdP='" + codigo +
"'", cn)
da.Fill(ds, "Consulta")
If ds.Tables("Consulta").Rows.Count > 0 Then
txtCod.Text = ds.Tables("Consulta").Rows(0)(0).ToString()
cboArea.Text =
DevolverNombreArea(ds.Tables("Consulta").Rows(0)(1).ToString())
cboMarca.Text =
DevolverNombreMarca(ds.Tables("Consulta").Rows(0)(2).ToString())
txtNombre.Text = ds.Tables("Consulta").Rows(0)(3).ToString()
txtPrecio.Text = ds.Tables("Consulta").Rows(0)(4).ToString()
txtS.Text = ds.Tables("Consulta").Rows(0)(5).ToString()
cboE.Text = ds.Tables("Consulta").Rows(0)(6).ToString()
Else
MsgBox("PRODUCTO NO EXISTE")
Limpiar()
End If
End Sub
Public Sub Limpiar()
txtCod.Text = ""
txtNombre.Text = ""
txtPrecio.Text = ""
txtS.Text = ""
cboArea.Text = ""
cboMarca.Text = ""
txtCod.Focus()
cboE.Text = ""
txtreg.Text = Date.Now
End Sub
Public Sub bloquear()
txtreg.Enabled = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Form2.Show()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Close()
End Sub
End Class

FILTROS
Imports System.Data.SqlClient
Imports System.Data
Public Class Form2
Dim cn As New
SqlConnection("Server=localhost;uid=sa;password=123;database=Producto2")

Public Sub PoblarAreas()
Dim dr As SqlDataReader
Dim cmd As New SqlCommand("select Nombre from Area", cn)
lbarea.Items.Clear()
cn.Open()
dr = cmd.ExecuteReader
While dr.Read
lbarea.Items.Add(dr("Nombre"))
End While
cn.Close()
cmd.Dispose()
End Sub
Public Function DevolverIdArea(ByVal nom As String) As String
Dim codigo As String = ""
Dim dr As SqlDataReader
Dim cmd As New SqlCommand("select IdA from Area where Nombre='" + nom + "'",
cn)
cn.Open()
dr = cmd.ExecuteReader
While dr.Read
codigo = dr("IdA")
End While
cn.Close()
cmd.Dispose()
DevolverIdArea = codigo
End Function

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PoblarAreas()
End Sub

Private Sub lbarea_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lbarea.Click
If lbarea.SelectedIndex <> -1 Then
dt1.DataSource = Nothing
Dim ida As String = DevolverIdArea(lbarea.Text)
Dim ds As New DataSet
Dim da As New SqlDataAdapter("select IdP,Nombre,Precio,Stock," + "Estado
from Producto where IdA ='" + ida + "'", cn)
da.Fill(ds, "filtro1")
If ds.Tables("filtro1").Rows.Count > 0 Then
dt1.DataSource = ds.Tables("filtro1")
Else
dt1.DataSource = Nothing
End If
Else
MsgBox("debe exixtir o seleccionar area de la lista")
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class

You might also like