vb.net database programming with examples

54
1 Task-2 Question 1) Define and explain the terms. a) Tables A table in database is a data object used to store data. Tables have the following features: * Data is stored in a table with a structure of rows and columns. * Columns must be pre-defined with names, types and constrains. For example, a table called Address may have columns defined to store different elements of an address like, street number, city, country, postal code, etc. b) Fields Fields are the columns in databases. Each field can only hold one data type at any time. e.g. a field for a date will always, and only, hold a date. Fields can be either fixed or variable in length. They contain a piece of specific information from a record c) Records A record contains all the information about a single 'member' of a table . In a students table, each student's details (name, date of birth, contact details, and so on) will be contained in its own record. d) Index Index is a summary table which lets you quickly lookup the contents of any record in a table. Think of how you use an index to a book: as a quick jumping off point to finding full information about a subject. A database index works in a similar way. You can create an index on any field in a table. Example, you have a customer table which Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Upload: hanima4

Post on 26-Oct-2014

308 views

Category:

Documents


7 download

DESCRIPTION

This book is teaches vb.net database programming with examples. It includes a database lottery program,library management system etc.

TRANSCRIPT

Page 1: Vb.net Database Programming With Examples

1

Task-2

Question 1) Define and explain the terms.

a) Tables

A table in database is a data object used to store data. Tables have the following features:

* Data is stored in a table with a structure of rows and columns.* Columns must be pre-defined with names, types and constrains.

For example, a table called Address may have columns defined to store different elements of an address like, street number, city, country, postal code, etc.

b) Fields

Fields are the columns in databases. Each field can only hold one data type at any time. e.g. a field for a date will always, and only, hold a date. Fields can be either fixed or variable in length. They contain a piece of specific information from a record

c) Records

A record contains all the information about a single 'member' of a table. In a students table, each student's details (name, date of birth, contact details, and so on) will be contained in its own record.

d) Index

Index is a summary table which lets you quickly lookup the contents of any record in a table. Think of how you use an index to a book: as a quick jumping off point to finding full information about a subject. A database index works in a similar way. You can create an index on any field in a table. Example, you have a customer table which contains customer numbers, names, addresses and other details. You can make indexes based on any information, such as the customers' customer number, last name + first name (a composite index based on more than one field), or postal code. Then, when you're searching for a particular customer or group of customers, you can use the index to speed up the search. This increase in performance may not be noticeable in a table containing a hundred records; in a database of thousands of records it will be a blessing.

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 2: Vb.net Database Programming With Examples

2

e) Query

A query is an inquiry into the database using the SELECT statement. A query is used to extract data from the database in a readable format according to the user's request. For instance, if you have an employee table, you might issue a SQL statement that returns the employee who is paid the most. This request to the database for usable employee information is a typical query that can be performed in a relational database.

f) Record set

Recordset is a data structure that consists of a group of database records, and can either come from a base table or as the result of a query to the table.

The concept is common to a number of platforms, notably Microsoft's Data Access Objects (DAO) and ActiveX Data Objects (ADO). The Recordset object contains a Fields collection and a Properties collection. At any time, the Recordset object refers to only a single record within the set as the current record.

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 3: Vb.net Database Programming With Examples

3

2) Show the structure of ADO.NET connectivity in Visual Basic.NET with an example?

The database part of .NET, called ADO.NET, offers significant improvements in interoperability, ease of programming and maintenance, and performance. It is an evolutionary advance from Microsoft's earlier database technology, called simply ADO (for ActiveX Data Objects).

The process of making a connection is as follows

1) Create a connection string that contains all the needed information, such as the data source, used name, and password. For example:

Module Module1 Public conn As New SqlClient.SqlConnection Public Sub connect() conn.ConnectionString = "initial catalog=sms;data source=mhasan;user id=sa;password=sa;" End SubEnd Module

The line “Public conn As New SqlClient.SqlConnection” means create a new SQLserver connection.

Initial catalog is the database name

Data source is the name of the computer or server where the SQLServer is installed.

User id is the user name used to connect to the SQLServer

Password is the password used to connect to SQLServer.

Connections

There are two ADO.NET classes to create a connection to a database. The SqlConnection class is used for connections to Microsoft SQL Server databases (version 7 and later), and the OleDbConnection class is used for connections to databases that support the OLE DB technology (for example, Access, Oracle, SQL Server versions 6.5 and earlier, and third party SQL products).

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 4: Vb.net Database Programming With Examples

4

3) Design and implement a simple lottery Automation system with start button, stop button and label box. While the start button is pressed the mobile numbers stored in the table should be displayed in the label boxes and should change within a time span of 1 sec. On pressing the stop button, the winner name and address should be displayed to the user?

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Click event of “start” button

Me.Width = 356 Timer1.Start()

Click event of “stop” button

Timer1.Stop() GroupBox2.Visible = True Me.Width = 690 Label4.Text = dt.Rows(i - 1).Item(1) Label5.Text = dt.Rows(i - 1).Item(2)

Timertick event

connect() conn.Open() comm.CommandText = "select * from lottery" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) Label1.Text = dt.Rows(i).Item(0) i = i + 1 conn.Close()

Form load event

GroupBox2.Visible = False Me.Width = 356

Declarations for the program

Public Class Form1 Dim adp As New SqlClient.SqlDataAdapter Dim comm As New SqlClient.SqlCommand Dim dt As New DataTable Dim i As IntegerEnd class

Page 5: Vb.net Database Programming With Examples

5

Screenshots of the program while it is running

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 6: Vb.net Database Programming With Examples

6

4) Create a Visual Basic.NET Application for the given scenario. The database name IBS and the table name as courses and the fields as course ID, course Name, Duration and affiliations. While executing the application the records in the field course ID should be loaded to the combo boxes and on selecting a particular course id, the corresponding values like course Name, Duration and affiliations for that course should be displayed in the corresponding Text boxes.

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Form load event

TextBox1.Enabled = False TextBox2.Enabled = False TextBox3.Enabled = False dt.Clear() connect() conn.Open() comm.CommandText = "select course_id from courses" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) Dim i As Integer For i = 0 To dt.Rows.Count - 1 ComboBox1.Items.Add(dt.Rows(i).Item(0)) Next conn.Close()

Declarations for the program

Public Class Form1 Dim adp As New SqlClient.SqlDataAdapter Dim dt, dt2 As New DataTable Dim comm As New SqlClient.SqlCommandEnd class

Page 7: Vb.net Database Programming With Examples

7

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Module Module1Public conn As New SqlClient.SqlConnection

Public Sub connect() conn.ConnectionString="initial catalog=IBS;data source=MHASAN;user id=sa;password=sa;" End Sub

End Module

Click event of combobox (combobox text changed event)TextBox1.Enabled = True TextBox2.Enabled = True TextBox3.Enabled = True dt2.Clear() connect() conn.Open() comm.CommandText = "select * from courses where course_id='" & ComboBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt2) TextBox1.Text = dt2.Rows(0).Item(1) TextBox2.Text = dt2.Rows(0).Item(2) TextBox3.Text = dt2.Rows(0).Item(3) conn.Close()

Page 8: Vb.net Database Programming With Examples

8

5) Using Data Grid control, display all the contents in the table courses, when a Visual Basic.NET application is executed. On clicking a particular row or column that particular course details for which the user has selected should be displayed in the corresponding text boxes.

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Form Load event GroupBox1.Visible = False connect() conn.Open() comm.CommandText = "select * from courses" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close()

DataGridview cell click event

GroupBox1.Visible = True TextBox1.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(0) TextBox2.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(1) TextBox3.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(2) TextBox4.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(3)

Declaration for the program

Public Class Form1 Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTableEnd class

Codes for the module

Module Module1 Public conn As New SqlClient.SqlConnection Public Sub connect() conn.ConnectionString = "initial catalog=IBS;data source=MHASAN;user id=sa;password=sa;" End Sub

Page 9: Vb.net Database Programming With Examples

9

6) Create a table called enquiry with the fields, applicant name, course opted for, mobile number, date of enquiry and comments. Design a form in VB.NET with these fields and it should have option to add new enquiry, and search options like search by name of the applicant, date of enquiry and view all enquiry. Use menu edition, Data Grid, and MDI form to do the question?

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Click event of the “add” button

connect() conn.Open() comm.CommandText = "insert into enquiry values('" & TextBox1.Text & "','" & ComboBox1.Text & "','" & TextBox2.Text & "','" & Label6.Text & "','" & RichTextBox1.Text & "')" comm.CommandType = CommandType.Text comm.Connection = conn comm.ExecuteNonQuery() conn.Close() MsgBox("record added")

Form load event

Label6.Text = Format(Date.Today, "MM/dd/yyyy") connect() conn.Open() comm.CommandText = "select course_id from courses" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) Dim i As Integer For i = 0 To dt.Rows.Count - 1 ComboBox1.Items.Add(dt.Rows(i).Item(0)) Next conn.Close()

Click event of the “add new query” menustrip item Form1.Show()

Click event of the “Search” menustrip item Form2.Show()

Page 10: Vb.net Database Programming With Examples

10

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Clikc event of “Textbox1”

dt.Clear() connect() conn.Open() comm.CommandText = "select * from enquiry where applicant_name like '" & TextBox1.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close()

Closeup event of “datetime picker”

dt.Clear() connect() conn.Open() comm.CommandText = "select * from enquiry where date_of_enquiry='" & Format(DateTimePicker1.Value, "dd/MM/yyyy") & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt)if dt.rows.count=0 thenelse DataGridView1.DataSource = dtEnd if conn.Close()

TextBox2_TextChanged event

connect() conn.Open() comm.CommandText = "select * from enquiry where mobile_number like '" & TextBox2.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close()

Click event of the “view all enquiry” linked textbox

GroupBox5.Visible = True GroupBox2.Visible = False GroupBox3.Visible = False GroupBox4.Visible = False dt.Clear() connect() conn.Open() comm.CommandText = "select * from enquiry" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close()

Page 11: Vb.net Database Programming With Examples

11

Public Class Form1 Public comm As New SqlClient.SqlCommand Public adp As New SqlClient.SqlDataAdapter Dim dt As New DataTableEnd class

Module Module1 Public conn As New SqlClient.SqlConnection Public Sub connect() conn.ConnectionString = "initial catalog=IBS;data source=MHASAN;user id=sa;password=sa;" End SubEnd Module

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 12: Vb.net Database Programming With Examples

12

7) Follow the procedure as said in question 6 and on displaying the details in the corresponding text boxes, the Course ID text box should be disabled. On making the necessary editing, and when the user presses the update button, the newly updated records from the form should be updated to the table as well as Grid values / details should be updated automatically?

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Form load event

GroupBox1.Enabled = False gridvalues()

“Gridvalues” function

dt.Clear() connect() conn.Open() comm.CommandText = "select * from enquiry" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close()

Cell click event of the “Datagrid”

GroupBox1.Enabled = True ComboBox1.Enabled = False TextBox1.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(0) ComboBox1.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(1) TextBox2.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(2) TextBox3.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(3) RichTextBox1.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(4)

Page 13: Vb.net Database Programming With Examples

13

Public Class Form1 Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt, dt1, dt2 As New DataTableEnd class

Module Module1 Public conn As New SqlClient.SqlConnection Public Sub connect() conn.ConnectionString = "initial catalog=IBS;data source=MHASAN;user id=sa;password=sa;" End SubEnd Module

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Cell click event of “update” button

connect() conn.Open() comm.CommandText = "update enquiry set applicant_name='" & TextBox1.Text & "',mobile_number='" & TextBox2.Text & "',date_of_enquiry='" & TextBox3.Text & "',comments='" & RichTextBox1.Text & "' where course_opted='" & ComboBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) conn.Close() MsgBox("record updated") TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" ComboBox1.Text = "" RichTextBox1.Text = "" gridvalues() adp.Fill(dt) conn.Close()

Page 14: Vb.net Database Programming With Examples

14

8) Follow the procedure as said in question 6 and on displaying the details in the corresponding text boxes, the Course ID textbox should be disabled. Create a button with the caption “Delete” and on pressing the “Delete” button that particular record should be deleted from the table. As well as the Grid values / details should be updated automatically?

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Form Load event

GroupBox1.Enabled = False gridvalues()

“gridvalues” Function

dt.Clear() connect() conn.Open() comm.CommandText = "select * from enquiry" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close()

Cell click event of the datagrid

ComboBox1.Enabled = False GroupBox1.Enabled = True TextBox1.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(0) ComboBox1.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(1) TextBox2.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(2) TextBox3.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(3) RichTextBox1.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(4)

Page 15: Vb.net Database Programming With Examples

15

Public Class Form1 Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTableEnd class

Module Module1 Public conn As New SqlClient.SqlConnection Public Sub connect() conn.ConnectionString = "initial catalog=IBS;data source=MHASAN;user id=sa;password=sa;" End SubEnd Module

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Click event of the “delete” button

dt.Clear() connect() conn.Open() comm.CommandText = "delete enquiry where course_opted='" & ComboBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) conn.Close() MsgBox("record deleted") gridvalues() TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" ComboBox1.Text = "" RichTextBox1.Text = ""

Page 16: Vb.net Database Programming With Examples

16

9) Design a simple application using VB.NET for library Management where the should be options for

a) Adding books

b) Searching books by

i) Name

II) ID

III) Publisher

IV) Author

c) Edit book details

d) Delete a book

e) Issues

f) Returns

g) Members

i) add member

ii) Search member

1) by Name

2) By ID

III) delete Member

IV) Edit member Details

While member makes a book request the system should check whether the member has already borrowed the maximum book or not. If not the system should check whether the book is available in the stock. If so issues have to be recorded. While a member returns a book the system should check whether the due_date has exceeded or not. If so the corresponding fine has to be calculated. For each day exceeded the fine should be 50 laaree. While adding a member along with the details, add the photo of the member to the database using the image tool.

Generate a Report for the Fine collected, and member details using crystal report. The should be functions like

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 17: Vb.net Database Programming With Examples

17

1) View monthly fine amount collected.

2) View fine amount collected for a particular date/day.

3) View a particular member detail by giving the member id.

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Login form

Click event of the “login” button

If TextBox1.Text = "" Then MsgBox("Please fill the user name field") ElseIf TextBox2.Text = "" Then MsgBox("Please fill the password field") Else dt.Clear() fname = TextBox1.Text connect() conn.Open() comm.CommandText = "select * from login where user_name='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) If dt.Rows.Count = 0 Then MsgBox("Invalid user name") Else fpassword = TextBox2.Text dpassword = dt.Rows(0).Item("user_password") If fpassword = dpassword Then MsgBox("Login successful") MDIParent1.Show() flevel = dt.Rows(0).Item("user_level") Me.Hide() If (flevel = "user") Then MDIParent1.LoginToolStripMenuItem.Enabled = False End If

Else MsgBox("Invalid password") End If End If conn.Close() End If

Clikc event of the “Cancel” button

TextBox1.Text = ""TextBox2.Text = ""

Declarations for the “login” form

Public Class login Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTable Public fname, flevel, fpassword, dname, dpassword As StringEnd class

Module codes

Module Module1 Public conn As New SqlClient.SqlConnection Public buttontext As String Public Sub connect() conn.ConnectionString = "initial catalog=library;data source=MHASAN;user id=sa;password=sa;" End Sub Public Sub edite() buttontext = "Edit" End Sub Public Sub del() buttontext = "Delete" End SubEnd Module

Page 18: Vb.net Database Programming With Examples

18

Imports System.Windows.Forms

Public Class MDIParent1

Private Sub AddToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToolStripMenuItem.Click addbook.Show() End Sub

Private Sub SearchBooksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchBooksToolStripMenuItem.Click search.Show() End Sub

Private Sub EditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem.Click edite() Form1.Show()

End Sub

Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click del() Form1.Show() End Sub

Private Sub SearchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchToolStripMenuItem.Click member_search.Show() End Sub

Private Sub AddToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToolStripMenuItem1.Click

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Mdiparent form

Page 19: Vb.net Database Programming With Examples

19

member_add.Show() End Sub

Private Sub DeleteToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem1.Click del() member_edit_delete.Show() End Sub

Private Sub EditToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem1.Click edite() member_edit_delete.Show() End Sub

Private Sub IssuesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IssuesToolStripMenuItem.Click issues.Show() End Sub

Private Sub ReturnsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReturnsToolStripMenuItem.Click returns.Show() End Sub

Private Sub MDIParent1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ToolStripStatusLabel1.Text = "Date : " & Format(Date.Now, "MM/dd/yyyy") ToolStripStatusLabel2.Text = "User Name :" & login.fname End Sub

Private Sub AddToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToolStripMenuItem2.Click login_add.Show() End Sub

Private Sub EditToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem2.Click login_delete_edit.Show() login_delete_edit.Button1.Text = "edit" End Sub

Private Sub DeleteToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem2.Click login_delete_edit.Show() login_delete_edit.Button1.Text = "delete" End Sub

Private Sub MonthlyFineToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonthlyFineToolStripMenuItem.Click monthly_fine.Show() End Sub

Private Sub FineForADayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FineForADayToolStripMenuItem.Click report_day.Show() End Sub

Private Sub MemberToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MemberToolStripMenuItem.Click report_member_id.Show() End SubEnd Class

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 20: Vb.net Database Programming With Examples

20

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Login table details

Only “administrator” user_level users can change the login details for the system users.

LoginToolStripMenuItem is disabled for system users whose user_level is “user”.

If the system user is an “administrator” then it is enabled.

Page 21: Vb.net Database Programming With Examples

21

addbook form

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Form load event (generating auto increment book number)TextBox1.Enabled = False connect() conn.Open() comm.CommandText = "select * from book" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) Dim id, bid As String Dim newid, rowcount As Integer rowcount = dt.Rows.Count

If rowcount = 0 Then TextBox1.Text = "B001" Else id = dt.Rows(rowcount - 1).Item(0) newid = id.Substring(3, 1) + 1 bid = id.Substring(0, 3) TextBox1.Text = bid & newid End If conn.Close()

Click event of the “add book” button

If TextBox1.Text = "" Then MsgBox("Please fill book id field") ElseIf TextBox2.Text = "" Then MsgBox("Please enter a book name") ElseIf TextBox3.Text = "" Then MsgBox("Please enter a publisher") ElseIf TextBox4.Text = "" Then MsgBox("Please enter an author") ElseIf TextBox5.Text = "" Then MsgBox("Please enter book cost") ElseIf TextBox6.Text = "" Then MsgBox("Please enter an isbn number") Else connect() conn.Open() comm.CommandText = "insert into book values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "'," & TextBox5.Text & ",'" & TextBox6.Text & "')" comm.CommandType = CommandType.Text comm.Connection = conn comm.ExecuteNonQuery() conn.Close() MsgBox("Record added") TextBox1.Text = " " TextBox2.Text = " " TextBox3.Text = " " TextBox4.Text = " " TextBox5.Text = " " TextBox6.Text = " " End If

addbook form

Declarations for the “addbook” form

Public Class addbook

Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTabl

End class

Page 22: Vb.net Database Programming With Examples

22

Form 1 (Edit and delete books)

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Form1 Design View (edit/delete books)Form load event

GroupBox1.Visible = False gridvalues()

Cell click event of “DataGridView1”

GroupBox1.Visible = True TextBox1.Enabled = False TextBox1.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(0) TextBox2.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(1) TextBox3.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(2) TextBox4.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(3) TextBox5.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(4) TextBox6.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(5) Button1.Text = buttontext

gridvalues function

Public Sub gridvalues() dt.Clear() connect() conn.Open() comm.CommandText = "select * from book" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close() End Sub

Page 23: Vb.net Database Programming With Examples

23

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Click event of the button ( if the Mdiparents book edit is pressed then button text become “edit” . if the Mdiparents book delete is pressed then button text become “delete”)

If Button1.Text = "Edit" Then connect() conn.Open() comm.CommandText = "update book set book_name='" & TextBox2.Text & "',publisher='" & TextBox3.Text & "',author='" & TextBox4.Text & "',cost=" & TextBox5.Text & ",isbn='" & TextBox6.Text & "' where book_id='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) conn.Close() MsgBox("book details updated") gridvalues() ElseIf Button1.Text = "Delete" Then connect() conn.Open() comm.CommandText = "delete from book where book_id = '" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) conn.Close() MsgBox("book deleted") gridvalues() End If

TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox5.Text = "" TextBox6.Text = ""

Page 24: Vb.net Database Programming With Examples

24

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Declarations for form1

Public Class Form1

Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTable

End class

Page 25: Vb.net Database Programming With Examples

25

Search form for book

Public Class search Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTabl

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load GroupBox2.Visible = False GroupBox3.Visible = False GroupBox4.Visible = False GroupBox5.Visible = False GroupBox6.Visible = False GroupBox7.Visible = False GroupBox8.Visible = False GroupBox9.Visible = False End Sub

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked GroupBox2.Visible = True GroupBox3.Visible = False GroupBox4.Visible = False GroupBox5.Visible = False GroupBox6.Visible = False GroupBox7.Visible = False GroupBox8.Visible = False GroupBox9.Visible = True dt.Clear() End Sub

Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked dt.Clear() GroupBox2.Visible = False GroupBox3.Visible = True GroupBox4.Visible = False GroupBox5.Visible = False

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Click event of textbox (textchange ivent)

dt.Clear() connect() conn.Open() comm.CommandText = "select * from book where isbn like '" & TextBox6.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close()

Page 26: Vb.net Database Programming With Examples

26

GroupBox6.Visible = False GroupBox7.Visible = False GroupBox8.Visible = False GroupBox9.Visible = True End Sub

Private Sub LinkLabel3_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel3.LinkClicked dt.Clear() GroupBox2.Visible = False GroupBox3.Visible = False GroupBox4.Visible = True GroupBox5.Visible = False GroupBox6.Visible = False GroupBox7.Visible = False GroupBox8.Visible = False GroupBox9.Visible = True End Sub

Private Sub LinkLabel4_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel4.LinkClicked dt.Clear() GroupBox2.Visible = False GroupBox3.Visible = False GroupBox4.Visible = False GroupBox5.Visible = True GroupBox6.Visible = False GroupBox7.Visible = False GroupBox8.Visible = False GroupBox9.Visible = True End Sub

Private Sub LinkLabel5_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel5.LinkClicked dt.Clear() GroupBox2.Visible = False GroupBox3.Visible = False GroupBox4.Visible = False GroupBox5.Visible = False GroupBox6.Visible = True GroupBox7.Visible = False GroupBox8.Visible = False GroupBox9.Visible = True End Sub

Private Sub LinkLabel7_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) GroupBox2.Visible = False GroupBox3.Visible = False GroupBox4.Visible = False GroupBox5.Visible = False GroupBox6.Visible = False GroupBox7.Visible = False GroupBox8.Visible = True GroupBox9.Visible = True End Sub

Private Sub LinkLabel6_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) GroupBox2.Visible = False GroupBox3.Visible = False GroupBox4.Visible = False GroupBox5.Visible = False GroupBox6.Visible = False GroupBox7.Visible = True GroupBox8.Visible = False GroupBox9.Visible = False End Sub

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 27: Vb.net Database Programming With Examples

27

Private Sub LinkLabel6_LinkClicked_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel6.LinkClicked dt.Clear() GroupBox2.Visible = False GroupBox3.Visible = False GroupBox4.Visible = False GroupBox5.Visible = False GroupBox6.Visible = False GroupBox7.Visible = True GroupBox8.Visible = False GroupBox9.Visible = True End Sub

Private Sub LinkLabel7_LinkClicked_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel7.LinkClicked dt.Clear() GroupBox2.Visible = False GroupBox3.Visible = False GroupBox4.Visible = False GroupBox5.Visible = False GroupBox6.Visible = False GroupBox7.Visible = False GroupBox8.Visible = True GroupBox9.Visible = True ComboBox1.Items.Add("Yes") ComboBox1.Items.Add("No") End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from book where book_id like '" & TextBox1.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close() End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from book where book_name like '" & TextBox2.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close() End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from book where publisher like '" & TextBox3.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close() End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged dt.Clear() connect()

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 28: Vb.net Database Programming With Examples

28

conn.Open() comm.CommandText = "select * from book where author like '" & TextBox4.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close() End Sub

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from book where cost like '" & TextBox5.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close() End Sub

Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from book where isbn like '" & TextBox6.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close() End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from book where available like '" & ComboBox1.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close() End SubEnd Class

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 29: Vb.net Database Programming With Examples

29

member_add form (To add members)

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Click event of “browse” button

OpenFileDialog1.Filter = "picture files(*.jpg)|*.jpg" If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName) pfilename = OpenFileDialog1.FileName End If

Click event of the “Add” button

If TextBox1.Text = "" Then MsgBox("Please enter an id") ElseIf TextBox2.Text = "" Then MsgBox("Please enter a name") ElseIf TextBox3.Text = "" Then MsgBox("Please enter a permanent address") ElseIf TextBox4.Text = "" Then MsgBox("please enter present address") ElseIf TextBox5.Text = "" Then MsgBox("Please enter national id number") ElseIf TextBox6.Text = "" Then MsgBox("please enter contact number") Else

connect() conn.Open() comm.CommandText = "insert into member values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "'," & TextBox6.Text & ",'" & pfilename & "')" comm.CommandType = CommandType.Text comm.Connection = conn comm.ExecuteNonQuery() conn.Close() MsgBox("member added") End If TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox5.Text = "" TextBox6.Text = ""

Form Load event (Auto incrementing member id)

TextBox1.Enabled = False connect() conn.Open() comm.CommandText = "select * from member" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) Dim id, textid As String Dim newid, rcount As Integer rcount = dt.Rows.Count If rcount = 0 Then TextBox1.Text = "M001" Else id = dt.Rows(rcount - 1).Item(0) newid = id.Substring(3, 1) + 1 textid = id.Substring(0, 3) TextBox1.Text = textid & newid End If conn.Close()

Page 30: Vb.net Database Programming With Examples

30

Member_search form (To search members)

Public Class member_search Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTable Private Sub member_search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load GroupBox9.Visible = False End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged dt.Clear() connect()

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Click event of “textbox1” (TextBox1_TextChanged)

dt.Clear() connect() conn.Open() comm.CommandText = "select * from member where member_id like '" & TextBox1.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) GroupBox9.Visible = True If dt.Rows.Count = 0 Then Else DataGridView1.DataSource = dt Label14.Text = dt.Rows(0).Item(0) Label15.Text = dt.Rows(0).Item(1) Label16.Text = dt.Rows(0).Item(2) Label17.Text = dt.Rows(0).Item(3) Label18.Text = dt.Rows(0).Item(4) Label19.Text = dt.Rows(0).Item(5) PictureBox1.Image = Image.FromFile(dt.Rows(0).Item(6)) End If

Cell click of DataGrid (DataGridView1_CellClick)

Label14.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(0) Label15.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(1) Label16.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(2) Label17.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(3) Label18.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(4) Label19.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(5) PictureBox1.Image = Image.FromFile(dt.Rows(DataGridView1.CurrentRow.Index).Item(6)) End Sub

Page 31: Vb.net Database Programming With Examples

31

conn.Open() comm.CommandText = "select * from member where member_id like '" & TextBox1.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) GroupBox9.Visible = True If dt.Rows.Count = 0 Then Else DataGridView1.DataSource = dt Label14.Text = dt.Rows(0).Item(0) Label15.Text = dt.Rows(0).Item(1) Label16.Text = dt.Rows(0).Item(2) Label17.Text = dt.Rows(0).Item(3) Label18.Text = dt.Rows(0).Item(4) Label19.Text = dt.Rows(0).Item(5) PictureBox1.Image = Image.FromFile(dt.Rows(0).Item(6)) End If

conn.Close() End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from member where name like '" & TextBox2.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt If dt.Rows.Count = 0 Then Else DataGridView1.DataSource = dt GroupBox9.Visible = True Label14.Text = dt.Rows(0).Item(0) Label15.Text = dt.Rows(0).Item(1) Label16.Text = dt.Rows(0).Item(2) Label17.Text = dt.Rows(0).Item(3) Label18.Text = dt.Rows(0).Item(4) Label19.Text = dt.Rows(0).Item(5) PictureBox1.Image = Image.FromFile(dt.Rows(0).Item(6)) End If conn.Close() End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from member where permanent_address like '" & TextBox3.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt If dt.Rows.Count = 0 Then Else DataGridView1.DataSource = dt GroupBox9.Visible = True Label14.Text = dt.Rows(0).Item(0) Label15.Text = dt.Rows(0).Item(1) Label16.Text = dt.Rows(0).Item(2) Label17.Text = dt.Rows(0).Item(3) Label18.Text = dt.Rows(0).Item(4) Label19.Text = dt.Rows(0).Item(5) PictureBox1.Image = Image.FromFile(dt.Rows(0).Item(6)) End If

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 32: Vb.net Database Programming With Examples

32

conn.Close() End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from member where present_address like '" & TextBox4.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt If dt.Rows.Count = 0 Then Else DataGridView1.DataSource = dt GroupBox9.Visible = True Label14.Text = dt.Rows(0).Item(0) Label15.Text = dt.Rows(0).Item(1) Label16.Text = dt.Rows(0).Item(2) Label17.Text = dt.Rows(0).Item(3) Label18.Text = dt.Rows(0).Item(4) Label19.Text = dt.Rows(0).Item(5) PictureBox1.Image = Image.FromFile(dt.Rows(0).Item(6)) End If conn.Close() End Sub

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from member where national_id like '" & TextBox5.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) If dt.Rows.Count = 0 Then Else DataGridView1.DataSource = dt GroupBox9.Visible = True Label14.Text = dt.Rows(0).Item(0) Label15.Text = dt.Rows(0).Item(1) Label16.Text = dt.Rows(0).Item(2) Label17.Text = dt.Rows(0).Item(3) Label18.Text = dt.Rows(0).Item(4) Label19.Text = dt.Rows(0).Item(5) PictureBox1.Image = Image.FromFile(dt.Rows(0).Item(6)) End If conn.Close() End Sub

Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged dt.Clear() connect() conn.Open() comm.CommandText = "select * from member where contact_number like '" & TextBox6.Text & "%" & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) If dt.Rows.Count = 0 Then Else DataGridView1.DataSource = dt GroupBox9.Visible = True Label14.Text = dt.Rows(0).Item(0) Label15.Text = dt.Rows(0).Item(1) Label16.Text = dt.Rows(0).Item(2) Label17.Text = dt.Rows(0).Item(3) Label18.Text = dt.Rows(0).Item(4)

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 33: Vb.net Database Programming With Examples

33

Label19.Text = dt.Rows(0).Item(5) PictureBox1.Image = Image.FromFile(dt.Rows(0).Item(6)) End If conn.Close() End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click GroupBox9.Hide() End Sub

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick Label14.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(0) Label15.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(1) Label16.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(2) Label17.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(3) Label18.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(4) Label19.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(5) PictureBox1.Image = Image.FromFile(dt.Rows(DataGridView1.CurrentRow.Index).Item(6)) End Sub

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Page 34: Vb.net Database Programming With Examples

34

mebmer_edit_delete form (to edit and delete members)

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Form load event

gridvalues()

Gridvalues function codes

dt.Clear() connect() conn.Open() comm.CommandText = "select * from member" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close()

Click event of the button

If Button1.Text = "Delete" Then dt.Clear() connect() conn.Open() comm.CommandText = "delete from member where member_id='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) MsgBox("member details deleted") conn.Close() ElseIf Button1.Text = "Edit" Then dt.Clear() connect() comm.CommandText = "update member set name='" & TextBox2.Text & "',permanent_address='" & TextBox3.Text & "',present_address='" & TextBox4.Text & "',national_id='" & TextBox5.Text & "',contact_number=" & TextBox6.Text & " where member_id='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) MsgBox("member details updated") conn.Close() End If gridvalues()

Cell click event of DataGridView1TextBox1.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(0) TextBox2.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(1) TextBox3.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(2) TextBox4.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(3) TextBox5.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(4) TextBox6.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(5) TextBox1.Enabled = False Button1.Text = buttontext

Page 35: Vb.net Database Programming With Examples

35

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Declaration for the member_edit_delete form

Public Class member_edit_delete Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTableEnd class

Page 36: Vb.net Database Programming With Examples

36

issues form (to record the issued book details)

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

click event of the “member or not” button

dt.Clear() connect() conn.Open() comm.CommandText = "select * from member where member_id='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) If dt.Rows.Count = 0 Then MsgBox("not a member") TextBox1.Text = "" GoTo c Else MsgBox("member") TextBox2.Enabled = True End If conn.Close() dt2.Clear() connect() conn.Open() comm.CommandText = "select * from issue where member_id='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt2) If (dt2.Rows.Count = 0) Then TextBox1.Enabled = True MsgBox("2 books can be borrowed") Label5.Text = Format(Date.Now, "MM/dd/yyyy") Label6.Text = Format(DateAdd(DateInterval.Day, 14, Date.Now), "MM/dd/yyyy") Button2.Enabled = True ElseIf (dt2.Rows.Count = 1) Then MsgBox("1 book can be borrowed") TextBox2.Enabled = True Label5.Text = Format(Date.Now, "MM/dd/yyyy") Label6.Text = Format(DateAdd(DateInterval.Day, 14, Date.Now), "MM/dd/yyyy") Button2.Enabled = True Else MsgBox("Can't borrow more than 2 books") TextBox2.Enabled = False Button1.Enabled = False End Ifc: conn.Close()

form load event TextBox2.Enabled = False Button1.Enabled = False Button2.Enabled = False

Declaration for the issues form

Public Class issues Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt, dt2, dt3 As New DataTableEnd class

Page 37: Vb.net Database Programming With Examples

37

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Click event of the “book available or not” button

dt3.Clear() connect() conn.Open() comm.CommandText = "select * from issue where book_id='" & TextBox2.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt3) If dt3.Rows.Count = 1 Then MsgBox("book is not availble") TextBox2.Text = "" Else MsgBox("Book Available") TextBox2.Enabled = True Button1.Enabled = True Label8.Text = login.fname End If conn.Close()Click event of the “Add” button

dt.Clear() connect() conn.Open() comm.CommandText = "insert into issue values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & Label5.Text & "','" & Label6.Text & "','" & Label8.Text & "')" comm.CommandType = CommandType.Text comm.Connection = conn comm.ExecuteNonQuery() conn.Close()

MsgBox("book issued") TextBox1.Text = "" TextBox2.Text = "" Label5.Text = "" Label6.Text = "" Label8.Text = "" TextBox2.Enabled = False Button1.Enabled = False Button2.Enabled = False

Page 38: Vb.net Database Programming With Examples

38

returns form ( to keep records of the fines collected and to return books )

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

click event of “search” button dt.Clear() connect() conn.Open() comm.CommandText = "select * from issue where book_id='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) Label7.Text = dt.Rows(0).Item(0) Label8.Text = dt.Rows(0).Item(3) return_date = Label8.Text Label9.Text = Format(Date.Now, "MM/dd/yyyy") returned_date = Label9.Text dayselapsed = DateDiff(DateInterval.Day, return_date, returned_date, FirstDayOfWeek.Sunday) If dayselapsed > 14 Then fine = 0.5 * dayselapsed Label11.Text = Format(fine, "#.00") Label10.Text = dayselapsed Button1.Visible = True conn.Close() Else Label10.Text = "0" Label11.Text = "0" dt.Clear() conn.Close() connect() conn.Open() comm.CommandText = "delete from issue where book_id='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) conn.Close() MsgBox("book returned") conn.Close() End If

Page 39: Vb.net Database Programming With Examples

39

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

dt.Clear() connect() conn.Open() comm.CommandText = "insert into fine values('" & TextBox1.Text & "','" & Label7.Text & "', '" & Label8.Text & "','" & Label9.Text & "'," & Label10.Text & "," & Label11.Text & ")" comm.CommandType = CommandType.Text comm.Connection = conn comm.ExecuteNonQuery() conn.Close() MsgBox("fine details added") conn.Close()

dt.Clear() connect() conn.Open() comm.CommandText = "delete from issue where book_id='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) conn.Close() MsgBox("book returned") conn.Close() TextBox1.Text = "" Label7.Text = "" Label8.Text = "" Label9.Text = "" Label10.Text = "" Label11.Text = ""

form load event

Button1.Visible = False

Declarations for the form Public Class returns Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTable Dim dayselapsed As Integer Dim return_date, returned_date As Date Dim fine As DoubleEnd class

Page 40: Vb.net Database Programming With Examples

40

login_add form (to add system users)

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Click event of the “Add” button

If TextBox1.Text = "" Then MsgBox("Please fill user name") ElseIf TextBox2.Text = "" Then MsgBox("Please fill password field") ElseIf ComboBox1.Text = "" Then MsgBox("Please select user level") Else connect() conn.Open() comm.CommandText = "insert into login values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & ComboBox1.Text & "')" comm.CommandType = CommandType.Text comm.Connection = conn comm.ExecuteNonQuery() MsgBox("user added") conn.Close()

TextBox1.Text = "" TextBox2.Text = "" ComboBox1.Text = "" End If

Form Load event

ComboBox1.Items.Add("administrator") ComboBox1.Items.Add("user")

Page 41: Vb.net Database Programming With Examples

41

login_delete_edit form (to delete and edit system users)

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Form load event

TextBox1.Enabled = False TextBox2.Enabled = False ComboBox1.Enabled = False Button1.Enabled = False gridvalues() ComboBox1.Items.Add("user") ComboBox1.Items.Add("administrator")

function for gridvalues

Public Sub gridvalues() dt.Clear() connect() conn.Open() comm.CommandText = "select * from login" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) DataGridView1.DataSource = dt conn.Close() End Sub

Click event of the “edit” or “delete” button

If (Button1.Text = "edit") Then dt.Clear() connect() conn.Open() comm.CommandText = "update login set user_password='" & TextBox2.Text & "', user_level='" & ComboBox1.Text & "' where user_name='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) conn.Close() MsgBox("login details edited") gridvalues() ElseIf (Button1.Text = "delete") Then dt.Clear() connect() conn.Open() comm.CommandText = "delete from login where user_name='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) conn.Close() MsgBox("login details deleted") gridvalues() End If

Cell click event of DataGridView1(DataGridView1_CellClick)

TextBox1.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(0) TextBox2.Text = dt.Rows(DataGridView1.CurrentRow.Index).Item(1) Button1.Enabled = True TextBox1.Enabled = False TextBox2.Enabled = True ComboBox1.Enabled = True

Page 42: Vb.net Database Programming With Examples

42

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Declarations for the “login_delete_edit” form

Public Class login_delete_edit Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTableEnd Class

Page 43: Vb.net Database Programming With Examples

43

monthly_fine form (to generate Report for monthly fine collected)

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Click event of the “fine” button

dt.Clear() connect() conn.Open() comm.CommandText = "select * from fine where returned_date>='" & startdate & "' and returned_date<='" & enddate & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) Dim rpt As New CrystalReport1 rpt.SetDataSource(dt) CrystalReportViewer1.ReportSource = rpt conn.Close()

DateTimePicker1_CloseUp Event

startdate = Format(DateTimePicker1.Value, "MM/dd/yyyy")

DateTimePicke2_CloseUp Event

enddate = Format(DateTimePicker2.Value, "MM/dd/yyyy")

Declarations for the monthly_fine form

Public Class monthly_fine Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTable Dim startdate, enddate As StringEnd Class

Page 44: Vb.net Database Programming With Examples

44

report_day form (to generate fine collected amount for a particular day)

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Declarations for the report_day

Public Class report_day Dim ddate As String Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTableEnd Class

Declarations for the report_day

ddate = Format(DateTimePicker1.Value, "MM/dd/yyyy")

Click event of the “fine/day” button

connect() conn.Open() comm.CommandText = "select * from fine where returned_date='" & ddate & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) Dim rpt2 As New CrystalReport2 rpt2.SetDataSource(dt) CrystalReportViewer1.ReportSource = rpt2 conn.Close()

Page 45: Vb.net Database Programming With Examples

45

report_member_id form (to generate report for member details by searching for a given member id)

Module Title: Advanced Visual Programming (Vb.Net) Assignment Title: Database Programming Student Id: MIDSE 552 Student Name: Mohamed Hassan

Declaration for the “report_member_id” form

Public Class report_member_id Dim comm As New SqlClient.SqlCommand Dim adp As New SqlClient.SqlDataAdapter Dim dt As New DataTableEnd Class

Click event of “Search” button

dt.Clear() connect() conn.Open() comm.CommandText = "select * from member where member_id='" & TextBox1.Text & "'" comm.CommandType = CommandType.Text comm.Connection = conn adp.SelectCommand = comm adp.Fill(dt) Dim rpt3 As New CrystalReport3 rpt3.SetDataSource(dt) CrystalReportViewer1.ReportSource = rpt3 conn.Close()