1

im trying to get the data from the database. in the first loop it can get the data but in the second loop it will pop up a error saying:

Object reference not set to an instance of an object.

heres my code:

        da = New SqlDataAdapter("SELECT refno, pono FROM transmital WHERE refname = '" & txtTransRefName.Text & "' ORDER BY refno", DB.DARConString)
        ds = New DataSet
        da.Fill(ds, "refnam")

        xlWb = xlApp.Workbooks.Add()

        For i = 0 To ds.Tables("refnam").Rows.Count - 1

            **'this line gets the error** refnum = ds.Tables("refnam").Rows(i).Item("refno").ToString.Trim
            ponum = ds.Tables("refnam").Rows(i).Item("pono").ToString.Trim
        Next

any help will be highly appreciated. Thank you

ms.tery
  • 149
  • 1
  • 4
  • 15

1 Answers1

1

looking at your code, seems that you are trying to get data from "refnam" table, right? then, what is "transmital"? your DB Name?

da = New SqlDataAdapter("SELECT refno, pono FROM transmital WHERE refname = '" & txtTransRefName.Text & "' ORDER BY refno", DB.DARConString)
            ds = New DataSet
            da.Fill(ds, "refnam")

your queryString should looks like as "SELECT refno, pono FROM refnam ..."

    Dim Adpt As New SqlDataAdapter(queryString, SQLConn)
                Dim ds As New DataSet()
                Adpt.Fill(ds, "refnam")
    MyDataGridView.DataSource = ds.Tables(0)

If you would like to present results on a datagridView before saving to xls, datagridView.columns.clear or non previous set columns are necessary.

Jaume
  • 3,672
  • 19
  • 60
  • 119