Skip to content

Instantly share code, notes, and snippets.

@bogere
Created February 7, 2019 15:35
Show Gist options
  • Save bogere/5d9c32f808a54b5e3db9eb2c415b919c to your computer and use it in GitHub Desktop.
Save bogere/5d9c32f808a54b5e3db9eb2c415b919c to your computer and use it in GitHub Desktop.
Clinic master
Imports System.Data
Imports System.Data.SqlClient ' Reference to the ADO.NET namespace(interact with the database)
Public Class Form1
'Create ADO.NET objects.
Private myConn As SqlConnection = New SqlConnection("Data Source=GOLDSOFTX;Initial Catalog=TheBox;Integrated Security=True")
Private myCmd As SqlCommand
Private myReader As SqlDataReader
Private results As String
' Adding the students records into the database
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Add.Click
'SQL query
Dim insertQuery As String = "insert into students(name, college, age) values(@name, @college, @age)"
'represents the Transact-SQL statement performed against SQL database
Dim cmd As New SqlCommand(insertQuery, myConn)
'time to sanitize teh submitted input values.
cmd.Parameters.AddWithValue("@name", nameTextBox.Text)
cmd.Parameters.AddWithValue("@college", collegeTextBox.Text)
cmd.Parameters.AddWithValue("@age", ageTextBox.Text)
'open the database
myConn.Open()
'Execute the commands
cmd.ExecuteNonQuery()
myConn.Close()
'show the successful text
MessageBox.Show("Student added successful")
clearInput()
End Sub
Private Sub clearInput()
'clear all the input boxes after adding records
nameTextBox.Clear()
collegeTextBox.Clear()
ageTextBox.Clear()
End Sub
Private Sub nameTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles nameTextBox.TextChanged
End Sub
Private Sub TextBox3_TextChanged(sender As System.Object, e As System.EventArgs) Handles ageTextBox.TextChanged
End Sub
'update the student records in the database
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Update.Click
Dim updateQuery As String = "update students set name=@name, college=@college, age=@age where id=@id"
Dim cmd As New SqlCommand(updateQuery, myConn)
'time to sanitize teh submitted input values.
cmd.Parameters.AddWithValue("@id", "selectedId")
cmd.Parameters.AddWithValue("@name", nameTextBox.Text)
cmd.Parameters.AddWithValue("@college", collegeTextBox.Text)
cmd.Parameters.AddWithValue("@age", ageTextBox.Text)
myConn.Open()
cmd.ExecuteNonQuery() 'in charge of executing the SQL query
MessageBox.Show("Record updated successful")
End Sub
Private Sub Button1_Click_2(sender As System.Object, e As System.EventArgs) Handles Delete.Click
Dim deleteQuery As String = "delete students where id =@id"
Dim cmd As New SqlCommand(deleteQuery, myConn)
'time to sanitize teh submitted input values.
cmd.Parameters.AddWithValue("@id", "selectedId")
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'TheBoxDataSet.students' table. You can move, or remove it, as needed.
'Me.StudentsTableAdapter.Fill(Me.TheBoxDataSet.students)
'load the database records when the this form loads.
Dim readQuery As String = "select * from students"
Dim cmd As New SqlCommand(readQuery, myConn) 'issue SQL query commands
'dealing with Data Adapter.
Dim da As New SqlDataAdapter(cmd)
Dim studentTable As New DataTable
da.Fill(studentTable) ' Adds rows to the SQL data set
If studentTable.Rows.Count > 0 Then
'MessageBox.Show("I can see my grid table ")
DataGridView1.DataSource = studentTable
'DataGridView1.DataBind()
End If
End Sub
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment