0% found this document useful (0 votes)
25 views7 pages

Im 223 - Linq Lab-Updated

This document provides code requirements and guidelines for students to create a Student Resource Information System (SRIS) using LINQ to SQL class coding at Sultan Kudarat State University. It outlines tasks for students to design a database and user interface, create classes and forms, and code the application using methods like adding, updating, deleting and searching student records from the database. Code snippets are provided as examples for common tasks like loading student data from the database table to a data grid view, showing the first record in the grid to textboxes, and using LINQ queries to add, update and delete records.

Uploaded by

CHRISTIAN SORIO
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)
25 views7 pages

Im 223 - Linq Lab-Updated

This document provides code requirements and guidelines for students to create a Student Resource Information System (SRIS) using LINQ to SQL class coding at Sultan Kudarat State University. It outlines tasks for students to design a database and user interface, create classes and forms, and code the application using methods like adding, updating, deleting and searching student records from the database. Code snippets are provided as examples for common tasks like loading student data from the database table to a data grid view, showing the first record in the grid to textboxes, and using LINQ queries to add, update and delete records.

Uploaded by

CHRISTIAN SORIO
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/ 7

Republic of the Philippines

SULTAN KUDARAT STATE UNIVERSITY


IT Department
Kalamansig, Sultan Kudarat

IM 223 – Advanced DBMS REVISED ACTIVITY GUIDE


Student Name: __________________________________ Yr/Section ________________________
Objectives:
➢ Enable students create a Student Resource Information System(SRIS) using
LINQtoSQL Class Coding method. Enhance student’s mastery on creating database
and table structure.
Task to Do:

1. Create a database in SQL Server 2008. Name it “SRIS”. Then, create a table based
on the USER interface below. Save your table as “tblStudent”.
2. Design your application using the interface below. You may enhance the design
interface but observer proper naming convention.

3. Save the project as SRIS, then navigate to C:\Users\Acer\Documents\Visual Studio


2008\Projects\SRIS\bin\Debug and create a folder here named “Image”.
4. Create a class folder named “CLASS” under your project name in project explorer
window. Add new class to the folder and named it “studentcodes.vb”
5. Add new LINQTOSQL dbml file in your project. Navigate to Project explorer window
and browse your database server “localhost” and connect your database “SRIS” in
the current project.

5. Then, start coding the application using the following code requirements (70
points)
5.1 studentcodes class

a. General Declarations, 2 points


b. Add, 5 points
c. Update, 5 points
d. Delete, 5 points
e. Search, 5 points
f. Load , 2 points
g. ShowtoFirstRec, 2 points
h. ShowToGrid , 2 points
i. ClearInputs 2 points

5.2 RegisterStudent Form


a. FormLoad Event 5 points
b. Add Button, 5 points
c. Update Button, 5 points
d. Delete Button, 5 points
e. Grid CellClick Event , 5 points
f. SearchBox changed Event 5 points
g. SaveImage Sub Procedure 5 points
h. Picture Box Click Event 5 points
Rubrics for Application Checking:
Code Requirements (70 pts)
Coding and Debugging Skill (30 pt____
100 Pts

studentcodes.vb (CLASS CODES)


Republic of the Philippines
SULTAN KUDARAT STATE UNIVERSITY
IT Department
Kalamansig, Sultan Kudarat

1. Imports (put this code outside the class)

Imports System
Imports System.Data.Linq
Imports System.IO
Imports SRIS.SRISDataContext

2. Add the following within your class (ClassModule e.g. main)

Public Shared dbcon As New SRISDataContext

3. Code for Add

Public Shared Sub AddStudent()


Dim MemStream As New MemoryStream
Dim DataPic_Insert As Byte()
register_student.pb1.Image.Save(MemStream, Imaging.ImageFormat.Jpeg)
DataPic_Insert = MemStream.GetBuffer()
MemStream.Read(DataPic_Insert, 0, MemStream.Length)
Dim newstudent As New tblStudent With { _
.StudentID = register_student.txtStudID.Text, _
.name = register_student.txtName.Text, _
.gender = register_student.cboGender.Text, _
.contact = register_student.txtContact.Text, _
.course = register_student.cboCourse.Text, _
.major = register_student.cboMajor.Text, _
.year = register_student.cboYear.Text, _
.section = register_student.cboSection.Text, _
.photo = DataPic_Insert}
dbcon.tblStudents.InsertOnSubmit(newstudent)
Try
dbcon.SubmitChanges()
MessageBox.Show("Successfully Saved!", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Catch ex As Exception
End Try
End Sub

4. Code for Update

Public Shared Sub UpdateStudent(ByVal strCTRL As String) 'save


Dim MemStream As New MemoryStream
Dim DataPic_Insert As Byte()
register_student.pb1.Image.Save(MemStream, Imaging.ImageFormat.Jpeg)
DataPic_Insert = MemStream.GetBuffer()
MemStream.Read(DataPic_Insert, 0, MemStream.Length)

Dim student = (From studentrecords In dbcon.tblStudents _


Where studentrecords.StudNo = strCTRL).ToList(0)
student.StudentID = register_student.txtStudID.Text
student.name = register_student.txtName.Text
student.gender = register_student.cboGender.Text
student.contact = register_student.txtContact.Text
student.course = register_student.cboCourse.Text
student.major = register_student.cboMajor.Text
student.year = register_student.cboYear.Text
student.section = register_student.cboSection.Text
Try
dbcon.SubmitChanges()
LoadStudents(dtgStudent:=register_student.dtgStudent)
Catch ex As Exception
End Try

End Sub
Public Shared Sub Delete(ByVal strCTRL As String)
If register_student.dtgStudent.Rows.Count <> 0 Then
Dim deletedStudents = (From studentrecords In dbcon.GetTable(Of tblStudent)() _
Where studentrecords.StudNo = strCTRL Select
studentrecords).SingleOrDefault
dbcon.GetTable(Of tblStudent)().DeleteOnSubmit(deletedStudents)
Try
dbcon.SubmitChanges()
MessageBox.Show("Student named: " & register_student.txtName.Text & " was
successfully deleted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
studentcodes.LoadStudents(dtgStudent:=register_student.dtgStudent)
studentcodes.ShowFirstRecord()
Catch ex As Exception
End Try
Else
MsgBox("No Records can be deleted!")
End If
Republic of the Philippines
SULTAN KUDARAT STATE UNIVERSITY
IT Department
Kalamansig, Sultan Kudarat

End Sub

5. Code for Delete Using LINQ (ClassModule e.g. studentcodes)

Public Shared Sub Delete(ByVal strCTRL As String)


If register_student.dtgStudent.Rows.Count <> 0 Then
Dim deletedStudents = (From studentrecords In dbcon.GetTable(Of tblStudent)() _
Where studentrecords.StudNo = strCTRL Select
studentrecords).SingleOrDefault
dbcon.GetTable(Of tblStudent)().DeleteOnSubmit(deletedStudents)
Try
dbcon.SubmitChanges()
MessageBox.Show("Student named: " & register_student.txtName.Text & " was
successfully deleted", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
studentcodes.LoadStudents(dtgStudent:=register_student.dtgStudent)
studentcodes.ShowFirstRecord()
Catch ex As Exception
End Try
Else
MsgBox("No Records can be deleted!")
End If
End Sub

6. Code for Load Students

Public Shared Sub LoadStudents(ByRef dtgStudent As DataGridView)


Dim student = From studentrecords In dbcon.tblStudents _
Select studentrecords
register_student.dtgStudent.DataSource = student
End Sub
7. Code to show first record (ClassModule e.g. studentcodes)

Public Shared Sub ShowFirstRecord()


register_student.StudNO.Text = register_student.dtgStudent.Rows(0).Cells(0).Value
register_student.txtStudID.Text = register_student.dtgStudent.Rows(0).Cells(1).Value
register_student.txtName.Text = register_student.dtgStudent.Rows(0).Cells(2).Value
register_student.cboGender.Text = register_student.dtgStudent.Rows(0).Cells(3).Value
register_student.txtContact.Text = register_student.dtgStudent.Rows(0).Cells(4).Value
register_student.cboCourse.Text = register_student.dtgStudent.Rows(0).Cells(5).Value
register_student.cboMajor.Text = register_student.dtgStudent.Rows(0).Cells(6).Value
register_student.cboYear.Text = register_student.dtgStudent.Rows(0).Cells(7).Value
register_student.cboSection.Text = register_student.dtgStudent.Rows(0).Cells(8).Value
register_student.pb1.ImageLocation = Application.StartupPath & "\Image\" &
register_student.dtgStudent.Rows(0).Cells("StudentID").Value & ".jpg"
End Sub

8. Code to show record from grid to textboxes,combo

Public Shared Sub ShowRecFromGridToTextboxes()


If register_student.dtgStudent.Rows.Count <> 0 Then
register_student.StudNO.Text =
register_student.dtgStudent.CurrentRow.Cells(0).Value
register_student.txtStudID.Text =
register_student.dtgStudent.CurrentRow.Cells(1).Value
register_student.txtName.Text =
register_student.dtgStudent.CurrentRow.Cells(2).Value
register_student.cboGender.Text =
register_student.dtgStudent.CurrentRow.Cells(3).Value
register_student.txtContact.Text =
register_student.dtgStudent.CurrentRow.Cells(4).Value
register_student.cboCourse.Text =
register_student.dtgStudent.CurrentRow.Cells(5).Value
register_student.cboMajor.Text =
register_student.dtgStudent.CurrentRow.Cells(6).Value
register_student.cboYear.Text =
register_student.dtgStudent.CurrentRow.Cells(7).Value
register_student.cboSection.Text =
register_student.dtgStudent.CurrentRow.Cells(8).Value
register_student.pb1.ImageLocation = Application.StartupPath & "\Image\" &
register_student.dtgStudent.CurrentRow.Cells("StudentID").Value & ".jpg"
End If
End Sub
Republic of the Philippines
SULTAN KUDARAT STATE UNIVERSITY
IT Department
Kalamansig, Sultan Kudarat

9. Code to clear texboxe or other controls…

Public Shared Sub ClearStudents()


register_student.StudNO.Text = ""
register_student.txtStudID.Text = ""
register_student.txtName.Text = ""
register_student.cboGender.SelectedIndex = -1
register_student.txtContact.Text = ""
register_student.cboCourse.SelectedIndex = -1
register_student.cboMajor.SelectedIndex = -1
register_student.cboYear.SelectedIndex = -1
register_student.cboSection.SelectedIndex = -1
register_student.pb1.Text = Nothing
End Sub

10. Code for search student

Public Shared Sub SearchStudent(ByRef dgView As DataGridView, ByVal strID As String)


Dim studentname = From studentecords In dbcon.tblStudents _
Where studentecords.Name.Contains(strID) _
Select studentecords Order By studentecords.Name
dgview.DataSource = studentname
End Sub
Republic of the Philippines
SULTAN KUDARAT STATE UNIVERSITY
IT Department
Kalamansig, Sultan Kudarat

register_student(FORM CODES)
1. Declaration within the class
Public toggleswitch As Boolean ‘ for save command

2. Code for Add button


toggleswitch = True
If btnAdd.Text = "Add" Then
txtstudID.Focus()
studentcodes.Clear()
btnAdd.Text = "Cancel"
btnSave.Enabled = True
btnEdit.Enabled = False
btnDelete.Enabled = False
btnClose.Enabled = False
txtSearch.Enabled = False
Else
btnAdd.Text = "Add"
btnSave.Enabled = False
btnEdit.Enabled = True
btnDelete.Enabled = True
btnClose.Enabled = True
txtSearch.Enabled = True
Me.Close()
End If

11. Code for Edit button


toggleswitch = False
If btnEdit.Text = "Edit" Then
btnEdit.Text = "Cancel"
btnSave.Enabled = True
btnAdd.Enabled = False
btnDelete.Enabled = False
btnClose.Enabled = False
Else
btnEdit.Text = "Edit"
btnSave.Enabled = False
btnAdd.Enabled = True
btnDelete.Enabled = True
btnClose.Enabled = True
End If

12. Code for Save Button


If toggleswitch = True Then
studentcodes.AddNewStudent()
saveimage()
pb1.ImageLocation = Application.StartupPath & "\Image\Default.jpg"
txtSearch.Enabled = True
btnAdd.Text = "Add"
btnSave.Enabled = False
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDelete.Enabled = True
btnClose.Enabled = True
studentcodes.loadStudents(dtgStudent)
studentcodes.showfirststudent()
Else
studentcodes.UpdateStudent(studNo.Text)
saveimage()
pb1.ImageLocation = Application.StartupPath & "\Image\Default.jpg"
txtSearch.Enabled = True
btnEdit.Text = "Edit"
btnSave.Enabled = False
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDelete.Enabled = True
btnClose.Enabled = True
studentcodes.loadStudents(dtgStudent)
studentcodes.showfirststudent()
End If
Republic of the Philippines
SULTAN KUDARAT STATE UNIVERSITY
IT Department
Kalamansig, Sultan Kudarat

13. Code for Delete Button


If MsgBox("Are you sure you want to delete this record?", MsgBoxStyle.YesNo +
MsgBoxStyle.Critical, "Warning") = MsgBoxResult.Yes Then
studentcodes.DeleteStudent(studNo.Text)
studentcodes.loadStudents(dtgStudent)
Else
Exit Sub
End If

14. Code for Search( textbox changed property)


If txtSearch.Text <> "" Then
studentcodes.searchStudent(dtgStudent, txtSearch.Text)
Else
studentcodes.loadStudents(dtgStudent)
End If

15. Code for grid cellclick event


studentcodes.ShowFromGridtoText()

16. Code for saving image (note this code is a sub procedure/function)

Sub saveimage()
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Jpeg Image|*.jpg|Bitmap Image|*.bmp|Gif
Image|*.gif"
saveFileDialog1.FileName = Application.StartupPath & "\Image\" & txtstudID.Text &
".jpg"

If saveFileDialog1.FileName <> "" Then


' Saves the Image via a FileStream created by the OpenFile method.
Dim fs As System.IO.FileStream = CType _
(saveFileDialog1.OpenFile(), System.IO.FileStream)
pb1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
fs.Close()
End If
End Sub
17. Code for picture box

Dim fdlg As OpenFileDialog = New OpenFileDialog()


fdlg.Title = "Choose a Profile Photo"
fdlg.InitialDirectory = Application.StartupPath & "\Image\"
fdlg.Filter = "Picture
Files(*.jpg;*.jpeg;*.png;*.bmp;*.gif)|*.jpg;*.jpeg;*.png;*.bmp;*.gif"
fdlg.FilterIndex = 2
fdlg.RestoreDirectory = True
If fdlg.ShowDialog() = DialogResult.OK Then
pb1.ImageLocation = fdlg.FileName
End If
Republic of the Philippines
SULTAN KUDARAT STATE UNIVERSITY
IT Department
Kalamansig, Sultan Kudarat

Expected Components of the Project

1. Project Explorer

2. Table Structure as shown in DBML

“Blessed are those people, who persevere…”


-sirnan

Best of Luck!

You might also like