Programming for Everybody: January 2023

VB.net - load data from SQL database format datagridview and use statement with

 


Contents๐ŸŽž
Load data from SQL database 00:56
Delete the last blank row and Prevent user to add rows 03:59
Fill the width of the available display area in datagridview 04:35
Change font column headers (Type - size - Style) 05:25
Change Colors column headers (Backcolor - Forecolor) 06:40
Change alignment column headers 08:51
Change font columns (Type - size) 09:47
Change alignment columns 10:46
With...End With Statement 11:35


๐ŸŽ๐ŸŽFull course VB.net with MySQL database(Insert, Update, Delete, and Search)๐ŸŽ‰๐ŸŽ‰ with code




https://youtube.com/clip/UgkxaESVafvSL8Sjake6z1v2gUA9vA1Xe-gk

Code Video
Imports MySql.Data.MySqlClient
Public Class Form1
    Dim conn As New MySqlConnection("Data Source=localhost;Database=employees;Username=root;Password=55551")
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "Image|*.jpg;*.png;*.bmp"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            TextBox4.Text = OpenFileDialog1.FileName
            PictureBox1.Image = Image.FromFile(TextBox4.Text)
        Else
            MessageBox.Show("Please Select Image")
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim cmd1 As New MySqlCommand("Insert Into names(firstname,lastname,telephone,image_path)Values(@firstname,@lastname,@telephone,@image_path)", conn)
        cmd1.Parameters.AddWithValue("firstname", TextBox1.Text.Trim)
        cmd1.Parameters.AddWithValue("lastname", TextBox2.Text.Trim)
        cmd1.Parameters.AddWithValue("telephone", TextBox3.Text.Trim)
        cmd1.Parameters.AddWithValue("image_path", TextBox4.Text.Trim)
        conn.Open()
        cmd1.ExecuteNonQuery()
        conn.Close()
        MessageBox.Show("Data Inserted Successfully")
        display_data()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        PictureBox1.Image = Nothing
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        display_data()
    End Sub
    Private Sub display_data()
        Dim cmd2 As New MySqlCommand("Select * From names Order By id desc", conn)
        Dim da As New MySqlDataAdapter(cmd2)
        Dim dt As New DataTable
        dt.Clear()
        da.Fill(dt)
        DataGridView1.DataSource = dt
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        idlbl.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString
        TextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
        TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString
        TextBox3.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString
        TextBox4.Text = DataGridView1.CurrentRow.Cells(4).Value.ToString
        PictureBox1.Image = Image.FromFile(TextBox4.Text)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim cmd3 As New MySqlCommand("Update names Set firstname=@firstname,lastname=@lastname,telephone=@telephone,image_path=@image_path Where id=@id", conn)
        cmd3.Parameters.AddWithValue("firstname", TextBox1.Text.Trim)
        cmd3.Parameters.AddWithValue("lastname", TextBox2.Text.Trim)
        cmd3.Parameters.AddWithValue("telephone", TextBox3.Text.Trim)
        cmd3.Parameters.AddWithValue("image_path", TextBox4.Text)
        cmd3.Parameters.AddWithValue("id", idlbl.Text)
        conn.Open()
        cmd3.ExecuteNonQuery()
        conn.Close()
        MessageBox.Show("Data Updated successfully")
        display_data()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim cmd3 As New MySqlCommand("Delete From names Where id=@id", conn)
        cmd3.Parameters.AddWithValue("id", idlbl.Text)
        conn.Open()
        cmd3.ExecuteNonQuery()
        conn.Close()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        PictureBox1.Image = Nothing
        MessageBox.Show("Data Deleted successfully")
        display_data()
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim cmd2 As New MySqlCommand("Select * From names Where id=@id Order By id desc", conn)
        cmd2.Parameters.AddWithValue("id", TextBox5.Text.Trim)
        Dim da As New MySqlDataAdapter(cmd2)
        Dim dt As New DataTable
        dt.Clear()
        da.Fill(dt)
        DataGridView1.DataSource = dt
    End Sub
End Class