0
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.

2

2 Answers 2

1

You are getting the error message because you haven't initialised the tempArray variable. 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.Add method takes an array, not an array of arrays.

Just use the stringArr variable:

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 <> "" And txtQty.Text <> "" Then

    tblScItem.Rows.Add(stringArr)

  End If

End Sub

Note that the Text property of a control is never Empty, 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 a List(Of String()):

Public tempList As new List(Of String())

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 <> "" And txtQty.Text <> "" Then

    tempList.Add(stringArr)

    tblScItem.Rows.Add(stringArr)

  End If

End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

it works great! next, i would like to export it to a txt file when the export button is clicked. However, only the last record in the datagrid is exported. Private Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click Dim exportFile As String = "C:\Users\scannedItems.txt" Dim objWriter As New System.IO.StreamWriter(exportFile) objWriter.WriteLine(String.Join(",", stringArr)) objWriter.Close() End Sub
Or, Do you think it's possible to get all the rows from the datagrid instead? in the objwriter to export it to the txtfile.
@user1699905: The stringArr variable only contains the last row added. Get the rows from the tempList variable, alternatively from the DataTable.
Oh. How do i get the rows from the tempList? since list doesnt support datarow.
@user1699905: The tempList contains string arrays, just as your tempArray would have. You can just loop trough it and output each array as you did stringArr.
0

Why do you use DataGrid and an array? Just use the datagrid and call Rows(0).ItemArray on the datatable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.