Here is the code which is working fine for me on another form but creating problem when i use the same code on another form it gives me this error message when i try to insert a new record in DB "Value cannot be null Parameter name: Data Table"

and here is the code

Imports System
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Public Class frmprrec
    Private myDA As OleDbDataAdapter
    Private mydataset As DataSet
    Private bindingsource1 = New BindingSource
    Private Sub frmprrec_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.ColumnHeadersDefaultCellStyle.Font = New Font("Tahoma", 9, FontStyle.Bold, GraphicsUnit.Point)
        DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.ControlDark
        DataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.[Single]
        DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DataGridView1.DefaultCellStyle.Font = New Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point)
        DataGridView1.DefaultCellStyle.BackColor = Color.Empty
        DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.ControlLight
        DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.[Single]
        DataGridView1.GridColor = SystemColors.ControlDarkDark

        DataGridView1.AutoGenerateColumns = False

        Dim clmprno As New DataGridViewTextBoxColumn
        clmprno.HeaderText = "PR NO"
        clmprno.DataPropertyName = "PR_NO"
        DataGridView1.Columns.Add(clmprno)

        Dim clmItemName As New DataGridViewTextBoxColumn
        clmItemName.HeaderText = "ITEM NAME"
        clmItemName.DataPropertyName = "ITEM"
        DataGridView1.Columns.Add(clmItemName)

        Dim clmqty As New DataGridViewTextBoxColumn
        clmqty.HeaderText = "QUANTITY"
        clmqty.DataPropertyName = "QTY"
        DataGridView1.Columns.Add(clmqty)


        Dim clmprr As New DataGridViewTextBoxColumn
        clmprr.HeaderText = "PUR REASON"
        clmprr.DataPropertyName = "PUR_REASON"
        DataGridView1.Columns.Add(clmprr)


        Dim clmPDate As New CalendarColumn
        clmPDate.HeaderText = "PR Date"
        clmPDate.DataPropertyName = "PR_DATE"
        DataGridView1.Columns.Add(clmPDate)


        Dim clmst As New DataGridViewComboBoxColumn
        clmst.HeaderText = "STATUS"
        clmst.DataPropertyName = "STATUS"
        DataGridView1.Columns.Add(clmst)
        clmst.MaxDropDownItems = 10
        clmst.Items.Add("ITM")
        clmst.Items.Add("AITM")
        clmst.Items.Add("PM")
        clmst.Items.Add("SDF")
        BindData()
        DataGridView1.DataSource = bindingsource1
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            For r = 0 To DataGridView1.RowCount - 2 ' Why RowCount-2? 
                For Each cell As DataGridViewCell In DataGridView1.Rows(r).Cells
                    If cell.FormattedValue Is Nothing OrElse cell.FormattedValue.ToString = String.Empty Then
                        MessageBox.Show("Please Enter the Required Field(s)!", "ITPCHR", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                        Exit Sub ' This will exit the sub and not show multiple messageboxes (which in my opinion is pretty irritating to the user)
                    End If
                Next
            Next r
            ' Only Update your changes to the Database if there are no errors 
            Me.Validate()
            Me.myDA.Update(Me.mydataset.Tables("PE_REC"))
            BindData()
            MessageBox.Show("Record Inserted Successfully......", "ITPCHR", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub BindData()
        Try
            myDA = New OleDbDataAdapter("SELECT PR_NO,ITEM,PUR_REASON,PR_DATE,STATUS,QTY FROM PR_REC ORDER BY PR_NO", ConnectionString)
            Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)
            mydataset = New DataSet()
            myDA.Fill(mydataset, "PR_REC")
            mydataset.Tables("PR_REC").Columns(0).AutoIncrement = False
            bindingsource1.DataSource = mydataset.Tables("PR_REC")
        Catch ex As Exception
            bindingsource1.DataSource = Nothing
        End Try
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim a As Integer
        a = MessageBox.Show("Are you sure you want to exit????", "ITPCHR", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
        If a = vbYes Then
            Me.Close()
        Else
            Exit Sub
        End If
    End Sub
End Class

I have resolved the issue thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.