Write a VB.NET program to create a player table (PID, PName, Game, no_of_matches). Insert records and update the number of matches of ‘Rohit Sharma’ and display the result in a DataGridView.

Answer:

Imports System

Imports System.Data

Imports System.Data.OleDb

Public Class Form1

    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Saurabh\Desktop\New folder\player.accdb")

    Dim adpt As New OleDbDataAdapter("Select * from player", con)

    Dim cmd As New OleDbCommand

    Dim ds As New DataSet

    Public Function display()

        adpt.Fill(ds, "player")

        DataGridView1.DataSource = ds

        DataGridView1.DataMember = "player"

        Return ds

    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        display()

    End Sub

 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        cmd.Connection = con

        cmd.CommandType = CommandType.Text

        cmd.CommandText = "insert into player values(" & TextBox1.Text & ",'" & TextBox2.Text & "','" & TextBox3.Text & "'," & TextBox4.Text & ")"

        con.Open()

If cmd.ExecuteNonQuery() Then

            MessageBox.Show("Inserted Successfully...!")

        End If

        con.Close()

        ds.Clear()

        display()

    End Sub

 

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        cmd.Connection = con

        cmd.CommandType = CommandType.Text

        cmd.CommandText = "UPDATE player SET no_of_matches = " & TextBox4.Text & " WHERE PName = '" & TextBox2.Text & "'"

        con.Open()

        If cmd.ExecuteNonQuery() Then

            MessageBox.Show("Updated Successfully...!")

        End If

        con.Close()

        con.Close()

        ds.Clear()

        display()

    End Sub


End Class
Scroll to Top