ms.tery
ms.tery

Reputation: 149

how to get the data in dataset in vb.net

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

Upvotes: 1

Views: 4648

Answers (1)

Jaume
Jaume

Reputation: 3780

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.

Upvotes: 1

Related Questions