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

One To-Many

Uploaded by

Raja G
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)
22 views4 pages

One To-Many

Uploaded by

Raja G
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/ 4

Relational Database (One – to – Many)?

Solution:
Student.css:
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 One_to_Many
{
public partial class Student : Form
{
SqlConnection con = new SqlConnection(@"Data Source=Muddasir-Ismail;Initial
Catalog=One-to-Many;Integrated Security=True");
public Student()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
string a = "";
if (radioButton1.Checked)
{
a = "Yes";
}
else
{
a = "No";
}
SqlCommand cmd = new SqlCommand("insert into Student(Student Name,
Address, Program ID) 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 Student_Load(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("select *from Course", con);
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Course Name";
comboBox1.ValueMember = "Course ID";

private void button2_Click(object sender, EventArgs e)


{
string q = "SELECT Student.[Student Name], Course.[Course Name],
Student.Address FROM Course INNER JOIN Student ON Course.[Course ID] =
Student.[Course ID]";
SqlCommand cmd = new SqlCommand(q, con);
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
Course.css:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace One_to_Many
{
public partial class Courses : Form
{
public Courses()
{
InitializeComponent();
}

private void linkLabel1_LinkClicked(object sender,


LinkLabelLinkClickedEventArgs e)
{
Student obj = new Student();
obj.ShowDialog(); // first method to show student form
//obj.Show(); // second method to show student form
// this.Close();
}

private void button1_Click(object sender, EventArgs e)


{
SqlConnection con = new SqlConnection(@"Data Source=Muddasir-
Ismail;Initial Catalog=One-to-Many;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into Course(Course Name)
values('"+textBox1.Text+"')",con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if(i > 0)
{
MessageBox.Show("Record Inserted!!");
}
else
{
MessageBox.Show("Record not Inserted!!");
}
}
}
}

You might also like