[go: up one dir, main page]

0% found this document useful (0 votes)
28 views3 pages

LAB05

This document contains code for a student form application that demonstrates a one-to-many relationship between students and programs in a database. The form allows a user to select a program from a dropdown, input a student name and admission status, and insert a new student record into the database table with a foreign key reference to the selected program. It also includes a button to query the database and display related student and program data in a datagrid.

Uploaded by

Saqlain Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

LAB05

This document contains code for a student form application that demonstrates a one-to-many relationship between students and programs in a database. The form allows a user to select a program from a dropdown, input a student name and admission status, and insert a new student record into the database table with a foreign key reference to the selected program. It also includes a button to query the database and display related student and program data in a datagrid.

Uploaded by

Saqlain Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Visual Programming

LAB 5

Submitted By
Hureen Shahid RC – 192 Urwa Malik RC – 195
Irum Sajjad RC – 221 Aqsa Arif RC – 191
Ali Kashif RC-210

Submitted To
Sir Seemab

CLASS: BSCS-35 (6th Semester)


Apply One to Many Relationship

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace onttomanyBSCS6
{
public partial class Student : Form
{
SqlConnection con = new SqlConnection(@"Data Source=Lenovo-
G50;Initial Catalog=Test2;Integrated Security=True");

public Student()
{
InitializeComponent();
}

private void Student_Load(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("select * from Program1",
con); DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
comboBox1.DataSource=dt;
comboBox1.DisplayMember = "ProgramName";
comboBox1.ValueMember = "ProgramId";
}

private void button1_Click(object sender, EventArgs e)


{
string a = null;
if (radioButton1.Checked)
a = "Yes";
else
a = "No";
// SqlConnection con = new SqlConnection(@"Data
Source=HAIRE;Initial Catalog=BSCS6db;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into
Student(StudentName,StudentAdmission,ProgramID) values('" + textBox1.Text
+ "','"+a+"','"+Convert.ToInt32(comboBox1.SelectedValue)+"')", con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
MessageBox.Show("Record inserted");
else
MessageBox.Show("Record not inserted");

private void button2_Click(object sender, EventArgs e)


{
string q = "SELECT Student.StudentName, Program1.ProgramName,
Student.StudentAdmission FROM Program1 INNER JOIN Student ON Program1.ProgramId
= Student.ProgramID";
SqlCommand cmd = new SqlCommand(q, con);
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}

You might also like