You are on page 1of 5

TRABAJO CON BASE DE DATOS

CODIGO BOTON NUEVO


Session("vmodoedicion") = False limpiar() generanuevocod() BtnBuscar.Visible = False

PROCEDIMIENTO LIMPIAR TxtCodigo.Text = "" TxtNombre.Text = "" TxtDireccion.Text = "" Txttelefono.Text = "" LblMensaje.Text = "" TxtCodigo.Focus() PROCEDIMIENTO GENERA NUEVO CODIGO Dim cn As New SqlConnection("Data Source=SERVERLAP\SQLEXPRESS;Initial Catalog=BDVentas;Integrated Security=True") cn.Open() Dim cmdsql As New SqlCommand("select max(codcliente) as codigo from clientes", cn) Dim da As New SqlDataAdapter(cmdsql) Dim ds As New DataSet da.Fill(ds) If ds.Tables(0).Rows.Count = 0 Then TxtCodigo.Text = "0001" Else TxtCodigo.Text = "C" + Right("000" + Trim(Str(Val(Mid(ds.Tables(0).Rows(0).Item(0).ToString, 2, 3))) + 1), 3) End If End Sub

En el Evento Page Load de la Pagina


If Page.IsPostBack = False Then Session("vmodoedicion") = True TxtCodigo.Text = Request.QueryString("cod") TxtNombre.Text = Request.QueryString("nom") TxtDireccion.Text = Request.QueryString("dir") Txttelefono.Text = Request.QueryString("tel") End If

PROCEDIMIENTO GRABAR If Session("vmodoedicion") = False Then insertar() Else modificar() End If PROCEDIMIENTO INSERTAR Dim cn As New SqlConnection Dim mycommand As SqlCommand Dim cadcn As String cadcn = "Data Source=SERVERLAP\SQLEXPRESS;Initial Catalog=BDVentas;Integrated Security=True" cn.ConnectionString = cadcn

cn.Open() Dim insertcmd As String = "insert into clientes(codcliente,nomcliente,dircliente,telcliente)" & _ " values ('" & TxtCodigo.Text & "','" & TxtNombre.Text & "','" & TxtDireccion.Text & "','" & Txttelefono.Text & "' )" mycommand = New SqlCommand(insertcmd, cn) Try mycommand.ExecuteNonQuery() Catch exp As SqlException If exp.Number = 2627 Then LblMensaje.Text = "Error ya existe clave" Else LblMensaje.Text = "No pudo grabar Registro" End If End Try LblMensaje.Text = "Registro Guardado" cn.Close() PROCEDIMIENTO MODIFICAR Dim cn As New SqlConnection Dim mycommand As SqlCommand Dim cadcn As String cadcn = "Data Source=SERVERLAP\SQLEXPRESS;Initial Catalog=BDVentas;Integrated Security=True" cn.ConnectionString = cadcn cn.Open() Dim actualizacmd As String = "update clientes set nomcliente='" & TxtNombre.Text & "', dircliente='" & TxtDireccion.Text & "', telcliente='" & Txttelefono.Text & "' where codcliente='" & TxtCodigo.Text & "'" mycommand = New SqlCommand(actualizacmd, cn) mycommand.ExecuteNonQuery() End Sub CODIGO BOTON MODIFICAR TxtCodigo.Enabled = True Session("vmodoedicion") = True limpiar() BtnBuscar.Visible = True

CAJA DE TEXTO EVENTO TEXTCHANGED If Session("vmodoedicion") = True Then Dim cn As New SqlConnection("Data Source=SERVERLAP\SQLEXPRESS;Initial Catalog=BDVentas;Integrated Security=True") cn.Open()

Dim cmdsql As New SqlCommand("select * from clientes where codcliente='" & TxtCodigo.Text & "'", cn) Dim da As New SqlDataAdapter(cmdsql) Dim ds As New DataSet da.Fill(ds) If ds.Tables(0).Rows.Count = 0 Then LblMensaje.Text = "Codigo No Existe" Else TxtNombre.Text = ds.Tables(0).Rows(0).Item(1).ToString TxtDireccion.Text = ds.Tables(0).Rows(0).Item(2).ToString Txttelefono.Text = ds.Tables(0).Rows(0).Item(3).ToString End If End If BOTON ELIMINAR Dim cn As New SqlConnection Dim mycommand As SqlCommand Dim cadcn As String cadcn = "Data Source=SERVERLAP\SQLEXPRESS;Initial Catalog=BDVentas;Integrated Security=True" cn.ConnectionString = cadcn cn.Open() Dim deletecmd As String = "delete from clientes " & _ " where codcliente='" & TxtCodigo.Text & "'" mycommand = New SqlCommand(deletecmd, cn) mycommand.ExecuteNonQuery() cn.Close()

BOTON BUSCAR Response.Redirect("bcliente.aspx") BOTON CANCELAR limpiar() BtnBuscar.Visible = False PAGINA BUSCAR If IsPostBack = False Then mostrardatos() End If PROCEDIMIENTO MOSTRAR DATOS Dim dsDatos As New DataSet Dim cn As New SqlConnection("Data Source=SERVERLAP\SQLEXPRESS;Initial Catalog=BDVentas;Integrated Security=True") Dim daClientes As New SqlDataAdapter("select * from clientes", cn)

daClientes.Fill(dsDatos, "Clientes") GrvClientes.DataSource = dsDatos.Tables("Clientes") GrvClientes.DataBind() Protected Sub BtnBuscar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBuscar.Click Dim cn As New SqlConnection("Data Source=SERVERLAP\SQLEXPRESS;Initial Catalog=BDVentas;Integrated Security=True") cn.Open() Dim cmdsql As New SqlCommand("select * from clientes where nomcliente like '" & TxtNombres.Text & "%'", cn) Dim da As New SqlDataAdapter(cmdsql) Dim ds As New DataSet da.Fill(ds) GrvClientes.DataSource = ds.Tables(0) GrvClientes.DataBind() End Sub Protected Sub GrvClientes_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GrvClientes.PageIndexChanging GrvClientes.PageIndex = e.NewPageIndex mostrardatos() End Sub Protected Sub GrvClientes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GrvClientes.SelectedIndexChanged Dim cod, nom, dir, tel As String cod = GrvClientes.Rows(GrvClientes.SelectedIndex).Cells(1).Text nom = GrvClientes.Rows(GrvClientes.SelectedIndex).Cells(2).Text dir = GrvClientes.Rows(GrvClientes.SelectedIndex).Cells(3).Text tel = GrvClientes.Rows(GrvClientes.SelectedIndex).Cells(4).Text Response.Redirect("MClientes.aspx?cod=" & cod & "&nom=" & nom & "&dir=" & dir & "&tel=" & tel & "") End Sub

You might also like