Module globalVariable
Public tblScItem As New DataTable
Public tempArray()
Public index As Integer
Public stringArr() As String
End Module
Private Sub txtQty_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQty.TextChanged
stringArr = New String() {"", txtItem.Text, Form2.cbGondola.SelectedItem, txtQty.Text, DateTime.Now, Form1.txtLoginId.Text}
If txtItem.Text <> Nothing And txtQty.Text <> Nothing Then
index = 0
tempArray(index) = stringArr
tblScItem.Rows.Add(tempArray)
index += 1
End If
End Sub
My program is a stock take program which works in a way that when the quantity of the item is entered, it will display in a datagrid and at the same time, store in an array. After the entire transaction is done, the entire array is exported to a txt file.
I have declared an array stringArr to store all the details of the an item. Then, i used a tempArray to store each item (which contains all the details in stringArr in the individual index of the tempArray.
Example:
tempArray(0) = 'details of item 1 obtained from stringArr
tempArray(1) = 'details of item 2 obtained from stringArr
and so on
However, i kept getting ‘object is not set to an instance of an object’ after the quantity is entered.
Anyone know why? I’m in need of help.
Thank you.
You are getting the error message because you haven’t initialised the
tempArrayvariable. It’s just a reference to an array, but it doesn’t have an array to reference.However, you are trying to put an array in an array, but the
DataRowCollection.Addmethod takes an array, not an array of arrays.Just use the
stringArrvariable:Note that the
Textproperty of a control is neverEmpty, you should check if it is an empty string.If you want to add the rows to a collection other than the
DataTable, you wouldn’t use an array, as it’s not resizable. You would use aList(Of String()):