Answered [Solved]simple database program - system.NullReferenceException error

dionysus

New member
Joined
Aug 14, 2009
Messages
2
Programming Experience
1-3
Title pretty much says it. I'm trying to run a simple database and im getting a "System.NullReferenceException" error ) & "Additional Information:Object reference not set to an instance of a object" .

txtItemName.Text = rowCurrent.Item("ItemName") <-- highlighted in yellow

Code is as follows:

VB.NET:
Imports System.Data.OleDB
----------------------------------------------------
Public Class Form1
    Inherits System.Windows.Forms.Form
----------------------------------------------------
    Dim conStock As OleDbConnection
    Dim adpStock As OleDbDataAdapter
    Dim datStock As New DataSet
    Dim rowCurrent As DataRow
    Dim ItemID
-----------------------------------------------------
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Key(0) As DataColumn

        Dim ConnectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + _
        "Data Source=database2.mdb"

        conStock = New OleDbConnection(ConnectString)
        conStock.Open()

        adpStock = New OleDbDataAdapter
        adpStock.SelectCommand = New OleDbCommand
        With adpStock.SelectCommand
            .Connection = conStock
            .CommandText = "SELECT * FROM tblStock"
            .CommandType = CommandType.Text
            .ExecuteNonQuery()
        End With
        adpStock.Fill(datStock, "tblStock")
        Key(0) = datStock.Tables("tblStock").Columns(0)
        datStock.Tables("tblStock").PrimaryKey = Key

        txtItemName.Text = rowCurrent.Item("ItemName")
        txtItemID.Text = rowCurrent.Item("ItemID")
        txtItemSize.Text = rowCurrent.Item("ItemSize")
        txtItemCost.Text = rowCurrent.Item("ItemCost")
        txtItemSupplier.Text = rowCurrent.Item("ItemSupplier")
        txtItemStock.Text = rowCurrent.Item("AmountInStock")
        txtItemType.Text = rowCurrent.Item("ItemType")
        txtItemQuantity.Text = rowCurrent.Item("ItemQuantity")

    End Sub
Thanks for all the help
Cheers, demiurge.
[Note: VB 2003]
 
Last edited:
Usually means that the item you are trying to read is NULL in the DB. You can either make that field required in the DB and turn all current NULL fields into blank strings or you can check to see if each field is DBNULL before trying to use that field.

By the looks of your code it doesn't look like your assigning rowCurrent to an actual row in the DB. You also might want to define rowCurrent closer to where it is actually used. When i just read the code it took me a moment to find its definition.
 
I found another way to do it:
txtItemType.Text = datStock.Tables("tblStock").Rows(ItemRow).Item(1)

Where ItemRow is an integer (i put it by default at 0)

This works fine but id rather use the RowCurrent idea i had going before as this is the same example we were taught to use.

I tried any number of ways to integrate it into RowCurrent but none of them really worked.

Any help would be appreciated,
Cheers, dionysus.
 
You just need to point rowCurrent to the first row in the dataset.

VB.NET:
rowCurrent = datStoc.Tables("tblStock").rows(0)

Then you can use

VB.NET:
txtItemType.Text = rowCurrent(1)

FYI:
XXX.Item(y) can usually(maybe always) be shortened to XXX(y)
 
Back
Top