Você está na página 1de 16

APLICACIÓN CON MÚLTIPLES

TABLAS
Aplicación

Diseñe una aplicación que permita registrar la matrícula de


alumnos en un determinado curso.

Solución

1.Script de la base de datos


use master
go
create database webmatricula
go
use webmatricula
go
create table alumnos (
idal int identity primary key not null,
nomal varchar(40) not null,
apal varchar(40) not null,
diral varchar(40) not null,
genal varchar(1) not null)
go
create table cursos (
idcur int identity primary key not null,
nomcur varchar(30) not null)
go
create table profesor (
idprof int identity primary key not null,
nomprof varchar(40) not null)
go
create table matricula (
idmat int identity primary key not null,
idcur int foreign key references cursos,
idprof int foreign key references profesor,
turno varchar(30) not null,
horario varchar(30) not null)
go
create table dmatricula (
iddmat int identity primary key not null,
idmat int foreign key references matricula,
idal int foreign key references alumnos,
nomal varchar(80) not null)
go
insert into cursos values('Windows')
insert into cursos values('Word')
insert into cursos values('Excel')
insert into cursos values('Internet')
insert into cursos values('Visual NET')
insert into cursos values('PHP')
insert into cursos values('Power Builder')
insert into cursos values('Java')
insert into cursos values('ASP.NET')
insert into cursos values('Oracle')
go
select * from cursos
go
insert into profesor values('Jerónimo Pardo Luján')
insert into profesor values('Tatiana Flores Padilla')
insert into profesor values('Raymundo Olórtegui Dávalos')
go
select * from profesor
go
insert into alumnos values('Melissa','Gonzales Mendieta','Lima','F')
insert into alumnos values('Maria','Tapia Villar','San Juan de
Miraflores','F')
insert into alumnos values('Fabiana','Villanueva Jimenez','Lince','F')
insert into alumnos values('Fabricio','Fernandez Trelles','Surco','M')
insert into alumnos values('Mateo','Lopez Perez','Lima','M')
go
select * from alumnos
go

2.Crear el proyecto PROY_MATRICULA en CSharp


a)Diseño
b)Código Behind

<%@ Page Language="C#" AutoEventWireup="true"


CodeBehind="matricula.aspx.cs" Inherits="PROY_MATRICULA.matricula" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">
<title>Registro de Matrícula</title>
<style type="text/css">
.style1
{
width: 214px;
}
.style3
{
width: 144px;
}
.style4
{
width: 104px;
}
.style5
{
width: 197px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table style="width:109%;" bgcolor="#FFFF80">
<tr>
<td class="style4">
<asp:Label ID="Label10" runat="server"
Text="Matrícula N°" Font-Bold="True"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txt_idmat"
runat="server"></asp:TextBox>
</td>
<td class="style5">
<asp:Button ID="Button4" runat="server"
onclick="Button4_Click"
Text="Nueva Matrícula" Font-Bold="True" Width="117px"
/>
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label1" runat="server"
Text="Seleccione Curso" Font-Bold="True"></asp:Label>
</td>
<td class="style3">
<asp:DropDownList ID="ddl_cursos" runat="server"
AutoPostBack="True"

onselectedindexchanged="ddl_cursos_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td class="style5">
<asp:Label ID="Label11" runat="server"
Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label2" runat="server" Text="Código
Curso" Font-Bold="True"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txt_curso"
runat="server"></asp:TextBox>
</td>
<td class="style5">
<asp:Button ID="Button3" runat="server"
onclick="Button3_Click"
Text="Guardar" Font-Bold="True" />
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label3" runat="server"
Text="Seleccione Profesor"
Font-Bold="True"></asp:Label>
</td>
<td class="style3">
<asp:DropDownList ID="ddl_profesor" runat="server"
AutoPostBack="True"

onselectedindexchanged="ddl_profesor_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td class="style5">
&nbsp;</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label4" runat="server" Text="Código
Profesor" Font-Bold="True"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txt_profesor"
runat="server"></asp:TextBox>
</td>
<td class="style5">
&nbsp;</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label5" runat="server" Text="Turno" Font-
Bold="True"></asp:Label>
</td>
<td class="style3">
<asp:DropDownList ID="ddl_turno" runat="server">
<asp:ListItem>Mañana</asp:ListItem>
<asp:ListItem>Tarde</asp:ListItem>
<asp:ListItem>Noche</asp:ListItem>
</asp:DropDownList>
</td>
<td class="style5">
&nbsp;</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label6" runat="server" Text="Horario"
Font-Bold="True"></asp:Label>
</td>
<td class="style3">
<asp:DropDownList ID="ddl_horario" runat="server">
<asp:ListItem>L-M-V- 08:00-10:00</asp:ListItem>
<asp:ListItem>M-J-08:00-10:00</asp:ListItem>
<asp:ListItem>L-M-V-15:00-17:00</asp:ListItem>
<asp:ListItem>M-J-15:00-17:00</asp:ListItem>
<asp:ListItem>L-M-V-18:00-20:00</asp:ListItem>
<asp:ListItem>M-J-18:00-20:00</asp:ListItem>
</asp:DropDownList>
</td>
<td class="style5">
&nbsp;</td>
</tr>
<tr>
<td class="style4">
&nbsp;</td>
<td class="style3">
&nbsp;</td>
<td class="style5">
&nbsp;</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label7" runat="server" Text="Seleccione
Alumno" Font-Bold="True"></asp:Label>
</td>
<td class="style3">
<asp:DropDownList ID="ddl_alumnos" runat="server"
Height="16px" Width="156px"
AutoPostBack="True"

onselectedindexchanged="ddl_alumnos_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td class="style5">
&nbsp;</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label8" runat="server" Text="Código
Alumno" Font-Bold="True"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txt_codal" runat="server"></asp:TextBox>
</td>
<td class="style5">
&nbsp;</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label12" runat="server" Font-Bold="True"
Text="Nombres"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txt_nomal" runat="server"></asp:TextBox>
</td>
<td class="style5">
&nbsp;</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label9" runat="server" Text="Apellidos"
Font-Bold="True"></asp:Label>
</td>
<td class="style3">
<asp:TextBox ID="txt_apal" runat="server"></asp:TextBox>
</td>
<td class="style5">
&nbsp;</td>
</tr>
<tr>
<td class="style4">
<asp:Button ID="Button1" runat="server" Text="Agregar"
onclick="Button1_Click" Font-Bold="True" />
</td>
<td class="style3">
<asp:Button ID="Button5" runat="server"
onclick="Button5_Click"
Text="Nuevo Alumno" Font-Bold="True" />
</td>
<td class="style5">
&nbsp;</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="lbl_mensaje" runat="server"
Text="Label"></asp:Label>
</td>
<td class="style3">
&nbsp;</td>
<td class="style5">
&nbsp;</td>
</tr>
</table>
<table style="width:109%;" bgcolor="#FFFF80">
<tr>
<td>
<asp:ListBox ID="lst_codal" runat="server" Font-
Bold="True" Width="100px"></asp:ListBox>
</td>
<td class="style1" bgcolor="#FFFF80">
<asp:ListBox ID="lst_nomal" runat="server" Font-
Bold="True" Width="200px"></asp:ListBox>
</td>
<td class="style1" bgcolor="#FFFF80">
&nbsp;</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lbl_mensaje_detalle" runat="server"
Text="Label"></asp:Label>
</td>
<td>
&nbsp;</td>
</tr>
</table>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="~/reporte_matricula.aspx">Ir al
Reporte</asp:HyperLink>
</form>
</body>
</html>
c)Código de la Clase
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace PROY_MATRICULA
{
public partial class matricula : System.Web.UI.Page
{
private string connectionString =
("server=.;database=WEBMATRICULA;uid=sa;pwd=123");
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
cargar_cursos();
cargar_profesor();
cargar_alumnos();
}

private void cargar_cursos()


{
ddl_cursos.Items.Clear();
string selectSQL = "SELECT idcur,nomcur from cursos";

SqlConnection con = new SqlConnection(connectionString);


SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;

try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = reader["nomcur"].ToString();
newItem.Value = reader["idcur"].ToString();
ddl_cursos.Items.Add(newItem);
}
reader.Close();
}
catch (Exception err)
{
//lblResultado.Text = "Error leyendo lista de empresas.
";
//lblResultado.Text += err.Message;
}
finally
{
con.Close();
}
}

protected void ddl_cursos_SelectedIndexChanged(object sender,


EventArgs e)
{
string selectSQL;
selectSQL = "select * from cursos ";
selectSQL += "where idcur='" + ddl_cursos.SelectedItem.Value
+ "'";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;

try
{
con.Open();
reader = cmd.ExecuteReader();
reader.Read();

txt_curso.Text = reader["idcur"].ToString();
reader.Close();

}
catch (Exception err)
{
//lblResultado.Text = "Error obteniendo empresa. ";
//lblResultado.Text += err.Message;
}
finally
{
con.Close();
}
}

protected void ddl_profesor_SelectedIndexChanged(object sender,


EventArgs e)
{
string selectSQL;
selectSQL = "select * from profesor ";
selectSQL += "where idprof='" +
ddl_profesor.SelectedItem.Value + "'";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;

try
{
con.Open();
reader = cmd.ExecuteReader();
reader.Read();

txt_profesor.Text = reader["idprof"].ToString();
reader.Close();

}
catch (Exception err)
{
//lblResultado.Text = "Error obteniendo empresa. ";
//lblResultado.Text += err.Message;
}
finally
{
con.Close();
}
}

private void cargar_profesor()


{
ddl_profesor.Items.Clear();
string selectSQL = "SELECT idprof,nomprof from profesor";

SqlConnection con = new SqlConnection(connectionString);


SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;

try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = reader["nomprof"].ToString();
newItem.Value = reader["idprof"].ToString();
ddl_profesor.Items.Add(newItem);
}
reader.Close();
}
catch (Exception err)
{
//lblResultado.Text = "Error leyendo lista de empresas.
";
//lblResultado.Text += err.Message;
}
finally
{
con.Close();
}
}

private void cargar_alumnos()


{
ddl_alumnos.Items.Clear();
string selectSQL = "SELECT idal,nomal,apal from alumnos";

SqlConnection con = new SqlConnection(connectionString);


SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;

try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = reader["apal"].ToString();
newItem.Value = reader["idal"].ToString();
ddl_alumnos.Items.Add(newItem);
}
reader.Close();
}
catch (Exception err)
{
//lblResultado.Text = "Error leyendo lista de empresas.
";
//lblResultado.Text += err.Message;
}
finally
{
con.Close();
}
}

protected void ddl_alumnos_SelectedIndexChanged(object sender,


EventArgs e)
{
string selectSQL;
selectSQL = "select * from alumnos ";
selectSQL += "where idal='" + ddl_alumnos.SelectedItem.Value
+ "'";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;

try
{
con.Open();
reader = cmd.ExecuteReader();
reader.Read();

txt_codal.Text = reader["idal"].ToString();
txt_nomal.Text = reader["nomal"].ToString();
txt_apal.Text = reader["apal"].ToString();
reader.Close();

}
catch (Exception err)
{
//lblResultado.Text = "Error obteniendo empresa. ";
//lblResultado.Text += err.Message;
}
finally
{
con.Close();
}
}

private int calcular_idmat()


{
string cadsql = "select idmat from matricula order by idmat";

SqlConnection con = new SqlConnection(connectionString);


SqlCommand cmd = new SqlCommand(cadsql, con);
SqlDataReader reader;
int idmat = 0;
try
{
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
idmat = Int32.Parse(reader["idmat"].ToString());
}
reader.Close();
}
catch (Exception err)
{
Label11.Text = "Error leyendo tabla matricula. ";
Label11.Text += err.Message;
}
finally
{
con.Close();
}
return idmat;
}

protected void Button1_Click(object sender, EventArgs e)


{
lst_codal.Items.Add(txt_codal.Text);
lst_nomal.Items.Add(txt_apal.Text+" "+txt_nomal.Text);

protected void Button3_Click(object sender, EventArgs e)


{
string insertSQL;
insertSQL = "insert into matricula (";
insertSQL += "idcur,idprof,turno,horario) values (";
insertSQL += "@idcur, @idprof, @turno, @horario)";

SqlConnection con = new SqlConnection(connectionString);


SqlCommand cmd = new SqlCommand(insertSQL, con);

cmd.Parameters.AddWithValue("@idcur", txt_curso.Text);
cmd.Parameters.AddWithValue("@idprof", txt_profesor.Text);
cmd.Parameters.AddWithValue("@turno",
ddl_turno.SelectedValue);
cmd.Parameters.AddWithValue("@horario",
ddl_horario.SelectedValue);
int added = 0;
try
{
con.Open();
added = cmd.ExecuteNonQuery();
lbl_mensaje.Text = added.ToString() + " registro
insertado.";
}
catch (Exception err)
{
lbl_mensaje.Text = "Error insertando matricula. ";
lbl_mensaje.Text += err.Message;
}
finally
{
con.Close();
}

if (added > 0)
{
//Rellenar_Empresa();
}

int i, n;
n = lst_codal.Items.Count;
string sql_dmatricula = null;
int idmat=calcular_idmat();
txt_idmat.Text = idmat.ToString();

for (i = 0; i <= n - 1; i++)


{

sql_dmatricula = "insert into dmatricula (";


sql_dmatricula += "idmat,idal,nomal) values (";
sql_dmatricula += "@idmat, @idal, @nomal)";

SqlConnection con1 = new SqlConnection(connectionString);


SqlCommand cmd1 = new SqlCommand(sql_dmatricula, con1);

string idal= (string)(lst_codal.Items[i].Text);


string nomal = (string)(lst_nomal.Items[i].Text);

cmd1.Parameters.AddWithValue("@idmat", txt_idmat.Text);
cmd1.Parameters.AddWithValue("@idal", idal);
cmd1.Parameters.AddWithValue("@nomal", nomal);
int added1 = 0;
try
{
con1.Open();
added = cmd1.ExecuteNonQuery();
lbl_mensaje_detalle.Text = added.ToString() + "
registro de alumno insertado.";
}
catch (Exception err)
{
lbl_mensaje_detalle.Text = "Error insertando alumno
en dmatricula. ";
lbl_mensaje_detalle.Text += err.Message;
}
finally
{
con1.Close();
}

if (added1 > 0)
{
//Rellenar_Empresa();
}

protected void Button4_Click(object sender, EventArgs e)


{
int idmat;
idmat=calcular_idmat()+1;
txt_idmat.Text = idmat.ToString();
txt_curso.Text = "";
txt_profesor.Text = "";
txt_codal.Text = "";
txt_nomal.Text = "";
txt_apal.Text = "";
ddl_alumnos.SelectedIndex = -1;
ddl_cursos.SelectedIndex = -1;
ddl_profesor.SelectedIndex = -1;
ddl_turno.SelectedIndex = -1;
ddl_horario.SelectedIndex = -1;
lst_codal.Items.Clear();
lst_nomal.Items.Clear();
}

protected void Button2_Click(object sender, EventArgs e)


{
//ListBox1.Items.Remove(ListBox1.SelectedItem.Value);
}

protected void Button5_Click(object sender, EventArgs e)


{
txt_codal.Text = "";
txt_nomal.Text = "";
txt_apal.Text = "";
}

}
}
Ejecución

Você também pode gostar