DivineBlaze33
New member
- Joined
- Jan 19, 2018
- Messages
- 2
- Programming Experience
- Beginner
Hi all, I'm making a program in which the user can update a table of their choice from a list box. When an item in the listbox is selected, it shows the MS-Access database in the DataGridView. When a user selects a cell in the DataGridView, it will show the ID, the field name and the actual data inside the selected cell in textboxes, so the user can change the table. Once they click btnUpdateTablean UPDATE query will be run to change the cell data based on the ID and the field name.However, when clicking btnUpdateTable a message box appears reading:
"No value given for one or more required parameters"
I don't know what the problem is and how to fix it, I don't understand what parameter is null. Thanks all, appreciate it. Below is the code:
"No value given for one or more required parameters"
I don't know what the problem is and how to fix it, I don't understand what parameter is null. Thanks all, appreciate it. Below is the code:
VB.NET:
Imports System.Data.OleDb
Public Class frmViewTables
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\SAC1 Database.mdb"
Dim MyConn As New OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Dim dt As DataTable
Dim SelectedTable As String
Private Sub lbxTables_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbxTables.SelectedIndexChanged
If lbxTables.SelectedItem = "tblOrders" And frmLogin.AdminDetails = True Then
SelectedTable = "tblOrders"
Dim source1 As New BindingSource
Dim ds = New DataSet
Dim tables = ds.Tables
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\SAC1 Database.mdb")
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
Dim cmd As New OleDbCommand("Select * from [tblOrders]", cn)
da.SelectCommand = cmd
da.Fill(ds, "tblOrders")
Dim view As New DataView(tables(0))
source1.DataSource = view
dgvDynamic.DataSource = view
ElseIf lbxTables.SelectedItem = "tblOrders" Then
SelectedTable = "tblOrders"
Dim source1 As New BindingSource
Dim ds = New DataSet
Dim tables = ds.Tables
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\SAC1 Database.mdb")
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
Dim cmd As New OleDbCommand("Select * from [tblOrders] where Username = @username", cn)
cmd.Parameters.Add("@username", OleDbType.VarChar, 255).Value = frmLogin.SuccessfulLoginUsername
da.SelectCommand = cmd
da.Fill(ds, "tblOrders")
Dim view As New DataView(tables(0))
source1.DataSource = view
dgvDynamic.DataSource = view
End If
If lbxTables.SelectedItem = "tblTickets" And frmLogin.AdminDetails = True Then
SelectedTable = "tblTickets"
Dim source1 As New BindingSource
Dim ds = New DataSet
Dim tables = ds.Tables
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\SAC1 Database.mdb")
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
Dim cmd As New OleDbCommand("Select * from [tblTickets]", cn)
da.SelectCommand = cmd
da.Fill(ds, "tblTickets")
Dim view As New DataView(tables(0))
source1.DataSource = view
dgvDynamic.DataSource = view
ElseIf lbxTables.SelectedItem = "tblTickets" Then
SelectedTable = "tblTickets"
Dim source1 As New BindingSource
Dim ds = New DataSet
Dim tables = ds.Tables
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\SAC1 Database.mdb")
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
Dim cmd As New OleDbCommand("Select * from [tblTickets] where Username = @username", cn)
cmd.Parameters.Add("@username", OleDbType.VarChar, 255).Value = frmLogin.SuccessfulLoginUsername
da.SelectCommand = cmd
da.Fill(ds, "tblTickets")
Dim view As New DataView(tables(0))
source1.DataSource = view
dgvDynamic.DataSource = view
End If
If lbxTables.SelectedItem = "tblUsers" And frmLogin.AdminDetails = True Then
SelectedTable = "tblUsers"
Dim source1 As New BindingSource
Dim ds = New DataSet
Dim tables = ds.Tables
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\SAC1 Database.mdb")
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
Dim cmd As New OleDbCommand("Select * from [tblUsers]", cn)
da.SelectCommand = cmd
da.Fill(ds, "tblTickets")
Dim view As New DataView(tables(0))
source1.DataSource = view
dgvDynamic.DataSource = view
End If
End Sub
Private Sub frmViewTables_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If frmLogin.AdminDetails = True Then
lbxTables.Items.Add("tblUsers")
lbxTables.Items.Add("tblOrders")
lbxTables.Items.Add("tblTickets")
dgvDynamic.ReadOnly = False
Else
lbxTables.Items.Add("tblOrders")
lbxTables.Items.Add("tblTickets")
End If
End Sub
Private Sub dgvDynamic_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDynamic.CellClick
txtID.Text = dgvDynamic.CurrentRow.Cells(0).Value.ToString()
txtSelectedCellData.Text = dgvDynamic.CurrentCell.Value.ToString()
For Each c As DataGridViewCell In dgvDynamic.SelectedCells
txtColumnName.Text = (dgvDynamic.Columns(c.ColumnIndex).HeaderText)
Next
End Sub
Private Sub GetRecordID()
Dim rowContent As String = String.Empty
dgvDynamic.CurrentRow.Cells(0).Value.ToString()
MessageBox.Show(dgvDynamic.CurrentRow.Cells(0).Value.ToString())
End Sub
Private Sub btnGetID_Click(sender As Object, e As EventArgs) Handles btnGetID.Click
GetRecordID()
End Sub
Private Sub GetColumnName()
For Each c As DataGridViewCell In dgvDynamic.SelectedCells
MessageBox.Show(dgvDynamic.Columns(c.ColumnIndex).HeaderText)
Next
End Sub
Private Sub btnGetColumnName_Click(sender As Object, e As EventArgs) Handles btnGetColumnName.Click
GetColumnName()
End Sub
Private Sub btnUpdateTable_Click(sender As Object, e As EventArgs) Handles btnUpdateTable.Click
Dim tblName As String = SelectedTable
Dim colName As String = txtColumnName.Text
Dim recID As String = txtID.Text
Dim NewData As String = txtSelectedCellData.Text
Try
Dim con As New OleDbConnection(connString)
con.Open()
Dim cmd As New OleDbCommand("UPDATE [" & tblName & "] SET [" & colName & "] = @NewData WHERE ID = @recID", con)
cm.Parameters.Add(New OleDbParameter("@NewData", OleDbType.VarChar, 255))
cm.Parameters.Add(New OleDbParameter("@recID", OleDbType.VarChar, 255))
cm.Parameters("@NewData").Value = NewData
cm.Parameters("@recID").Value = recID
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End Class