I am new to vb.net and I'm doing a small project from vb.net.I want to insert values to a sql server database table from a windows form.I have written a vb class called OrderedItem.vb to add a new data row and assign variable values to that table.In the Form, for button_Click event I have called an object from that OrderedItem class and text field values are assigned to those variables in OrderedItem class.When I run this code the above mentioned error is occered in the highlighted line .Please help me to get through this.

Connections to the server is coded in another class called DataAccessLayer.

This is the code snippest of OrderedItem class

Public Class OrderedItem
    Public ItemID As Integer
    Public OrderID As Integer
    Public RequiredQty As Integer
    Public Quantity As Integer
    Public CurrentQty As Integer
    Private Shared fullSql As String

    Private Shared OrderedItemHandler As DataAccesslayer
    Private Shared OrderedItemTable As DataTable

    Public Shared Sub initialize()
        fullSql = "Select * From Ordered_Item"
        OrderedItemHandler = New DataAccesslayer
        OrderedItemHandler.fullSql = fullSql
        OrderedItemTable = OrderedItemHandler.GetTable(fullSql)
    End Sub

    Public Shared Sub saveOrderedItem(ByVal itm As OrderedItem)
        Dim dtr As DataRow
        dtr = OrderedItemTable.NewRow

        dtr("ItmID") = itm.ItemID
        dtr("OdrID") = itm.OrderID
        dtr("OrderedQty") = itm.Quantity
        dtr("CrntQty") = itm.CurrentQty
        dtr("RequiredQty") = itm.RequiredQty

        OrderedItemTable = OrderedItemHandler.GetTable(fullSql)
    End Sub

    Public Shared Function getOrderedItemtable() As DataTable
        Return OrderedItemTable
    End Function

End Class

This is the form's button_click event

Private Sub btnAddOder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 
Dim odrItm As New OrderedItem

        odrItm.ItemID = txtItmID.Text
        odrItm.Quantity = txtQuant.Text
        odrItm.OrderID = txtOrderID.Text
        odrItm.CurrentQty = lblStock.Text
        odrItm.RequiredQty = Integer.Parse(txtQuant.Text) - Integer.Parse(lblStock.Text)
        Ordered_ItemDataGridView.DataSource = OrderedItem.getOrderedItemtable

End Sub

Thank You.

The OrderedItemTable may not be initialized.

I believe that you are instantiating OrderedItemTable in the following line in
initialize() method.

OrderedItemTable = OrderedItemHandler.GetTable(fullSql)

The above line may not return a DataTable and therefore OrderedItemTable is still not initialized.
Therefore you cannot call NewRow method on an uninitialized DataTable.

Check the above line if it returns a datatable.

This article has been dead for over six months. Start a new discussion instead.