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)
        GC.Collect()
    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.Rows.Add(dtr)
        OrderedItemHandler.UpdateTable(OrderedItemTable)
        OrderedItemTable = OrderedItemHandler.GetTable(fullSql)
        GC.Collect()
    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)
        OrderedItem.saveOrderedItem(odrItm)
        Ordered_ItemDataGridView.DataSource = OrderedItem.getOrderedItemtable

End Sub

Thank You.
Nishara

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.

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.